xref: /aosp_15_r20/external/cronet/third_party/icu/fuzzers/icu_converter_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
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 <algorithm>
9 #include <array>
10 #include <memory>
11 #include <vector>
12 
13 #include "third_party/icu/source/common/unicode/unistr.h"
14 #include "third_party/icu/source/common/unicode/ucnv.h"
15 #include "third_party/icu/fuzzers/fuzzer_utils.h"
16 
17 IcuEnvironment* env = new IcuEnvironment();
18 
19 template <typename T>
20 using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
21 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)22 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
23   UErrorCode status = U_ZERO_ERROR;
24   auto rng = CreateRng(data, size);
25   icu::UnicodeString str(UnicodeStringFromUtf8(data, size));
26 
27   const char* converter_name =
28       ucnv_getAvailableName(rng() % ucnv_countAvailable());
29 
30   deleted_unique_ptr<UConverter> converter(ucnv_open(converter_name, &status),
31                                            &ucnv_close);
32 
33   if (U_FAILURE(status))
34     return 0;
35 
36   static const size_t dest_buffer_size = 1024 * 1204;
37   static const std::unique_ptr<char[]> dest_buffer(new char[dest_buffer_size]);
38 
39   str.extract(dest_buffer.get(), dest_buffer_size, converter.get(), status);
40 
41   if (U_FAILURE(status))
42     return 0;
43 
44   return 0;
45 }
46