Skip to content

Commit

Permalink
ICU-22549 Add PluralRule Fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Dec 21, 2023
1 parent ef78f2a commit 23dd2b8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions icu4c/source/test/fuzzer/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ FUZZER_TARGETS = \
normalizer2_fuzzer \
number_format_fuzzer \
number_formatter_fuzzer \
plurrule_fuzzer \
relative_date_time_formatter_fuzzer \
rule_based_break_iterator_fuzzer \
timezone_create_fuzzer \
Expand Down
82 changes: 82 additions & 0 deletions icu4c/source/test/fuzzer/plurrule_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// © 2023 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

// Fuzzer for ICU PluralRules.

#include <cstring>

#include "fuzzer_utils.h"

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


void TestPluralRules(icu::PluralRules* pp, int32_t number, double dbl, UErrorCode& status) {
if (U_FAILURE(status)) {
return;
}
pp->select(number);
pp->select(dbl);
}

void TestPluralRulesWithLocale(
const icu::Locale& locale, int32_t number, double dbl, UPluralType type, UErrorCode& status) {
if (U_FAILURE(status)) {
return;
}
std::unique_ptr<icu::PluralRules> pp(icu::PluralRules::forLocale(locale, status));
TestPluralRules(pp.get(), number, dbl, status);

status = U_ZERO_ERROR;
pp.reset(icu::PluralRules::forLocale(locale, type, status));
TestPluralRules(pp.get(), number, dbl, status);

type = static_cast<UPluralType>(
static_cast<int>(type) % (static_cast<int>(UPLURAL_TYPE_COUNT)));

status = U_ZERO_ERROR;
pp.reset(icu::PluralRules::forLocale(locale, type, status));
TestPluralRules(pp.get(), number, dbl, status);
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
uint16_t rnd;
int32_t number;
double dbl;
UPluralType type;
if (size > 5000) {
size = 5000;
}
if (size < sizeof(rnd) + sizeof(number) + sizeof(dbl) + sizeof(type)) return 0;
icu::StringPiece fuzzData(reinterpret_cast<const char *>(data), size);

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

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

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

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

size_t len = fuzzData.size() / sizeof(char16_t);
icu::UnicodeString text(false, reinterpret_cast<const char16_t*>(fuzzData.data()), len);

UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<icu::PluralRules> pp(
icu::PluralRules::createRules(text, status));
TestPluralRules(pp.get(), number, dbl, status);

status = U_ZERO_ERROR;
TestPluralRulesWithLocale(locale, number, dbl, type, status);

icu::Locale locale2 = icu::Locale(fuzzData.data());

status = U_ZERO_ERROR;
TestPluralRulesWithLocale(locale2, number, dbl, type, status);
return EXIT_SUCCESS;
}

0 comments on commit 23dd2b8

Please sign in to comment.