Skip to content

Commit

Permalink
ICU-22549 Reland PR2770 w/ fix
Browse files Browse the repository at this point in the history
Reland PR2770 w/ the fix that null termination the input to Locale
constuctor.
  • Loading branch information
FrankYFTang committed Jan 5, 2024
1 parent 539e8f4 commit cfba9a8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
21 changes: 21 additions & 0 deletions icu4c/source/test/fuzzer/break_iterator_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ 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);

std::string str(reinterpret_cast<const char*>(data), size);
icu::Locale locale2(str.c_str()); // ensure null-termination by c_str()
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;
}

18 changes: 11 additions & 7 deletions icu4c/source/test/fuzzer/collator_compare_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,26 @@ 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;

std::string str(reinterpret_cast<const char*>(data), size);
fuzzCollator.adoptInstead(
icu::Collator::createInstance(icu::Locale(str.c_str()), status));
return 0;
}
7 changes: 7 additions & 0 deletions icu4c/source/test/fuzzer/date_format_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,12 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
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;
}
34 changes: 19 additions & 15 deletions icu4c/source/test/fuzzer/date_time_pattern_generator_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,32 @@ 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->getSkeleton(fuzzstr, status);

status = U_ZERO_ERROR;
gen->getBaseSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
gen->getBaseSkeleton(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->getPatternForSkeleton(fuzzstr);

status = U_ZERO_ERROR;
gen->getBestPattern(fuzzstr, status);
status = U_ZERO_ERROR;
gen->getBestPattern(fuzzstr, status);

status = U_ZERO_ERROR;
icu::DateTimePatternGenerator::staticGetSkeleton(fuzzstr, status);
status = U_ZERO_ERROR;
icu::DateTimePatternGenerator::staticGetSkeleton(fuzzstr, status);

status = U_ZERO_ERROR;
icu::DateTimePatternGenerator::staticGetBaseSkeleton (fuzzstr, status);
}

status = U_ZERO_ERROR;
icu::DateTimePatternGenerator::staticGetBaseSkeleton (fuzzstr, status);
std::string str(reinterpret_cast<const char*>(data), size);
gen.adoptInstead(icu::DateTimePatternGenerator::createInstance(icu::Locale(str.c_str()), status));
return 0;
}

0 comments on commit cfba9a8

Please sign in to comment.