File: locale_fuzzer.cpp

package info (click to toggle)
icu 76.1-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 121,296 kB
  • sloc: cpp: 522,712; ansic: 113,387; sh: 4,983; makefile: 4,709; perl: 3,198; python: 2,847; xml: 2,652; sed: 36; lisp: 12
file content (49 lines) | stat: -rw-r--r-- 1,409 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

// Fuzzer for ICU Locales.

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <string>
#include <vector>

#include "unicode/locid.h"

namespace {

void ConsumeNBytes(const uint8_t** data, size_t* size, size_t N) {
  *data += N;
  *size -= N;
}

uint8_t ConsumeUint8(const uint8_t** data, size_t* size) {
  uint8_t tmp = 0;
  if (*size >= 1) {
    tmp = (*data)[0];
    ConsumeNBytes(data, size, 1);
  }
  return tmp;
}

std::string ConsumeSubstring(const uint8_t** data, size_t* size) {
  const size_t request_size = ConsumeUint8(data, size);
  const char* substring_start = reinterpret_cast<const char*>(*data);
  const size_t substring_size = std::min(*size, request_size);
  ConsumeNBytes(data, size, substring_size);
  return std::string(substring_start, substring_size);
}

}  // namespace

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  const std::string language = ConsumeSubstring(&data, &size);
  const std::string country = ConsumeSubstring(&data, &size);
  const std::string variant = ConsumeSubstring(&data, &size);
  const std::string kv_pairs = ConsumeSubstring(&data, &size);
  icu::Locale locale(language.c_str(), country.c_str(), variant.c_str(),
                     kv_pairs.c_str());
  return EXIT_SUCCESS;
}