golang: add 2 fuzzers (#8738)

Signed-off-by: AdamKorcz <adam@adalogics.com>

Signed-off-by: AdamKorcz <adam@adalogics.com>
This commit is contained in:
AdamKorcz 2022-10-11 00:26:45 +01:00 committed by GitHub
parent 9f94eb977e
commit d7de364029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -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

View File

@ -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
}