| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2477e79 commit 654c8d1
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,10 +71,6 @@ Data read_file_data_or_exit(const char* name) { | |||
| 71 | 71 | return data; | |
| 72 | 72 | } | |
| 73 | 73 | ||
| 74 | - size_t zlib_estimate_compressed_size(size_t input_size) { | ||
| 75 | - return compressBound(input_size); | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | 74 | enum zlib_wrapper { | |
| 79 | 75 | kWrapperNONE, | |
| 80 | 76 | kWrapperZLIB, | |
@@ -128,10 +124,6 @@ void zlib_compress( | |||
| 128 | 124 | std::string* output, | |
| 129 | 125 | bool resize_output = false) | |
| 130 | 126 | { | |
| 131 | - if (resize_output) | ||
| 132 | - output->resize(zlib_estimate_compressed_size(input_size)); | ||
| 133 | - size_t output_size = output->size(); | ||
| 134 | - | ||
| 135 | 127 | z_stream stream; | |
| 136 | 128 | memset(&stream, 0, sizeof(stream)); | |
| 137 | 129 | ||
@@ -140,6 +132,11 @@ void zlib_compress( | |||
| 140 | 132 | if (result != Z_OK) | |
| 141 | 133 | error_exit("deflateInit2 failed", result); | |
| 142 | 134 | ||
| 135 | + if (resize_output) { | ||
| 136 | + output->resize(deflateBound(&stream, input_size)); | ||
| 137 | + } | ||
| 138 | + size_t output_size = output->size(); | ||
| 139 | + | ||
| 143 | 140 | stream.next_out = (Bytef*)string_data(output); | |
| 144 | 141 | stream.avail_out = (uInt)output_size; | |
| 145 | 142 | stream.next_in = (z_const Bytef*)input; | |
@@ -299,19 +296,14 @@ void zlib_file(const char* name, | |||
| 299 | 296 | ||
| 300 | 297 | // Pre-grow the output buffer so we don't measure string resize time. | |
| 301 | 298 | for (int b = 0; b < blocks; ++b) | |
| 302 | - compressed[b].resize(zlib_estimate_compressed_size(block_size)); | ||
| 299 | + zlib_compress(type, input[b], input_length[b], &compressed[b], true); | ||
| 303 | 300 | ||
| 304 | 301 | auto start = now(); | |
| 305 | 302 | for (int b = 0; b < blocks; ++b) | |
| 306 | 303 | for (int r = 0; r < repeats; ++r) | |
| 307 | 304 | zlib_compress(type, input[b], input_length[b], &compressed[b]); | |
| 308 | 305 | ctime[run] = std::chrono::duration<double>(now() - start).count(); | |
| 309 | 306 | ||
| 310 | - // Compress again, resizing compressed, so we don't leave junk at the | ||
| 311 | - // end of the compressed string that could confuse zlib_uncompress(). | ||
| 312 | - for (int b = 0; b < blocks; ++b) | ||
| 313 | - zlib_compress(type, input[b], input_length[b], &compressed[b], true); | ||
| 314 | - | ||
| 315 | 307 | for (int b = 0; b < blocks; ++b) | |
| 316 | 308 | output[b].resize(input_length[b]); | |
| 317 | 309 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,6 +34,11 @@ fuzzer_test("zlib_deflate_set_dictionary_fuzzer") { | |||
| 34 | 34 | deps = [ "../../../:zlib" ] | |
| 35 | 35 | } | |
| 36 | 36 | ||
| 37 | + fuzzer_test("zlib_compress_fuzzer") { | ||
| 38 | + sources = [ "compress_fuzzer.cc" ] | ||
| 39 | + deps = [ "../../../:zlib" ] | ||
| 40 | + } | ||
| 41 | + | ||
| 37 | 42 | fuzzer_test("zlib_deflate_fuzzer") { | |
| 38 | 43 | sources = [ "deflate_fuzzer.cc" ] | |
| 39 | 44 | deps = [ "../../../:zlib" ] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,46 @@ | |||
| 1 | + // Copyright 2024 The Chromium Authors | ||
| 2 | + // Use of this source code is governed by a BSD-style license that can be | ||
| 3 | + // found in the LICENSE file. | ||
| 4 | + | ||
| 5 | + #include <fuzzer/FuzzedDataProvider.h> | ||
| 6 | + | ||
| 7 | + #include <vector> | ||
| 8 | + | ||
| 9 | + #include "zlib.h" | ||
| 10 | + | ||
| 11 | + // Fuzzer builds often have NDEBUG set, so roll our own assert macro. | ||
| 12 | + #define ASSERT(cond) \ | ||
| 13 | + do { \ | ||
| 14 | + if (!(cond)) { \ | ||
| 15 | + fprintf(stderr, "%s:%d Assert failed: %s\n", __FILE__, __LINE__, #cond); \ | ||
| 16 | + exit(1); \ | ||
| 17 | + } \ | ||
| 18 | + } while (0) | ||
| 19 | + | ||
| 20 | + extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | ||
| 21 | + FuzzedDataProvider fdp(data, size); | ||
| 22 | + const int level = fdp.PickValueInArray({-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); | ||
| 23 | + const std::vector<uint8_t> src = fdp.ConsumeRemainingBytes<uint8_t>(); | ||
| 24 | + | ||
| 25 | + const unsigned long compress_bound = compressBound(src.size()); | ||
| 26 | + std::vector<uint8_t> compressed; | ||
| 27 | + compressed.resize(compress_bound); | ||
| 28 | + | ||
| 29 | + unsigned long compressed_size = compress_bound; | ||
| 30 | + int ret = compress2(compressed.data(), &compressed_size, src.data(), | ||
| 31 | + src.size(), level); | ||
| 32 | + ASSERT(ret == Z_OK); | ||
| 33 | + ASSERT(compressed_size <= compress_bound); | ||
| 34 | + compressed.resize(compressed_size); | ||
| 35 | + | ||
| 36 | + std::vector<uint8_t> uncompressed; | ||
| 37 | + uncompressed.resize(src.size()); | ||
| 38 | + unsigned long uncompressed_size = uncompressed.size(); | ||
| 39 | + ret = uncompress(uncompressed.data(), &uncompressed_size, compressed.data(), | ||
| 40 | + compressed.size()); | ||
| 41 | + ASSERT(ret == Z_OK); | ||
| 42 | + ASSERT(uncompressed_size == src.size()); | ||
| 43 | + ASSERT(uncompressed == src); | ||
| 44 | + | ||
| 45 | + return 0; | ||
| 46 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,5 +2,5 @@ | |||
| 2 | 2 | // Refer to tools/dep_updaters/update-zlib.sh | |
| 3 | 3 | #ifndef SRC_ZLIB_VERSION_H_ | |
| 4 | 4 | #define SRC_ZLIB_VERSION_H_ | |
| 5 | - #define ZLIB_VERSION "1.3.0.1-motley-8b7eff8" | ||
| 5 | + #define ZLIB_VERSION "1.3.0.1-motley-68e57e6" | ||
| 6 | 6 | #endif // SRC_ZLIB_VERSION_H_ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments