Sync with various Google-internal changes.

Should not mean much for the open-source version.
This commit is contained in:
Steinar H. Gunderson
2015-06-22 16:07:58 +02:00
parent 22acaf438e
commit 11ccdfb868
3 changed files with 27 additions and 33 deletions

View File

@@ -132,7 +132,7 @@ namespace File {
} // namespace File } // namespace File
namespace file { namespace file {
int Defaults() { } int Defaults() { return 0; }
class DummyStatus { class DummyStatus {
public: public:
@@ -158,6 +158,8 @@ namespace file {
} }
fclose(fp); fclose(fp);
return DummyStatus();
} }
DummyStatus SetContents(const string& filename, DummyStatus SetContents(const string& filename,
@@ -176,6 +178,8 @@ namespace file {
} }
fclose(fp); fclose(fp);
return DummyStatus();
} }
} // namespace file } // namespace file
@@ -572,6 +576,7 @@ class LogMessageVoidify {
#define CHECK_NE(a, b) CRASH_UNLESS((a) != (b)) #define CHECK_NE(a, b) CRASH_UNLESS((a) != (b))
#define CHECK_LT(a, b) CRASH_UNLESS((a) < (b)) #define CHECK_LT(a, b) CRASH_UNLESS((a) < (b))
#define CHECK_GT(a, b) CRASH_UNLESS((a) > (b)) #define CHECK_GT(a, b) CRASH_UNLESS((a) > (b))
#define CHECK_OK(cond) (cond).CheckSuccess()
} // namespace } // namespace

View File

@@ -1241,7 +1241,6 @@ bool Uncompress(const char* compressed, size_t n, string* uncompressed) {
return RawUncompress(compressed, n, string_as_array(uncompressed)); return RawUncompress(compressed, n, string_as_array(uncompressed));
} }
// A Writer that drops everything on the floor and just does validation // A Writer that drops everything on the floor and just does validation
class SnappyDecompressionValidator { class SnappyDecompressionValidator {
private: private:

View File

@@ -59,7 +59,6 @@ DEFINE_bool(fastlz, false,
"Run FastLZ compression (http://www.fastlz.org/"); "Run FastLZ compression (http://www.fastlz.org/");
DEFINE_bool(snappy, true, "Run snappy compression"); DEFINE_bool(snappy, true, "Run snappy compression");
DEFINE_bool(write_compressed, false, DEFINE_bool(write_compressed, false,
"Write compressed versions of each file to <file>.comp"); "Write compressed versions of each file to <file>.comp");
DEFINE_bool(write_uncompressed, false, DEFINE_bool(write_uncompressed, false,
@@ -278,7 +277,6 @@ static bool Compress(const char* input, size_t input_size, CompressorType comp,
break; break;
} }
default: { default: {
return false; // the asked-for library wasn't compiled in return false; // the asked-for library wasn't compiled in
} }
@@ -370,7 +368,6 @@ static bool Uncompress(const string& compressed, CompressorType comp,
break; break;
} }
default: { default: {
return false; // the asked-for library wasn't compiled in return false; // the asked-for library wasn't compiled in
} }
@@ -474,7 +471,6 @@ static void Measure(const char* data,
urate.c_str()); urate.c_str());
} }
static int VerifyString(const string& input) { static int VerifyString(const string& input) {
string compressed; string compressed;
DataEndingAtUnreadablePage i(input); DataEndingAtUnreadablePage i(input);
@@ -589,12 +585,9 @@ static int Verify(const string& input) {
VerifyIOVec(input); VerifyIOVec(input);
} }
return result; return result;
} }
// This test checks to ensure that snappy doesn't coredump if it gets
// corrupted data.
static bool IsValidCompressedBuffer(const string& c) { static bool IsValidCompressedBuffer(const string& c) {
return snappy::IsValidCompressedBuffer(c.data(), c.size()); return snappy::IsValidCompressedBuffer(c.data(), c.size());
@@ -603,11 +596,13 @@ static bool Uncompress(const string& c, string* u) {
return snappy::Uncompress(c.data(), c.size(), u); return snappy::Uncompress(c.data(), c.size(), u);
} }
TYPED_TEST(CorruptedTest, VerifyCorrupted) { // This test checks to ensure that snappy doesn't coredump if it gets
// corrupted data.
TEST(CorruptedTest, VerifyCorrupted) {
string source = "making sure we don't crash with corrupted input"; string source = "making sure we don't crash with corrupted input";
VLOG(1) << source; VLOG(1) << source;
string dest; string dest;
TypeParam uncmp; string uncmp;
snappy::Compress(source.data(), source.size(), &dest); snappy::Compress(source.data(), source.size(), &dest);
// Mess around with the data. It's hard to simulate all possible // Mess around with the data. It's hard to simulate all possible
@@ -616,8 +611,8 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
dest[1]--; dest[1]--;
dest[3]++; dest[3]++;
// this really ought to fail. // this really ought to fail.
CHECK(!IsValidCompressedBuffer(TypeParam(dest))); CHECK(!IsValidCompressedBuffer(dest));
CHECK(!Uncompress(TypeParam(dest), &uncmp)); CHECK(!Uncompress(dest, &uncmp));
// This is testing for a security bug - a buffer that decompresses to 100k // This is testing for a security bug - a buffer that decompresses to 100k
// but we lie in the snappy header and only reserve 0 bytes of memory :) // but we lie in the snappy header and only reserve 0 bytes of memory :)
@@ -627,8 +622,8 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
} }
snappy::Compress(source.data(), source.size(), &dest); snappy::Compress(source.data(), source.size(), &dest);
dest[0] = dest[1] = dest[2] = dest[3] = 0; dest[0] = dest[1] = dest[2] = dest[3] = 0;
CHECK(!IsValidCompressedBuffer(TypeParam(dest))); CHECK(!IsValidCompressedBuffer(dest));
CHECK(!Uncompress(TypeParam(dest), &uncmp)); CHECK(!Uncompress(dest, &uncmp));
if (sizeof(void *) == 4) { if (sizeof(void *) == 4) {
// Another security check; check a crazy big length can't DoS us with an // Another security check; check a crazy big length can't DoS us with an
@@ -640,8 +635,8 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
dest[0] = dest[1] = dest[2] = dest[3] = 0xff; dest[0] = dest[1] = dest[2] = dest[3] = 0xff;
// This decodes to a really large size, i.e., about 3 GB. // This decodes to a really large size, i.e., about 3 GB.
dest[4] = 'k'; dest[4] = 'k';
CHECK(!IsValidCompressedBuffer(TypeParam(dest))); CHECK(!IsValidCompressedBuffer(dest));
CHECK(!Uncompress(TypeParam(dest), &uncmp)); CHECK(!Uncompress(dest, &uncmp));
} else { } else {
LOG(WARNING) << "Crazy decompression lengths not checked on 64-bit build"; LOG(WARNING) << "Crazy decompression lengths not checked on 64-bit build";
} }
@@ -649,8 +644,8 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
// This decodes to about 2 MB; much smaller, but should still fail. // This decodes to about 2 MB; much smaller, but should still fail.
dest[0] = dest[1] = dest[2] = 0xff; dest[0] = dest[1] = dest[2] = 0xff;
dest[3] = 0x00; dest[3] = 0x00;
CHECK(!IsValidCompressedBuffer(TypeParam(dest))); CHECK(!IsValidCompressedBuffer(dest));
CHECK(!Uncompress(TypeParam(dest), &uncmp)); CHECK(!Uncompress(dest, &uncmp));
// try reading stuff in from a bad file. // try reading stuff in from a bad file.
for (int i = 1; i <= 3; ++i) { for (int i = 1; i <= 3; ++i) {
@@ -665,8 +660,8 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
snappy::ByteArraySource source(data.data(), data.size()); snappy::ByteArraySource source(data.data(), data.size());
CHECK(!snappy::GetUncompressedLength(&source, &ulen2) || CHECK(!snappy::GetUncompressedLength(&source, &ulen2) ||
(ulen2 < (1<<20))); (ulen2 < (1<<20)));
CHECK(!IsValidCompressedBuffer(TypeParam(data))); CHECK(!IsValidCompressedBuffer(data));
CHECK(!Uncompress(TypeParam(data), &uncmp)); CHECK(!Uncompress(data, &uncmp));
} }
} }
@@ -929,7 +924,6 @@ TEST(Snappy, IOVecCopyOverflow) {
} }
} }
static bool CheckUncompressedLength(const string& compressed, static bool CheckUncompressedLength(const string& compressed,
size_t* ulength) { size_t* ulength) {
const bool result1 = snappy::GetUncompressedLength(compressed.data(), const bool result1 = snappy::GetUncompressedLength(compressed.data(),
@@ -998,7 +992,6 @@ TEST(Snappy, ZeroOffsetCopyValidation) {
EXPECT_FALSE(snappy::IsValidCompressedBuffer(compressed, 4)); EXPECT_FALSE(snappy::IsValidCompressedBuffer(compressed, 4));
} }
namespace { namespace {
int TestFindMatchLength(const char* s1, const char *s2, unsigned length) { int TestFindMatchLength(const char* s1, const char *s2, unsigned length) {
@@ -1125,21 +1118,20 @@ TEST(Snappy, FindMatchLengthRandom) {
} }
} }
static void CompressFile(const char* fname) { static void CompressFile(const char* fname) {
string fullinput; string fullinput;
file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess(); CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
string compressed; string compressed;
Compress(fullinput.data(), fullinput.size(), SNAPPY, &compressed, false); Compress(fullinput.data(), fullinput.size(), SNAPPY, &compressed, false);
file::SetContents(string(fname).append(".comp"), compressed, file::Defaults()) CHECK_OK(file::SetContents(string(fname).append(".comp"), compressed,
.CheckSuccess(); file::Defaults()));
} }
static void UncompressFile(const char* fname) { static void UncompressFile(const char* fname) {
string fullinput; string fullinput;
file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess(); CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
size_t uncompLength; size_t uncompLength;
CHECK(CheckUncompressedLength(fullinput, &uncompLength)); CHECK(CheckUncompressedLength(fullinput, &uncompLength));
@@ -1148,13 +1140,13 @@ static void UncompressFile(const char* fname) {
uncompressed.resize(uncompLength); uncompressed.resize(uncompLength);
CHECK(snappy::Uncompress(fullinput.data(), fullinput.size(), &uncompressed)); CHECK(snappy::Uncompress(fullinput.data(), fullinput.size(), &uncompressed));
file::SetContents(string(fname).append(".uncomp"), uncompressed, CHECK_OK(file::SetContents(string(fname).append(".uncomp"), uncompressed,
file::Defaults()).CheckSuccess(); file::Defaults()));
} }
static void MeasureFile(const char* fname) { static void MeasureFile(const char* fname) {
string fullinput; string fullinput;
file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess(); CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
printf("%-40s :\n", fname); printf("%-40s :\n", fname);
int start_len = (FLAGS_start_len < 0) ? fullinput.size() : FLAGS_start_len; int start_len = (FLAGS_start_len < 0) ? fullinput.size() : FLAGS_start_len;
@@ -1329,7 +1321,6 @@ static void BM_ZFlat(int iters, int arg) {
} }
BENCHMARK(BM_ZFlat)->DenseRange(0, ARRAYSIZE(files) - 1); BENCHMARK(BM_ZFlat)->DenseRange(0, ARRAYSIZE(files) - 1);
} // namespace snappy } // namespace snappy
@@ -1337,7 +1328,6 @@ int main(int argc, char** argv) {
InitGoogle(argv[0], &argc, &argv, true); InitGoogle(argv[0], &argc, &argv, true);
RunSpecifiedBenchmarks(); RunSpecifiedBenchmarks();
if (argc >= 2) { if (argc >= 2) {
for (int arg = 1; arg < argc; arg++) { for (int arg = 1; arg < argc; arg++) {
if (FLAGS_write_compressed) { if (FLAGS_write_compressed) {