Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/tools/llvm-yaml-numeric-parser-fuzzer/yaml-numeric-parser-fuzzer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- yaml-numeric-parser-fuzzer.cpp - Fuzzer for YAML numeric parser ---===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/ADT/StringRef.h"
10
#include "llvm/Support/Regex.h"
11
#include "llvm/Support/YAMLTraits.h"
12
#include <string>
13
14
577
inline bool isNumericRegex(llvm::StringRef S) {
15
577
  static llvm::Regex Infinity("^[-+]?(\\.inf|\\.Inf|\\.INF)$");
16
577
  static llvm::Regex Base8("^0o[0-7]+$");
17
577
  static llvm::Regex Base16("^0x[0-9a-fA-F]+$");
18
577
  static llvm::Regex Float(
19
577
      "^[-+]?(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?$");
20
21
577
  if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN"))
22
3
    return true;
23
24
574
  if (Infinity.match(S))
25
4
    return true;
26
27
570
  if (Base8.match(S))
28
2
    return true;
29
30
568
  if (Base16.match(S))
31
4
    return true;
32
33
564
  if (Float.match(S))
34
59
    return true;
35
36
505
  return false;
37
564
}
38
39
587
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
40
587
  std::string Input(reinterpret_cast<const char *>(Data), Size);
41
587
  llvm::erase(Input, 0);
42
587
  if (!Input.empty() && llvm::yaml::isNumeric(Input) != isNumericRegex(Input))
43
0
    LLVM_BUILTIN_TRAP;
44
587
  return 0;
45
587
}