Merge pull request #242 from yusmer96-maker/yusmer96-maker-patch-1
Implement overflow check in MaxCompressedLength function
diff --git a/snappy.cc b/snappy.cc
index ab61792..9826151 100644
--- a/snappy.cc
+++ b/snappy.cc
@@ -74,6 +74,7 @@
#include <cstdint>
#include <cstdio>
#include <cstring>
+#include <limits>
#include <memory>
#include <string>
#include <utility>
@@ -194,6 +195,11 @@
} // namespace
size_t MaxCompressedLength(size_t source_bytes) {
+ // Avoid integer overflow that could cause undersized buffer allocations.
+ // Return std::numeric_limits<size_t>::max() to force a controlled allocation failure.
+ if (source_bytes > (std::numeric_limits<size_t>::max() - 32) / 7 * 6) {
+ return std::numeric_limits<size_t>::max();
+ }
// Compressed data can be defined as:
// compressed := item* literal*
// item := literal* copy