/src/icu/icu4c/source/test/fuzzer/dtfmtsym_fuzzer.cpp
Line | Count | Source |
1 | | // © 2023 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | |
4 | | // Fuzzer for ICU DateFormatSymbols. |
5 | | |
6 | | #include <cstring> |
7 | | |
8 | | #include "fuzzer_utils.h" |
9 | | |
10 | | #include "unicode/dtfmtsym.h" |
11 | | #include "unicode/locid.h" |
12 | | |
13 | 6.54k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
14 | 6.54k | uint16_t rnd; |
15 | 6.54k | icu::DateFormatSymbols::DtContextType context; |
16 | 6.54k | icu::DateFormatSymbols::DtWidthType width; |
17 | 6.54k | if (size < sizeof(rnd) + sizeof(context) + sizeof(width)) return 0; |
18 | 6.54k | icu::StringPiece fuzzData(reinterpret_cast<const char *>(data), size); |
19 | | |
20 | 6.54k | std::memcpy(&rnd, fuzzData.data(), sizeof(rnd)); |
21 | 6.54k | icu::Locale locale = GetRandomLocale(rnd); |
22 | 6.54k | fuzzData.remove_prefix(sizeof(rnd)); |
23 | | |
24 | 6.54k | std::memcpy(&context, fuzzData.data(), sizeof(context)); |
25 | 6.54k | fuzzData.remove_prefix(sizeof(context)); |
26 | 6.54k | icu::DateFormatSymbols::DtContextType context_mod = |
27 | 6.54k | static_cast<icu::DateFormatSymbols::DtContextType>( |
28 | 6.54k | context % icu::DateFormatSymbols::DtContextType::DT_CONTEXT_COUNT); |
29 | 6.54k | ; |
30 | 6.54k | std::memcpy(&width, fuzzData.data(), sizeof(width)); |
31 | 6.54k | fuzzData.remove_prefix(sizeof(width)); |
32 | 6.54k | icu::DateFormatSymbols::DtWidthType width_mod = |
33 | 6.54k | static_cast<icu::DateFormatSymbols::DtWidthType>( |
34 | 6.54k | width % icu::DateFormatSymbols::DtWidthType::DT_WIDTH_COUNT); |
35 | | |
36 | 6.54k | size_t len = fuzzData.size() / sizeof(char16_t); |
37 | 6.54k | icu::UnicodeString text(false, reinterpret_cast<const char16_t*>(fuzzData.data()), len); |
38 | 6.54k | const icu::UnicodeString items[] = { text, text, text, text }; |
39 | | |
40 | 6.54k | UErrorCode status = U_ZERO_ERROR; |
41 | 6.54k | std::unique_ptr<icu::DateFormatSymbols> dfs( |
42 | 6.54k | new icu::DateFormatSymbols(locale, status)); |
43 | 6.54k | if (U_FAILURE(status)) { |
44 | 0 | return EXIT_SUCCESS; |
45 | 0 | } |
46 | | |
47 | 6.54k | int32_t count; |
48 | 6.54k | dfs->getEras(count); |
49 | 6.54k | dfs->getEraNames(count); |
50 | 6.54k | dfs->getNarrowEras(count); |
51 | 6.54k | dfs->getMonths(count); |
52 | 6.54k | dfs->getShortMonths(count); |
53 | 6.54k | dfs->getMonths(count, context, width); |
54 | 6.54k | dfs->getMonths(count, context_mod, width_mod); |
55 | 6.54k | dfs->getWeekdays(count); |
56 | 6.54k | dfs->getShortWeekdays(count); |
57 | 6.54k | dfs->getWeekdays(count, context, width); |
58 | 6.54k | dfs->getWeekdays(count, context_mod, width_mod); |
59 | 6.54k | dfs->getShortWeekdays(count); |
60 | 6.54k | dfs->getQuarters(count, context_mod, width_mod); |
61 | 6.54k | dfs->getAmPmStrings(count); |
62 | | |
63 | 6.54k | icu::UnicodeString output; |
64 | 6.54k | dfs->getTimeSeparatorString(output); |
65 | 6.54k | dfs->getYearNames(count, context, width); |
66 | 6.54k | dfs->getYearNames(count, context_mod, width_mod); |
67 | 6.54k | dfs->getZodiacNames(count, context, width); |
68 | 6.54k | dfs->getZodiacNames(count, context_mod, width_mod); |
69 | 6.54k | dfs->getLeapMonthPatterns(count); |
70 | 6.54k | int32_t count2; |
71 | 6.54k | dfs->getZoneStrings(count, count2); |
72 | 6.54k | dfs->getLocalPatternChars(output); |
73 | | |
74 | | return EXIT_SUCCESS; |
75 | 6.54k | } |