1 // © 2019 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 // Fuzzer for NumberFormat::parse.
5
6 #include <cstring>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string>
10 #include <memory>
11 #include "fuzzer_utils.h"
12 #include "unicode/numfmt.h"
13
14 IcuEnvironment* env = new IcuEnvironment();
15
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)16 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
17 UErrorCode status = U_ZERO_ERROR;
18 uint16_t rnd = 0;
19
20 if (size < 2) {
21 return 0;
22 }
23
24 rnd = *(reinterpret_cast<const uint16_t *>(data));
25 data = data + 2;
26 size = size - 2;
27
28 size_t unistr_size = size/2;
29 std::unique_ptr<char16_t[]> fuzzbuff(new char16_t[unistr_size]);
30 std::memcpy(fuzzbuff.get(), data, unistr_size * 2);
31
32 const icu::Locale& locale = GetRandomLocale(rnd);
33
34 std::unique_ptr<icu::NumberFormat> fmt(
35 icu::NumberFormat::createInstance(locale, status));
36 if (U_FAILURE(status)) {
37 return 0;
38 }
39
40 icu::UnicodeString fuzzstr(false, fuzzbuff.get(), unistr_size);
41 icu::Formattable result;
42 fmt->parse(fuzzstr, result, status);
43
44 return 0;
45 }
46