forked from dvyukov/go-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
literals.go
98 lines (90 loc) · 2.23 KB
/
literals.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"go/ast"
"go/token"
"strconv"
"golang.org/x/tools/go/packages"
)
func (c *Context) gatherLiterals(targets []*packages.Package, isIgnored func(string) bool) []string {
nolits := map[string]bool{
"math": true,
"os": true,
"unicode": true,
}
lits := make(map[string]struct{})
visit := func(pkg *packages.Package) {
if isIgnored(pkg.PkgPath) || nolits[pkg.PkgPath] {
return
}
for _, f := range pkg.Syntax {
ast.Walk(&LiteralCollector{lits: lits, ctxt: c}, f)
}
}
packages.Visit(targets, nil, visit)
litsList := make([]string, 0, len(lits))
for lit, _ := range lits {
litsList = append(litsList, lit)
}
return litsList
}
type LiteralCollector struct {
ctxt *Context
lits map[string]struct{}
}
func (lc *LiteralCollector) Visit(n ast.Node) (w ast.Visitor) {
switch nn := n.(type) {
default:
return lc // recurse
case *ast.ImportSpec:
return nil
case *ast.Field:
return nil // ignore field tags
case *ast.CallExpr:
switch fn := nn.Fun.(type) {
case *ast.Ident:
if fn.Name == "panic" {
return nil
}
case *ast.SelectorExpr:
if id, ok := fn.X.(*ast.Ident); ok && (id.Name == "fmt" || id.Name == "errors") {
return nil
}
}
return lc
case *ast.BasicLit:
lit := nn.Value
switch nn.Kind {
case token.CHAR:
// Conver 'a' -> "a"
lit = strconv.Quote(fmt.Sprintf("%c", lit[1]))
fallthrough
case token.STRING:
lc.lits[lit] = struct{}{}
case token.INT:
if lit[0] < '0' || lit[0] > '9' {
lc.ctxt.failf("unsupported literal '%v'", lit)
}
v, err := strconv.ParseInt(lit, 0, 64)
if err != nil {
u, err := strconv.ParseUint(lit, 0, 64)
if err != nil {
lc.ctxt.failf("failed to parse int literal '%v': %v", lit, err)
}
v = int64(u)
}
var val []byte
if v >= -(1<<7) && v < 1<<8 {
val = append(val, byte(v))
} else if v >= -(1<<15) && v < 1<<16 {
val = append(val, byte(v), byte(v>>8))
} else if v >= -(1<<31) && v < 1<<32 {
val = append(val, byte(v), byte(v>>8), byte(v>>16), byte(v>>24))
} else {
val = append(val, byte(v), byte(v>>8), byte(v>>16), byte(v>>24), byte(v>>32), byte(v>>40), byte(v>>48), byte(v>>56))
}
lc.lits[strconv.Quote(string(val))] = struct{}{}
}
return nil
}
}