Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICU-22479 Improve collator_compare_fuzzer #2574

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
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
25 changes: 22 additions & 3 deletions icu4c/source/test/fuzzer/collator_compare_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#include <array>
#include <cstring>

#include "fuzzer_utils.h"
Expand All @@ -10,23 +11,41 @@

IcuEnvironment* env = new IcuEnvironment();

static const std::array<icu::Collator::ECollationStrength, 5> kStrength = {
icu::Collator::PRIMARY,
icu::Collator::SECONDARY,
icu::Collator::TERTIARY,
icu::Collator::QUATERNARY,
icu::Collator::IDENTICAL
};

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
UErrorCode status = U_ZERO_ERROR;

if (size < 2)
uint16_t rnd16;

if (size < 2 + sizeof(rnd16))
return 0;

std::memcpy(&rnd16, data, sizeof(rnd16));
size -= sizeof(rnd16);
data += sizeof(rnd16);
icu::Collator::ECollationStrength strength = kStrength[rnd16 % kStrength.size()];
const icu::Locale& locale = GetRandomLocale(rnd16 / kStrength.size());

std::unique_ptr<char16_t> compbuff1(new char16_t[size/4]);
std::memcpy(compbuff1.get(), data, (size/4)*2);
data = data + size/2;
markusicu marked this conversation as resolved.
Show resolved Hide resolved
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(icu::Locale::getUS(), status), status);
icu::Collator::createInstance(locale, status), status);
if (U_FAILURE(status))
return 0;
fuzzCollator->setStrength(icu::Collator::TERTIARY);

fuzzCollator->setStrength(strength);

fuzzCollator->compare(compbuff1.get(), size/4,
compbuff2.get(), size/4);
Expand Down
Binary file not shown.
Loading