Skip to content

Commit 971dc96

Browse files
committed
Fix large heap allocation during load broken files.
1 parent cb09b3a commit 971dc96

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

kaitai/kaitaistream.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,23 @@ 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+
{
392+
throw std::runtime_error("read_bytes: requested length greater than stream size");
392393
}
393394

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

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

0 commit comments

Comments
 (0)