zippy: Fix data corruption when compressed output is more than 4GB

PiperOrigin-RevId: 741053848
This commit is contained in:
Danila Kutenin
2025-03-27 07:59:41 +00:00
parent dca57a700f
commit ec20182d26
2 changed files with 17 additions and 3 deletions

View File

@@ -1496,7 +1496,7 @@ class SnappyDecompressor {
// If ip < ip_limit_min_maxtaglen_ it's safe to read kMaxTagLength from
// buffer.
const char* ip_limit_min_maxtaglen_;
uint32_t peeked_; // Bytes peeked from reader (need to skip)
uint64_t peeked_; // Bytes peeked from reader (need to skip)
bool eof_; // Hit end of input without an error?
char scratch_[kMaximumTagLength]; // See RefillTag().
@@ -1723,7 +1723,7 @@ bool SnappyDecompressor::RefillTag() {
assert(needed <= sizeof(scratch_));
// Read more bytes from reader if needed
uint32_t nbuf = ip_limit_ - ip;
uint64_t nbuf = ip_limit_ - ip;
if (nbuf < needed) {
// Stitch together bytes from ip and reader to form the word
// contents. We store the needed bytes in "scratch_". They
@@ -1736,7 +1736,7 @@ bool SnappyDecompressor::RefillTag() {
size_t length;
const char* src = reader_->Peek(&length);
if (length == 0) return false;
uint32_t to_add = std::min<uint32_t>(needed - nbuf, length);
uint64_t to_add = std::min<uint64_t>(needed - nbuf, length);
std::memcpy(scratch_ + nbuf, src, to_add);
nbuf += to_add;
reader_->Skip(to_add);
@@ -1794,6 +1794,7 @@ size_t Compress(Source* reader, Sink* writer, CompressionOptions options) {
int snappy_token = 0;
size_t written = 0;
size_t N = reader->Available();
assert(N <= 0xFFFFFFFFu);
const size_t uncompressed_size = N;
char ulength[Varint::kMax32];
char* p = Varint::Encode32(ulength, N);