From d7de3640291406fe27ebfced72167e0de2c5359f Mon Sep 17 00:00:00 2001 From: AdamKorcz <44787359+AdamKorcz@users.noreply.github.com> Date: Tue, 11 Oct 2022 00:26:45 +0100 Subject: [PATCH] golang: add 2 fuzzers (#8738) Signed-off-by: AdamKorcz Signed-off-by: AdamKorcz --- projects/golang/build.sh | 2 ++ projects/golang/math_big_fuzzer.go | 55 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/projects/golang/build.sh b/projects/golang/build.sh index 56b3b6f4e..2d8e2410e 100755 --- a/projects/golang/build.sh +++ b/projects/golang/build.sh @@ -57,6 +57,8 @@ function compile_fuzzers() { compile_go_fuzzer $FUZZ_ROOT/text FuzzAcceptLanguage accept_language_fuzzer$version compile_go_fuzzer $FUZZ_ROOT/text FuzzMultipleParsers fuzz_multiple_parsers$version compile_go_fuzzer $FUZZ_ROOT/text FuzzCurrency currency_fuzzer$version + compile_go_fuzzer $FUZZ_ROOT/math FuzzFloatSetString fuzz_float_set_string$version + compile_go_fuzzer $FUZZ_ROOT/math FuzzBigGobdecode fuzz_big_gobdecode$version compile_go_fuzzer $FUZZ_ROOT/math FuzzBigIntCmp1 big_cmp_fuzzer1$version compile_go_fuzzer $FUZZ_ROOT/math FuzzBigIntCmp2 big_cmp_fuzzer2$version compile_go_fuzzer $FUZZ_ROOT/math FuzzRatSetString big_rat_fuzzer$version diff --git a/projects/golang/math_big_fuzzer.go b/projects/golang/math_big_fuzzer.go index 02127300f..79aa47f92 100644 --- a/projects/golang/math_big_fuzzer.go +++ b/projects/golang/math_big_fuzzer.go @@ -17,6 +17,7 @@ package mathfuzzer import ( "fmt" + fuzz "github.com/AdaLogics/go-fuzz-headers" "math" "math/big" "strconv" @@ -63,6 +64,60 @@ func FuzzRatSetString(data []byte) int { return 1 } +func FuzzFloatSetString(data []byte) int { + f := fuzz.NewConsumer(data) + f64, err := f.GetFloat64() + if err != nil { + return 0 + } + if math.IsNaN(f64) { + return 0 + } + s, err := f.GetString() + if err != nil { + return 0 + } + fl := big.NewFloat(f64) + fl.SetString(s) + return 1 +} + +func FuzzBigGobdecode(data []byte) int { + f := fuzz.NewConsumer(data) + buf, err := f.GetBytes() + if err != nil { + return 0 + } + target, err := f.GetInt() + if err != nil { + return 0 + } + switch target % 2 { + case 0: + i, err := f.GetInt() + if err != nil { + return 0 + } + bi := big.NewInt(int64(i)) + bi.GobDecode(buf) + case 1: + i1, err := f.GetInt() + if err != nil { + return 0 + } + i2, err := f.GetInt() + if err != nil { + return 0 + } + if int64(i2) == 0 { + return 0 + } + r := big.NewRat(int64(i1), int64(i2)) + r.GobDecode(buf) + } + return 1 +} + func isDivisibleBy(n int, divisibleby int) bool { return (n % divisibleby) == 0 }