From a9d2c4aa25bdc68ee24b3e61ac29ae4533acdbf7 Mon Sep 17 00:00:00 2001 From: "HenD.YA" Date: Thu, 4 Jun 2026 01:46:43 -0500 Subject: [PATCH 1/3] Implement overflow check in MaxCompressedLength function Add overflow check for buffer allocation in MaxCompressedLength --- snappy.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/snappy.cc b/snappy.cc index ab61792..46d870c 100644 --- a/snappy.cc +++ b/snappy.cc @@ -194,6 +194,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 SIZE_MAX to force a controlled allocation failure. + if (source_bytes > (SIZE_MAX - 32) / 7 * 6) { + return SIZE_MAX; + } // Compressed data can be defined as: // compressed := item* literal* // item := literal* copy From 56396853e39f0d25eaa9715aa045ae5f1c078ceb Mon Sep 17 00:00:00 2001 From: "HenD.YA" Date: Thu, 4 Jun 2026 02:11:18 -0500 Subject: [PATCH 2/3] Refactor MaxCompressedLength to use std::numeric_limits Updated the overflow check to use std::numeric_limits::max() for clarity. --- snappy.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snappy.cc b/snappy.cc index 46d870c..ce8bd88 100644 --- a/snappy.cc +++ b/snappy.cc @@ -195,9 +195,9 @@ inline uint16_t* TableEntry8ByteMatch(uint16_t* table, uint64_t bytes, size_t MaxCompressedLength(size_t source_bytes) { // Avoid integer overflow that could cause undersized buffer allocations. - // Return SIZE_MAX to force a controlled allocation failure. - if (source_bytes > (SIZE_MAX - 32) / 7 * 6) { - return SIZE_MAX; + // 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* From 9d0af7cdb911a7765b953835827d6721f5bc24e3 Mon Sep 17 00:00:00 2001 From: "HenD.YA" Date: Thu, 4 Jun 2026 02:20:44 -0500 Subject: [PATCH 3/3] Add limits header to snappy.cc --- snappy.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/snappy.cc b/snappy.cc index ce8bd88..9826151 100644 --- a/snappy.cc +++ b/snappy.cc @@ -74,6 +74,7 @@ #include #include #include +#include #include #include #include