diff --git a/snappy.cc b/snappy.cc index ab61792..9826151 100644 --- a/snappy.cc +++ b/snappy.cc @@ -74,6 +74,7 @@ #include #include #include +#include #include #include #include @@ -194,6 +195,11 @@ inline uint16_t* TableEntry8ByteMatch(uint16_t* table, uint64_t bytes, } // namespace size_t MaxCompressedLength(size_t source_bytes) { + // Avoid integer overflow that could cause undersized buffer allocations. + // Return std::numeric_limits::max() to force a controlled allocation failure. + if (source_bytes > (std::numeric_limits::max() - 32) / 7 * 6) { + return std::numeric_limits::max(); + } // Compressed data can be defined as: // compressed := item* literal* // item := literal* copy