File: date_format_fuzzer.cpp

package info (click to toggle)
icu 76.1-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 121,296 kB
  • sloc: cpp: 522,712; ansic: 113,387; sh: 4,983; makefile: 4,709; perl: 3,198; python: 2,847; xml: 2,652; sed: 36; lisp: 12
file content (94 lines) | stat: -rw-r--r-- 3,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// © 2023 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

// Fuzzer for ICU Calendar.

#include <cstring>

#include "fuzzer_utils.h"

#include "unicode/datefmt.h"
#include "unicode/locid.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    uint16_t rnd;
    uint8_t rnd2;
    UDate date;
    icu::DateFormat::EStyle styles[] = {
        icu::DateFormat::EStyle::kNone,
        icu::DateFormat::EStyle::kFull,
        icu::DateFormat::EStyle::kLong,
        icu::DateFormat::EStyle::kMedium,
        icu::DateFormat::EStyle::kShort,
        icu::DateFormat::EStyle::kDateOffset,
        icu::DateFormat::EStyle::kDateTime,
        icu::DateFormat::EStyle::kDateTimeOffset,
        icu::DateFormat::EStyle::kRelative,
        icu::DateFormat::EStyle::kFullRelative,
        icu::DateFormat::EStyle::kLongRelative,
        icu::DateFormat::EStyle::kMediumRelative,
        icu::DateFormat::EStyle::kShortRelative,
    };
    int32_t numStyles = sizeof(styles) / sizeof(icu::DateFormat::EStyle);

    icu::DateFormat::EStyle dateStyle;
    icu::DateFormat::EStyle timeStyle;
    if (size < sizeof(rnd) + sizeof(date) + 2*sizeof(rnd2) +
        sizeof(dateStyle) + sizeof(timeStyle) ) {
        return 0;
    }
    icu::StringPiece fuzzData(reinterpret_cast<const char *>(data), size);

    std::memcpy(&rnd, fuzzData.data(), sizeof(rnd));
    fuzzData.remove_prefix(sizeof(rnd));
    icu::Locale locale = GetRandomLocale(rnd);

    std::memcpy(&dateStyle, fuzzData.data(), sizeof(dateStyle));
    fuzzData.remove_prefix(sizeof(dateStyle));
    std::memcpy(&timeStyle, fuzzData.data(), sizeof(timeStyle));
    fuzzData.remove_prefix(sizeof(timeStyle));

    std::memcpy(&rnd2, fuzzData.data(), sizeof(rnd2));
    icu::DateFormat::EStyle dateStyle2 = styles[rnd2 % numStyles];
    fuzzData.remove_prefix(sizeof(rnd2));

    std::memcpy(&rnd2, fuzzData.data(), sizeof(rnd2));
    icu::DateFormat::EStyle timeStyle2 = styles[rnd2 % numStyles];
    fuzzData.remove_prefix(sizeof(rnd2));

    std::memcpy(&date, fuzzData.data(), sizeof(date));
    fuzzData.remove_prefix(sizeof(date));

    std::unique_ptr<icu::DateFormat> df(
        icu::DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale));
    icu::UnicodeString appendTo;
    df->format(date, appendTo);

    df.reset(
        icu::DateFormat::createDateTimeInstance(dateStyle2, timeStyle2, locale));
    appendTo.remove();
    df->format(date, appendTo);
    icu::UnicodeString skeleton = icu::UnicodeString::fromUTF8(fuzzData);

    UErrorCode status = U_ZERO_ERROR;
    appendTo.remove();
    df.reset(icu::DateFormat::createInstanceForSkeleton(skeleton, status));
    if (U_SUCCESS(status)) {
        df->format(date, appendTo);
    }

    status = U_ZERO_ERROR;
    appendTo.remove();
    df.reset(icu::DateFormat::createInstanceForSkeleton(skeleton, locale, status));
    if (U_SUCCESS(status)) {
        df->format(date, appendTo);
    }

    std::string str(fuzzData.data(), fuzzData.size());
    icu::Locale locale2(str.c_str());
    df.reset(
        icu::DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale2));
    df.reset(
        icu::DateFormat::createDateTimeInstance(dateStyle2, timeStyle2, locale2));
    return EXIT_SUCCESS;
}