ICU-22549 Improve fuzzer to test more locale

We found bogus locale cause crash in DateFormat so here
we enhance the fuzzer to also test locale name which are not
return by the available locale list.
This commit is contained in:
Frank Tang 2023-12-15 17:40:42 -08:00 committed by Frank Yung-Fong Tang
parent 4ba5d9191b
commit 11d1148e56
4 changed files with 60 additions and 28 deletions

View file

@ -66,6 +66,26 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
for (int32_t p = bi->first(); p != icu::BreakIterator::DONE; p = bi->next()) {}
utext_close(fuzzstr);
icu::Locale locale2(reinterpret_cast<const char*>(data));
switch (rnd8 % 5) {
case 0:
bi.reset(icu::BreakIterator::createWordInstance(locale2, status));
break;
case 1:
bi.reset(icu::BreakIterator::createLineInstance(locale2, status));
break;
case 2:
bi.reset(icu::BreakIterator::createCharacterInstance(locale2, status));
break;
case 3:
bi.reset(icu::BreakIterator::createSentenceInstance(locale2, status));
break;
case 4:
bi.reset(icu::BreakIterator::createTitleInstance(locale2, status));
break;
}
return 0;
}

View file

@ -37,22 +37,25 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size > 4096) {
size = 4096;
}
std::unique_ptr<char16_t> compbuff1(new char16_t[size/4]);
std::unique_ptr<char16_t[]> compbuff1(new char16_t[size/4]);
std::memcpy(compbuff1.get(), data, (size/4)*2);
data = data + size/2;
std::unique_ptr<char16_t> compbuff2(new char16_t[size/4]);
std::unique_ptr<char16_t[]> compbuff2(new char16_t[size/4]);
std::memcpy(compbuff2.get(), data, (size/4)*2);
icu::LocalPointer<icu::Collator> fuzzCollator(
icu::Collator::createInstance(locale, status), status);
if (U_FAILURE(status))
return 0;
if (U_SUCCESS(status)) {
fuzzCollator->setStrength(strength);
fuzzCollator->setStrength(strength);
fuzzCollator->compare(compbuff1.get(), size/4,
compbuff2.get(), size/4);
fuzzCollator->compare(compbuff1.get(), size/4,
compbuff2.get(), size/4);
}
status = U_ZERO_ERROR;
fuzzCollator.adoptInstead(
icu::Collator::createInstance(icu::Locale(reinterpret_cast<const char*>(data)), status));
return 0;
}

View file

@ -83,5 +83,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (U_SUCCESS(status)) {
df->format(date, appendTo);
}
icu::Locale locale2(fuzzData.data());
df.reset(
icu::DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale2));
df.reset(
icu::DateFormat::createDateTimeInstance(dateStyle2, timeStyle2, locale2));
return EXIT_SUCCESS;
}

View file

@ -29,28 +29,31 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
icu::LocalPointer<icu::DateTimePatternGenerator > gen(
icu::DateTimePatternGenerator::createInstance(locale, status), status);
if (U_FAILURE(status))
return 0;
if (U_SUCCESS(status)) {
status = U_ZERO_ERROR;
gen->getSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
gen->getBaseSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
gen->getBaseSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
gen->getPatternForSkeleton(fuzzstr);
status = U_ZERO_ERROR;
gen->getBestPattern(fuzzstr, status);
status = U_ZERO_ERROR;
icu::DateTimePatternGenerator::staticGetSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
icu::DateTimePatternGenerator::staticGetBaseSkeleton (fuzzstr, status);
}
status = U_ZERO_ERROR;
gen->getSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
gen->getBaseSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
gen->getBaseSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
gen->getPatternForSkeleton(fuzzstr);
status = U_ZERO_ERROR;
gen->getBestPattern(fuzzstr, status);
status = U_ZERO_ERROR;
icu::DateTimePatternGenerator::staticGetSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
icu::DateTimePatternGenerator::staticGetBaseSkeleton (fuzzstr, status);
gen.adoptInstead(icu::DateTimePatternGenerator::createInstance(icu::Locale(reinterpret_cast<const char*>(data)), status));
return 0;
}