Skip to content

Commit

Permalink
ICU-22479 Improve collator_compare_fuzzer
Browse files Browse the repository at this point in the history
See #2574
  • Loading branch information
FrankYFTang committed Sep 2, 2023
1 parent d501225 commit 869713b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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;
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.

0 comments on commit 869713b

Please sign in to comment.