forked from dvyukov/go-fuzz
/
main.go
64 lines (60 loc) · 1.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2015 Dmitry Vyukov. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package gofmt
import (
"bytes"
"fmt"
"go/format"
"unicode/utf8"
)
func Fuzz(data []byte) int {
if bytes.Contains(data, []byte("//line")) {
// https://github.com/golang/go/issues/11276
return 0
}
if bytes.IndexByte(data, ';') != -1 {
// https://github.com/golang/go/issues/11274
return 0
}
if bytes.IndexByte(data, '\r') != -1 {
// https://github.com/golang/go/issues/11151
return 0
}
if !utf8.Valid(data) {
return 0
}
data1, err := format.Source(data)
if err != nil {
if data1 != nil {
panic("data is not nil on error")
}
return 0
}
data2, err := format.Source(data1)
if err != nil {
fmt.Printf("orig: %q\n", data)
fmt.Printf("new : %q\n", data1)
panic(err)
}
if !bytes.Equal(data1, data2) {
fmt.Printf("orig: %q\n", data)
fmt.Printf("new1: %q\n", data1)
fmt.Printf("new2: %q\n", data2)
panic("non-idempotent format")
}
can := canonical(data)
can1 := canonical(data1)
if !bytes.Equal(can, can1) {
fmt.Printf("orig: %q\n", data)
fmt.Printf("new : %q\n", data1)
panic("corrupting format")
}
return 1
}
func canonical(data []byte) []byte {
for i := 0; i <= ' '; i++ {
data = bytes.Replace(data, []byte{byte(i)}, nil, -1)
}
data = bytes.Replace(data, []byte{';'}, nil, -1)
return data
}