Merge pull request #184 from silvergasp/main

Adds a simple fuzz test
This commit is contained in:
Daniel Lemire 2023-03-12 20:50:58 -04:00 committed by GitHub
commit 8cb0590c02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

8
fuzz/build.sh Normal file
View File

@ -0,0 +1,8 @@
#!/bin/bash
$CXX $CFLAGS $CXXFLAGS \
-I $SRC/fast_float/include \
-c $SRC/fast_float/fuzz/from_chars.cc -o from_chars.o
$CXX $CFLAGS $CXXFLAGS $LIB_FUZZING_ENGINE from_chars.o \
-o $OUT/from_chars

34
fuzz/from_chars.cc Normal file
View File

@ -0,0 +1,34 @@
#include "fast_float/fast_float.h"
#include <fuzzer/FuzzedDataProvider.h>
#include <string>
#include <system_error>
fast_float::chars_format arbitrary_format(FuzzedDataProvider &fdp) {
using fast_float::chars_format;
switch (fdp.ConsumeIntegralInRange<int>(0,3)) {
case 0:
return chars_format::scientific;
break;
case 1:
return chars_format::fixed;
break;
case 2:
return chars_format::fixed;
break;
}
return chars_format::general;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fdp(data, size);
fast_float::chars_format format = arbitrary_format(fdp);
double result_d = 0.0;
std::string input_d = fdp.ConsumeRandomLengthString(128);
auto answer =
fast_float::from_chars(input_d.data(), input_d.data() + input_d.size(), result_d, format);
std::string input_f = fdp.ConsumeRandomLengthString(128);
double result_f = 0.0;
answer =
fast_float::from_chars(input_f.data(), input_f.data() + input_f.size(), result_f, format);
return 0;
}