From d082de574ff13873123c55212ad20cf258ffb283 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Fri, 15 Sep 2023 16:16:32 -0700 Subject: [PATCH] ICU-22479 Add Fuzzer for DateFormat --- icu4c/source/test/fuzzer/Makefile.in | 2 +- .../source/test/fuzzer/date_format_fuzzer.cpp | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 icu4c/source/test/fuzzer/date_format_fuzzer.cpp diff --git a/icu4c/source/test/fuzzer/Makefile.in b/icu4c/source/test/fuzzer/Makefile.in index da22b464163..9c35674ba75 100644 --- a/icu4c/source/test/fuzzer/Makefile.in +++ b/icu4c/source/test/fuzzer/Makefile.in @@ -33,7 +33,7 @@ CPPFLAGS += -I$(srcdir) -I$(top_srcdir)/common -I$(top_srcdir)/i18n -I$(top_srcd DEFS += -D'U_TOPSRCDIR="$(top_srcdir)/"' -D'U_TOPBUILDDIR="$(BUILDDIR)"' LIBS = $(LIBCTESTFW) $(LIBICUTOOLUTIL) $(LIBICUIO) $(LIBICUI18N) $(LIBICUUC) $(DEFAULT_LIBS) $(LIB_M) -FUZZER_TARGETS = break_iterator_fuzzer calendar_fuzzer collator_compare_fuzzer collator_rulebased_fuzzer converter_fuzzer locale_fuzzer locale_morph_fuzzer number_format_fuzzer ucasemap_fuzzer uloc_canonicalize_fuzzer uloc_for_language_tag_fuzzer uloc_get_name_fuzzer uloc_is_right_to_left_fuzzer uloc_open_keywords_fuzzer unicode_string_codepage_create_fuzzer uregex_open_fuzzer +FUZZER_TARGETS = break_iterator_fuzzer calendar_fuzzer collator_compare_fuzzer collator_rulebased_fuzzer converter_fuzzer date_format_fuzzer locale_fuzzer locale_morph_fuzzer number_format_fuzzer ucasemap_fuzzer uloc_canonicalize_fuzzer uloc_for_language_tag_fuzzer uloc_get_name_fuzzer uloc_is_right_to_left_fuzzer uloc_open_keywords_fuzzer unicode_string_codepage_create_fuzzer uregex_open_fuzzer OBJECTS = $(FUZZER_TARGETS:%=%.o) OBJECTS += fuzzer_driver.o locale_util.o diff --git a/icu4c/source/test/fuzzer/date_format_fuzzer.cpp b/icu4c/source/test/fuzzer/date_format_fuzzer.cpp new file mode 100644 index 00000000000..4578e82f7a3 --- /dev/null +++ b/icu4c/source/test/fuzzer/date_format_fuzzer.cpp @@ -0,0 +1,52 @@ +// © 2023 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html + +// Fuzzer for ICU Calendar. + +#include + +#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; + UDate date; + icu::DateFormat::EStyle dateStyle; + icu::DateFormat::EStyle timeStyle; + if (size < sizeof(rnd) + sizeof(date) + sizeof(dateStyle) + sizeof(timeStyle)) return 0; + icu::StringPiece fuzzData(reinterpret_cast(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(&date, fuzzData.data(), sizeof(date)); + fuzzData.remove_prefix(sizeof(date)); + + std::unique_ptr df( + icu::DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale)); + icu::UnicodeString appendTo; + 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); + } + return EXIT_SUCCESS; +}