]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/fuzzer/break_iterator_fuzzer.cc
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / test / fuzzer / break_iterator_fuzzer.cc
CommitLineData
3d1f044b
A
1// © 2019 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3
4#include <stddef.h>
5#include <stdint.h>
6#include <string.h>
7#include <memory>
8#include <utility>
9#include "fuzzer_utils.h"
10#include "unicode/brkiter.h"
11#include "unicode/utext.h"
12
13IcuEnvironment* env = new IcuEnvironment();
14
15extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
16 UErrorCode status = U_ZERO_ERROR;
17 uint8_t rnd8 = 0;
18 uint16_t rnd16 = 0;
19
20 if (size < 3) {
21 return 0;
22 }
23 // Extract one and two bytes from fuzzer data for random selection purpose.
24 rnd8 = *data;
25 data++;
26 rnd16 = *(reinterpret_cast<const uint16_t *>(data));
27 data = data + 2;
28 size = size - 3;
29
30 size_t unistr_size = size/2;
31 std::unique_ptr<char16_t[]> fuzzbuff(new char16_t[unistr_size]);
32 std::memcpy(fuzzbuff.get(), data, unistr_size * 2);
33
34 UText* fuzzstr = utext_openUChars(nullptr, fuzzbuff.get(), unistr_size, &status);
35
36 const icu::Locale& locale = GetRandomLocale(rnd16);
37
38 std::unique_ptr<icu::BreakIterator> bi;
39
40 switch (rnd8 % 5) {
41 case 0:
42 bi.reset(icu::BreakIterator::createWordInstance(locale, status));
43 break;
44 case 1:
45 bi.reset(icu::BreakIterator::createLineInstance(locale, status));
46 break;
47 case 2:
48 bi.reset(icu::BreakIterator::createCharacterInstance(locale, status));
49 break;
50 case 3:
51 bi.reset(icu::BreakIterator::createSentenceInstance(locale, status));
52 break;
53 case 4:
54 bi.reset(icu::BreakIterator::createTitleInstance(locale, status));
55 break;
56 }
57
58 bi->setText(fuzzstr, status);
59
60 if (U_FAILURE(status)) {
61 utext_close(fuzzstr);
62 return 0;
63 }
64
65 for (int32_t p = bi->first(); p != icu::BreakIterator::DONE; p = bi->next()) {}
66
67 utext_close(fuzzstr);
68 return 0;
69}
70