Fangrui Song | 990061b | 2019-02-21 07:57:14 +0000 | [diff] [blame] | 1 | //===-- yaml-numeric-parser-fuzzer.cpp - Fuzzer for YAML numeric parser ---===// |
Kirill Bobyrev | 5f26a64 | 2018-08-20 07:00:36 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Kirill Bobyrev | 5f26a64 | 2018-08-20 07:00:36 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ADT/StringRef.h" |
| 10 | #include "llvm/Support/Regex.h" |
| 11 | #include "llvm/Support/YAMLTraits.h" |
| 12 | #include <cassert> |
| 13 | #include <string> |
| 14 | |
| 15 | llvm::Regex Infinity("^[-+]?(\\.inf|\\.Inf|\\.INF)$"); |
| 16 | llvm::Regex Base8("^0o[0-7]+$"); |
| 17 | llvm::Regex Base16("^0x[0-9a-fA-F]+$"); |
| 18 | llvm::Regex Float("^[-+]?(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?$"); |
| 19 | |
| 20 | inline bool isNumericRegex(llvm::StringRef S) { |
| 21 | |
| 22 | if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN")) |
| 23 | return true; |
| 24 | |
| 25 | if (Infinity.match(S)) |
| 26 | return true; |
| 27 | |
| 28 | if (Base8.match(S)) |
| 29 | return true; |
| 30 | |
| 31 | if (Base16.match(S)) |
| 32 | return true; |
| 33 | |
| 34 | if (Float.match(S)) |
| 35 | return true; |
| 36 | |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
| 41 | std::string Input(reinterpret_cast<const char *>(Data), Size); |
| 42 | Input.erase(std::remove(Input.begin(), Input.end(), 0), Input.end()); |
| 43 | if (!Input.empty() && llvm::yaml::isNumeric(Input) != isNumericRegex(Input)) |
Simon Pilgrim | bbd2d15 | 2018-08-20 09:49:20 +0000 | [diff] [blame] | 44 | LLVM_BUILTIN_TRAP; |
Kirill Bobyrev | 5f26a64 | 2018-08-20 07:00:36 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |