1 // © 2019 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 // Fuzzer for ICU Locales.
5
6 #include <algorithm>
7 #include <cstddef>
8 #include <cstdint>
9 #include <cstdlib>
10 #include <string>
11 #include <vector>
12
13 #include "unicode/locid.h"
14
15 namespace {
16
ConsumeNBytes(const uint8_t ** data,size_t * size,size_t N)17 void ConsumeNBytes(const uint8_t** data, size_t* size, size_t N) {
18 *data += N;
19 *size -= N;
20 }
21
ConsumeUint8(const uint8_t ** data,size_t * size)22 uint8_t ConsumeUint8(const uint8_t** data, size_t* size) {
23 uint8_t tmp = 0;
24 if (*size >= 1) {
25 tmp = (*data)[0];
26 ConsumeNBytes(data, size, 1);
27 }
28 return tmp;
29 }
30
ConsumeSubstring(const uint8_t ** data,size_t * size)31 std::string ConsumeSubstring(const uint8_t** data, size_t* size) {
32 const size_t request_size = ConsumeUint8(data, size);
33 const char* substring_start = reinterpret_cast<const char*>(*data);
34 const size_t substring_size = std::min(*size, request_size);
35 ConsumeNBytes(data, size, substring_size);
36 return std::string(substring_start, substring_size);
37 }
38
39 } // namespace
40
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)41 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
42 const std::string language = ConsumeSubstring(&data, &size);
43 const std::string country = ConsumeSubstring(&data, &size);
44 const std::string variant = ConsumeSubstring(&data, &size);
45 const std::string kv_pairs = ConsumeSubstring(&data, &size);
46 icu::Locale locale(language.c_str(), country.c_str(), variant.c_str(),
47 kv_pairs.c_str());
48 return EXIT_SUCCESS;
49 }
50