diff --git a/src/internal/fuzz/minimize.go b/src/internal/fuzz/minimize.go index 974df369ee..c6e4559665 100644 --- a/src/internal/fuzz/minimize.go +++ b/src/internal/fuzz/minimize.go @@ -79,6 +79,25 @@ func minimizeBytes(v []byte, try func(interface{}) bool, shouldStop func() bool) j = len(v) } } + + // Then, try to make it more simplified and human-readable by trying to replace each + // byte with a printable character. + printableChars := []byte("012789ABCXYZabcxyz !\"#$%&'()*+,.") + for i, b := range v { + if shouldStop() { + return + } + + for _, pc := range printableChars { + v[i] = pc + if try(v) { + // Successful. Move on to the next byte in v. + break + } + // Unsuccessful. Revert v[i] back to original value. + v[i] = b + } + } } func minimizeInteger(v uint, try func(interface{}) bool, shouldStop func() bool) { diff --git a/src/internal/fuzz/minimize_test.go b/src/internal/fuzz/minimize_test.go index 13385e14d6..dc153d0de4 100644 --- a/src/internal/fuzz/minimize_test.go +++ b/src/internal/fuzz/minimize_test.go @@ -13,6 +13,8 @@ import ( "fmt" "reflect" "testing" + "unicode" + "unicode/utf8" ) func TestMinimizeInput(t *testing.T) { @@ -54,7 +56,7 @@ func TestMinimizeInput(t *testing.T) { return fmt.Errorf("bad %v", e.Values[0]) }, input: []interface{}{[]byte{1, 2, 3, 4, 5}}, - expected: []interface{}{[]byte{2, 3}}, + expected: []interface{}{[]byte("00")}, }, { name: "set_of_bytes", @@ -71,6 +73,18 @@ func TestMinimizeInput(t *testing.T) { input: []interface{}{[]byte{0, 1, 2, 3, 4, 5}}, expected: []interface{}{[]byte{0, 4, 5}}, }, + { + name: "non_ascii_bytes", + fn: func(e CorpusEntry) error { + b := e.Values[0].([]byte) + if len(b) == 3 { + return fmt.Errorf("bad %v", e.Values[0]) + } + return nil + }, + input: []interface{}{[]byte("ท")}, // ท is 3 bytes + expected: []interface{}{[]byte("000")}, + }, { name: "ones_string", fn: func(e CorpusEntry) error { @@ -89,6 +103,31 @@ func TestMinimizeInput(t *testing.T) { input: []interface{}{"001010001000000000000000000"}, expected: []interface{}{"111"}, }, + { + name: "string_length", + fn: func(e CorpusEntry) error { + b := e.Values[0].(string) + if len(b) == 5 { + return fmt.Errorf("bad %v", e.Values[0]) + } + return nil + }, + input: []interface{}{"zzzzz"}, + expected: []interface{}{"00000"}, + }, + { + name: "string_with_letter", + fn: func(e CorpusEntry) error { + b := e.Values[0].(string) + r, _ := utf8.DecodeRune([]byte(b)) + if unicode.IsLetter(r) { + return fmt.Errorf("bad %v", e.Values[0]) + } + return nil + }, + input: []interface{}{"ZZZZZ"}, + expected: []interface{}{"A"}, + }, { name: "int", fn: func(e CorpusEntry) error {