-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathfuzz.go
More file actions
79 lines (71 loc) · 1.87 KB
/
fuzz.go
File metadata and controls
79 lines (71 loc) · 1.87 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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.
// Copyright 2018 The Radon Authors.
package sqlparser
import (
"fmt"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/dvyukov/go-fuzz-corpus/fuzz"
)
func parseAll(data []byte) ([]sqlparser.Statement, error) {
stmt, err := sqlparser.Parse(string(data))
return []sqlparser.Statement{stmt}, err
}
// stringAndParse turns the Statement into a SQL string, re-parses
// that string, and checks the result matches the original.
func stringAndParse(data []byte, stmt sqlparser.Statement) {
data1 := sqlparser.String(stmt)
stmt1, err := sqlparser.Parse(data1)
if err != nil {
fmt.Printf("data0: %q\n", data)
fmt.Printf("data1: %q\n", data1)
panic(err)
}
if !fuzz.DeepEqual(stmt, stmt1) {
fmt.Printf("data0: %q\n", data)
fmt.Printf("data1: %q\n", data1)
panic("not equal")
}
}
func Fuzz(data []byte) int {
stmts, err := parseAll(data)
if err != nil {
return 0
}
for _, stmt := range stmts {
stringAndParse(data, stmt)
if sel, ok := stmt.(*sqlparser.Select); ok {
var nodes []sqlparser.SQLNode
for _, x := range sel.From {
nodes = append(nodes, x)
}
for _, x := range sel.SelectExprs {
nodes = append(nodes, x)
}
for _, x := range sel.GroupBy {
nodes = append(nodes, x)
}
for _, x := range sel.OrderBy {
nodes = append(nodes, x)
}
nodes = append(nodes, sel.Where)
nodes = append(nodes, sel.Having)
nodes = append(nodes, sel.Limit)
for _, n := range nodes {
if n == nil {
continue
}
if x, ok := n.(sqlparser.SimpleTableExpr); ok {
sqlparser.GetTableName(x)
}
if x, ok := n.(sqlparser.Expr); ok {
sqlparser.IsColName(x)
sqlparser.IsValue(x)
sqlparser.IsNull(x)
sqlparser.IsSimpleTuple(x)
}
}
}
}
return 1
}