1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 
3 // Fuzzer for toLower/toUpper
4 
5 #include <stddef.h>
6 #include <stdint.h>
7 #include <memory>
8 #include "third_party/icu/fuzzers/fuzzer_utils.h"
9 #include "third_party/icu/source/common/unicode/ustring.h"
10 
11 IcuEnvironment* env = new IcuEnvironment();
12 
13 template <typename T>
14 using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
15 
16 // Most locale case convert the same, but we know ICU case convert are different
17 // of the below five
18 static const std::array<const char*, 5> kCaseLocales = {{
19     "en",  // root
20     "el",  // Greek
21     "tr",  // Turkish
22     "lt",  // Lithuanian
23     "nl",  // Dutch
24 }};
25 
26 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)27 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
28   UErrorCode status = U_ZERO_ERROR;
29   icu::UnicodeString str(UnicodeStringFromUtf32(data, size));
30 
31   auto rng = CreateRng(data, size);
32   const char* locale = kCaseLocales[rng() % kCaseLocales.size()];
33 
34   // Make the dest_size randomly fall in [0, strlen+3]
35   int32_t dest_size = (rng() % (str.length() + 3));
36   std::unique_ptr<UChar[]> dest(new UChar[dest_size]);
37 
38   switch (rng() % 2) {
39     case 0:
40       u_strToUpper(dest.get(), dest_size, (const UChar*)str.getBuffer(),
41                    str.length(), locale, &status);
42       break;
43     case 1:
44       u_strToLower(dest.get(), dest_size, (const UChar*)str.getBuffer(),
45                    str.length(), locale, &status);
46       break;
47   }
48 
49   return 0;
50 }
51