From 60934fa06dc226349954ecade4e02c16af82b3aa Mon Sep 17 00:00:00 2001 From: Vol-Alex Date: Tue, 28 Sep 2021 17:51:58 +0200 Subject: [PATCH] Fix large heap allocation during load broken files. --- kaitai/kaitaistream.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/kaitai/kaitaistream.cpp b/kaitai/kaitaistream.cpp index 32e757a..1b5c46a 100644 --- a/kaitai/kaitaistream.cpp +++ b/kaitai/kaitaistream.cpp @@ -379,19 +379,20 @@ uint64_t kaitai::kstream::get_mask_ones(int n) { // ======================================================================== std::string kaitai::kstream::read_bytes(std::streamsize len) { - std::vector result(len); - // NOTE: streamsize type is signed, negative values are only *supposed* to not be used. // http://en.cppreference.com/w/cpp/io/streamsize if (len < 0) { throw std::runtime_error("read_bytes: requested a negative amount"); + } else if (len == 0) { + return std::string(); + } else if (len > size()) { + throw std::runtime_error("read_bytes: requested length greater than stream size"); } - if (len > 0) { - m_io->read(&result[0], len); - } + std::string result(len, ' '); + m_io->read(&result[0], len); - return std::string(result.begin(), result.end()); + return result; } std::string kaitai::kstream::read_bytes_full() {