Skip to content

Commit

Permalink
improve fuzz testing and fix actual crash
Browse files Browse the repository at this point in the history
  • Loading branch information
alicebob committed Oct 4, 2023
1 parent d0775ff commit 3e39ffe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion fpconv/dtoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func minv(a, b int) int {

func Dtoa(d float64) string {
var (
dest [24]rune
dest [25]rune // Note C has 24, which is broken
digits [18]rune

str_len int = 0
Expand Down
4 changes: 4 additions & 0 deletions fpconv/fpconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ func TestFormatFloat(t *testing.T) {
eq(t, "3.5999999999999996", a)
a += 1.2
eq(t, "4.8", a)

// fuzz cases
eq(t, "386804001288986340000000", 3.8680400128898634e+23) // not a fuzz case...
eq(t, "-386804001288986340000000", -3.8680400128898634e+23) // ...but this one crashed
}
15 changes: 8 additions & 7 deletions fpconv/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//go:build !go1.14
// +build !go1.14
//go:build go1.16
// +build go1.16

package fpconv

import (
"strings"
"strconv"
"testing"
)

Expand All @@ -19,11 +19,12 @@ func FuzzDtoa(f *testing.F) {
if s == "" {
t.Errorf("empty result")
}
if strings.Count(s, ".") > 1 {
t.Errorf("too many .s")
n, err := strconv.ParseFloat(s, 64)
if err != nil {
t.Errorf("parse failed: %s", err)
}
if strings.Count(s, "e") > 1 {
t.Errorf("too many e's")
if n != orig {
t.Errorf("changed %f -> %f", n, orig)
}
})
}

0 comments on commit 3e39ffe

Please sign in to comment.