/src/icu/icu4c/source/test/fuzzer/locale_fuzzer.cpp
Line | Count | Source |
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 | | |
17 | 17.9k | void ConsumeNBytes(const uint8_t** data, size_t* size, size_t N) { |
18 | 17.9k | *data += N; |
19 | 17.9k | *size -= N; |
20 | 17.9k | } |
21 | | |
22 | 13.4k | uint8_t ConsumeUint8(const uint8_t** data, size_t* size) { |
23 | 13.4k | uint8_t tmp = 0; |
24 | 13.4k | if (*size >= 1) { |
25 | 4.51k | tmp = (*data)[0]; |
26 | 4.51k | ConsumeNBytes(data, size, 1); |
27 | 4.51k | } |
28 | 13.4k | return tmp; |
29 | 13.4k | } |
30 | | |
31 | 13.4k | std::string ConsumeSubstring(const uint8_t** data, size_t* size) { |
32 | 13.4k | const size_t request_size = ConsumeUint8(data, size); |
33 | 13.4k | const char* substring_start = reinterpret_cast<const char*>(*data); |
34 | 13.4k | const size_t substring_size = std::min(*size, request_size); |
35 | 13.4k | ConsumeNBytes(data, size, substring_size); |
36 | 13.4k | return std::string(substring_start, substring_size); |
37 | 13.4k | } |
38 | | |
39 | | } // namespace |
40 | | |
41 | 42.8k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
42 | 42.8k | const std::string language = ConsumeSubstring(&data, &size); |
43 | 42.8k | const std::string country = ConsumeSubstring(&data, &size); |
44 | 42.8k | const std::string variant = ConsumeSubstring(&data, &size); |
45 | 42.8k | const std::string kv_pairs = ConsumeSubstring(&data, &size); |
46 | 42.8k | icu::Locale locale(language.c_str(), country.c_str(), variant.c_str(), |
47 | 42.8k | kv_pairs.c_str()); |
48 | 42.8k | return EXIT_SUCCESS; |
49 | 42.8k | } |