xref: /llvm-project/llvm/tools/llvm-yaml-numeric-parser-fuzzer/yaml-numeric-parser-fuzzer.cpp (revision 5f26a642e61e6245862e62861c7b4e75a00a372b)
1 //===--- special-case-list-fuzzer.cpp - Fuzzer for special case lists -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/Support/Regex.h"
12 #include "llvm/Support/YAMLTraits.h"
13 #include <cassert>
14 #include <string>
15 
16 llvm::Regex Infinity("^[-+]?(\\.inf|\\.Inf|\\.INF)$");
17 llvm::Regex Base8("^0o[0-7]+$");
18 llvm::Regex Base16("^0x[0-9a-fA-F]+$");
19 llvm::Regex Float("^[-+]?(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?$");
20 
21 inline bool isNumericRegex(llvm::StringRef S) {
22 
23   if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN"))
24     return true;
25 
26   if (Infinity.match(S))
27     return true;
28 
29   if (Base8.match(S))
30     return true;
31 
32   if (Base16.match(S))
33     return true;
34 
35   if (Float.match(S))
36     return true;
37 
38   return false;
39 }
40 
41 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
42   std::string Input(reinterpret_cast<const char *>(Data), Size);
43   Input.erase(std::remove(Input.begin(), Input.end(), 0), Input.end());
44   if (!Input.empty() && llvm::yaml::isNumeric(Input) != isNumericRegex(Input))
45     __builtin_trap();
46   return 0;
47 }
48