xref: /aosp_15_r20/external/cronet/testing/libfuzzer/fuzzers/snappy_uncompress_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 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 <stddef.h>
6 #include <stdint.h>
7 
8 #include "third_party/snappy/src/snappy-sinksource.h"
9 #include "third_party/snappy/src/snappy.h"
10 
11 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)12 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
13   snappy::ByteArraySource src(reinterpret_cast<const char*>(data), size);
14   uint32_t len;
15   // Note: src is invalid after GetUncompressedLength call.
16   if (!snappy::GetUncompressedLength(&src, &len) || (len > 1E6)) {
17     // We have to bail out, to avoid self-crafted decompression bombs.
18     return 0;
19   }
20 
21   std::string uncompressed_str;
22   snappy::Uncompress(reinterpret_cast<const char*>(data), size,
23                      &uncompressed_str);
24   return 0;
25 }
26