Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions icu4c/source/test/fuzzer/list_format_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@ void TestFormat(icu::ListFormatter* listFormat, const icu::UnicodeString* items)
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
uint16_t rnd;
UListFormatterType type;
UListFormatterWidth width;
if (size < sizeof(rnd) + sizeof(type) + sizeof(width)) return 0;
int32_t raw_type;
int32_t raw_width;
if (size < sizeof(rnd) + sizeof(raw_type) + sizeof(raw_width)) 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(&type, fuzzData.data(), sizeof(type));
fuzzData.remove_prefix(sizeof(type));
std::memcpy(&width, fuzzData.data(), sizeof(width));
fuzzData.remove_prefix(sizeof(width));
std::memcpy(&raw_type, fuzzData.data(), sizeof(raw_type));
fuzzData.remove_prefix(sizeof(raw_type));
std::memcpy(&raw_width, fuzzData.data(), sizeof(raw_width));
fuzzData.remove_prefix(sizeof(raw_width));

// Ensure non-negative before modulo.
int32_t type_index = raw_type < 0 ? -raw_type : raw_type;
int32_t width_index = raw_width < 0 ? -raw_width : raw_width;

UListFormatterType type = static_cast<UListFormatterType>(
type_index % (static_cast<int>(ULISTFMT_TYPE_UNITS) + 1));
UListFormatterWidth width = static_cast<UListFormatterWidth>(
width_index % (static_cast<int>(ULISTFMT_WIDTH_NARROW) + 1));

size_t len = fuzzData.size() / sizeof(char16_t);
icu::UnicodeString text(false, reinterpret_cast<const char16_t*>(fuzzData.data()), len);
Expand All @@ -54,16 +63,5 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
TestFormat(listFormat.get(), items);
}

status = U_ZERO_ERROR;
type = static_cast<UListFormatterType>(
static_cast<int>(type) % (static_cast<int>(ULISTFMT_TYPE_UNITS) + 1));
width = static_cast<UListFormatterWidth>(
static_cast<int>(width) % (static_cast<int>(ULISTFMT_WIDTH_NARROW) + 1));
listFormat.reset(
icu::ListFormatter::createInstance(locale, type, width, status));
if (U_SUCCESS(status)) {
TestFormat(listFormat.get(), items);
}

return EXIT_SUCCESS;
}