Skip to content

Commit 1b34386

Browse files
committed
porting internal changes
replicating cl/361925036
1 parent c74dc47 commit 1b34386

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

app/rest/tests/zlibwrapper_unittest.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,5 +1047,34 @@ TEST(ZLibWrapperStandalone, ReadPastEndOfWindow) {
10471047
LOG(INFO) << "passed read-past-end-of-window test";
10481048
}
10491049

1050+
TEST(ZLibWrapperStandalone, GzipUncompressedLength) {
1051+
ZLib zlib;
1052+
1053+
// "Hello, World!", compressed.
1054+
std::string hello_world(
1055+
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xf3\x48\xcd\xc9\xc9"
1056+
"\xd7\x51\x08\xcf\x2f\xca\x49\x51\x04\x00\xd0\xc3\x4a\xec\x0d"
1057+
"\x00\x00\x00",
1058+
33);
1059+
EXPECT_EQ(13, zlib.GzipUncompressedLength(
1060+
reinterpret_cast<const Bytef*>(hello_world.c_str()),
1061+
hello_world.size()));
1062+
1063+
// Empty string, "", compressed.
1064+
std::string empty(
1065+
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x03\x00\x00\x00\x00"
1066+
"\x00\x00\x00\x00\x00",
1067+
20);
1068+
EXPECT_EQ(0,
1069+
zlib.GzipUncompressedLength(
1070+
reinterpret_cast<const Bytef*>(empty.c_str()), empty.size()));
1071+
1072+
std::string bad_data("\x01\x01\x01\x01", 4);
1073+
for (int len = 0; len <= bad_data.size(); len++) {
1074+
EXPECT_EQ(0, zlib.GzipUncompressedLength(
1075+
reinterpret_cast<const Bytef*>(bad_data.c_str()), len));
1076+
}
1077+
}
1078+
10501079
} // namespace
10511080
} // namespace firebase

app/rest/zlibwrapper.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ int ZLib::Uncompress(Bytef* dest, uLongf* destLen, const Bytef* source,
784784

785785
// read uncompress length from gzip footer
786786
uLongf ZLib::GzipUncompressedLength(const Bytef* source, uLong len) {
787-
assert(len > 4);
787+
if (len <= 4) return 0; // malformed data.
788+
788789
return (static_cast<uLongf>(source[len - 1]) << 24) +
789790
(static_cast<uLongf>(source[len - 2]) << 16) +
790791
(static_cast<uLongf>(source[len - 3]) << 8) +

app/rest/zlibwrapper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class ZLib {
126126
int Uncompress(Bytef* dest, uLongf* destLen, const Bytef* source,
127127
uLong sourceLen);
128128

129-
// Get the uncompressed size from the gzip header.
129+
// Get the uncompressed size from the gzip header. Returns 0 if source is too
130+
// short (len < 5).
130131
uLongf GzipUncompressedLength(const Bytef* source, uLong len);
131132

132133
// Special helper function to help uncompress gzipped documents:

0 commit comments

Comments
 (0)