• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2023 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 // Fuzzer for TimeZoneNames.
5 
6 #include <cstring>
7 
8 #include "fuzzer_utils.h"
9 
10 #include "unicode/tznames.h"
11 #include "unicode/locid.h"
12 
TestNames(icu::TimeZoneNames * names,const icu::UnicodeString & text,UDate date,UTimeZoneNameType type)13 void TestNames(icu::TimeZoneNames* names, const icu::UnicodeString& text, UDate date, UTimeZoneNameType type) {
14     UErrorCode status = U_ZERO_ERROR;
15     std::unique_ptr<icu::StringEnumeration> enumeration(
16         names->getAvailableMetaZoneIDs(status));
17     status = U_ZERO_ERROR;
18     enumeration.reset(
19         names->getAvailableMetaZoneIDs(text, status));
20     icu::UnicodeString output;
21     names->getMetaZoneID(text, date, output);
22     names->getMetaZoneDisplayName(text, type, output);
23     names->getTimeZoneDisplayName(text, type, output);
24     names->getExemplarLocationName(text, output);
25     names->getDisplayName(text, type, date, output);
26 }
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)27 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
28     uint16_t rnd;
29     UDate date;
30 
31     UTimeZoneNameType type;
32     if (size < sizeof(rnd) + sizeof(date) + sizeof(type)) return 0;
33     icu::StringPiece fuzzData(reinterpret_cast<const char *>(data), size);
34 
35     std::memcpy(&rnd, fuzzData.data(), sizeof(rnd));
36     icu::Locale locale = GetRandomLocale(rnd);
37     fuzzData.remove_prefix(sizeof(rnd));
38 
39     std::memcpy(&date, fuzzData.data(), sizeof(date));
40     fuzzData.remove_prefix(sizeof(date));
41 
42     std::memcpy(&type, fuzzData.data(), sizeof(type));
43     fuzzData.remove_prefix(sizeof(type));
44 
45     size_t len = fuzzData.size() / sizeof(char16_t);
46     icu::UnicodeString text(false, reinterpret_cast<const char16_t*>(fuzzData.data()), len);
47 
48     UErrorCode status = U_ZERO_ERROR;
49     std::unique_ptr<icu::TimeZoneNames> names(
50         icu::TimeZoneNames::createInstance(locale, status));
51     if (U_SUCCESS(status)) {
52         TestNames(names.get(), text, date, type);
53     }
54 
55     status = U_ZERO_ERROR;
56     names.reset(
57         icu::TimeZoneNames::createTZDBInstance(locale, status));
58     if (U_SUCCESS(status)) {
59         TestNames(names.get(), text, date, type);
60     }
61 
62     return EXIT_SUCCESS;
63 }
64