Skip to content

Commit 36d7a19

Browse files
committed
Fix large heap allocation during load broken files.
1 parent cb09b3a commit 36d7a19

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

kaitai/kaitaistream.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,22 @@ uint64_t kaitai::kstream::get_mask_ones(int n) {
379379
// ========================================================================
380380

381381
std::string kaitai::kstream::read_bytes(std::streamsize len) {
382-
std::vector<char> result(len);
383-
384382
// NOTE: streamsize type is signed, negative values are only *supposed* to not be used.
385383
// http://en.cppreference.com/w/cpp/io/streamsize
386384
if (len < 0) {
387385
throw std::runtime_error("read_bytes: requested a negative amount");
388386
}
389-
390-
if (len > 0) {
391-
m_io->read(&result[0], len);
387+
else if (len == 0) {
388+
return std::string();
389+
}
390+
else if (len > size()) {
391+
throw std::runtime_error("read_bytes: requested length greater than stream size");
392392
}
393393

394-
return std::string(result.begin(), result.end());
394+
std::string result(len, 0);
395+
m_io->read(&result[0], len);
396+
397+
return result;
395398
}
396399

397400
std::string kaitai::kstream::read_bytes_full() {

0 commit comments

Comments
 (0)