Skip to content

Use fuzzing to fix some bugs #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func NewFromString(s string) (Line, error) {
// Note: Don't use this functions to compare rules because the order of flags can be
// different and different flags can have equal meanings.
func (r Rule) String() (s string) {
s = fmt.Sprintf("-A %s %s", r.Chain, strings.Join(enquoteIfWS(r.Spec()), " "))
s = fmt.Sprintf("-A %s %s", enquoteIfWS(r.Chain)[0], strings.Join(enquoteIfWS(r.Spec()...), " "))
if r.Counter != nil {
s = fmt.Sprintf("%s %s", r.Counter.String(), s)
}
Expand Down Expand Up @@ -432,6 +432,9 @@ var (
)

func (p *Parser) parseDefault(lit string) (Line, error) {
if len(strings.TrimSpace(lit)) == 0 {
return nil, fmt.Errorf("nothing to parse in %q", lit)
}
var r Policy
r.Chain = string(regDefault.ReplaceAll([]byte(lit), []byte("$1")))
a := regDefault.ReplaceAll([]byte(lit), []byte("$2"))
Expand Down Expand Up @@ -720,7 +723,7 @@ func (p *Parser) unscan(n int) {

var hasWS *regexp.Regexp = regexp.MustCompile(`\s`)

func enquoteIfWS(s []string) []string {
func enquoteIfWS(s ...string) []string {
ret := make([]string, len(s))
for i, e := range s {
if hasWS.MatchString(e) {
Expand Down
30 changes: 30 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1394,3 +1394,33 @@ func TestParser_ParseMore(t *testing.T) {
}
}
}

func FuzzParser_Parse(f *testing.F) {
f.Add(":hello ACCEPT [10:100]")
f.Fuzz(func(t *testing.T, input string) {
p := NewParser(strings.NewReader(input))
for s, err := p.Parse(); err != io.EOF; s, err = p.Parse() {
if err == nil && s == nil {
t.Fatal("no error; no output")
}
if err != nil && s != nil {
t.Fatal("error and output")
}
if err != nil {
t.Skip()
}
{
p := NewParser(strings.NewReader(s.String()))
s2, err2 := p.Parse()
if err2 != nil {
t.Fatal(err2.Error())
}
if !reflect.DeepEqual(s, s2) {
t.Logf("failed for %q", input)
t.Errorf("%q != %q", s.String(), s2.String())
}

}
}
})
}
17 changes: 10 additions & 7 deletions scanner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iptables_parser

import (
"fmt"
"strings"
"testing"
)
Expand Down Expand Up @@ -31,13 +32,15 @@ func TestScanner_Scan(t *testing.T) {
{s: "# 192.168.178.2/24", tok: COMMENTLINE, lit: " 192.168.178.2/24"},
{s: "I_test_rule-something", tok: IDENT, lit: "I_test_rule-something"},
} {
s := newScanner(strings.NewReader(tc.s))
tok, lit := s.scan()
if tc.tok != tok {
t.Errorf("%d. %q token mismatch: exp=%q got=%q <%q>", i, tc.s, tc.tok, tok, lit)
} else if tc.lit != lit {
t.Errorf("%d. %q literal mismatch: exp=%q got=%q", i, tc.s, tc.lit, lit)
}
t.Run(fmt.Sprintf("%d. %q", i, tc.s), func(t *testing.T) {
s := newScanner(strings.NewReader(tc.s))
tok, lit := s.scan()
if tc.tok != tok {
t.Errorf("%q token mismatch: exp=%q got=%q <%q>", tc.s, tc.tok, tok, lit)
} else if tc.lit != lit {
t.Errorf("%q literal mismatch: exp=%q got=%q", tc.s, tc.lit, lit)
}
})
}
}

Expand Down
2 changes: 2 additions & 0 deletions testdata/fuzz/FuzzParser_Parse/55cbba0554d1434e
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("-A\" 0")
2 changes: 2 additions & 0 deletions testdata/fuzz/FuzzParser_Parse/82c2975c430ac608
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string(":")