Coverage Report

Created: 2026-03-31 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/test/fuzzer/locale_fuzzer.cpp
Line
Count
Source
1
// © 2019 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
// Fuzzer for ICU Locales.
5
6
#include <algorithm>
7
#include <cstddef>
8
#include <cstdint>
9
#include <cstdlib>
10
#include <string>
11
#include <vector>
12
13
#include "unicode/locid.h"
14
15
namespace {
16
17
17.1k
void ConsumeNBytes(const uint8_t** data, size_t* size, size_t N) {
18
17.1k
  *data += N;
19
17.1k
  *size -= N;
20
17.1k
}
21
22
12.8k
uint8_t ConsumeUint8(const uint8_t** data, size_t* size) {
23
12.8k
  uint8_t tmp = 0;
24
12.8k
  if (*size >= 1) {
25
4.35k
    tmp = (*data)[0];
26
4.35k
    ConsumeNBytes(data, size, 1);
27
4.35k
  }
28
12.8k
  return tmp;
29
12.8k
}
30
31
12.8k
std::string ConsumeSubstring(const uint8_t** data, size_t* size) {
32
12.8k
  const size_t request_size = ConsumeUint8(data, size);
33
12.8k
  const char* substring_start = reinterpret_cast<const char*>(*data);
34
12.8k
  const size_t substring_size = std::min(*size, request_size);
35
12.8k
  ConsumeNBytes(data, size, substring_size);
36
12.8k
  return std::string(substring_start, substring_size);
37
12.8k
}
38
39
}  // namespace
40
41
40.9k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
42
40.9k
  const std::string language = ConsumeSubstring(&data, &size);
43
40.9k
  const std::string country = ConsumeSubstring(&data, &size);
44
40.9k
  const std::string variant = ConsumeSubstring(&data, &size);
45
40.9k
  const std::string kv_pairs = ConsumeSubstring(&data, &size);
46
40.9k
  icu::Locale locale(language.c_str(), country.c_str(), variant.c_str(),
47
40.9k
                     kv_pairs.c_str());
48
  return EXIT_SUCCESS;
49
40.9k
}