• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2019 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include <cstring>
5 
6 #include "fuzzer_utils.h"
7 #include "unicode/localpointer.h"
8 #include "unicode/numberformatter.h"
9 
10 IcuEnvironment* env = new IcuEnvironment();
11 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)12 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
13   UErrorCode status = U_ZERO_ERROR;
14 
15   int16_t rnd;
16   int64_t value;
17   double doubleValue;
18   if (size < sizeof(rnd) + sizeof(value) + sizeof(doubleValue)) return 0;
19   icu::StringPiece fuzzData(reinterpret_cast<const char *>(data), size);
20 
21   std::memcpy(&rnd, fuzzData.data(), sizeof(rnd));
22   icu::Locale locale = GetRandomLocale(rnd);
23   fuzzData.remove_prefix(sizeof(rnd));
24 
25   std::memcpy(&value, fuzzData.data(), sizeof(value));
26   fuzzData.remove_prefix(sizeof(value));
27 
28   std::memcpy(&doubleValue, fuzzData.data(), sizeof(doubleValue));
29   fuzzData.remove_prefix(sizeof(doubleValue));
30 
31   size_t len = fuzzData.size() / sizeof(char16_t);
32   icu::UnicodeString fuzzstr(false, reinterpret_cast<const char16_t*>(fuzzData.data()), len);
33 
34   icu::number::UnlocalizedNumberFormatter unf = icu::number::NumberFormatter::forSkeleton(
35       fuzzstr, status);
36 
37   icu::number::LocalizedNumberFormatter nf = unf.locale(locale);
38 
39   status = U_ZERO_ERROR;
40   nf.formatInt(value, status);
41 
42   status = U_ZERO_ERROR;
43   nf.formatDouble(doubleValue, status);
44   return 0;
45 }
46