Skip to content

Commit

Permalink
Limit $GENERATE range to 65535 steps
Browse files Browse the repository at this point in the history
Fixes #1019

Signed-off-by: Miek Gieben <[email protected]>
  • Loading branch information
miekg committed Oct 3, 2019
1 parent 046ae4e commit 6a985c6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
6 changes: 5 additions & 1 deletion generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (zp *ZoneParser) generate(l lex) (RR, bool) {
// _BLANK
l, ok := zp.c.Next()
if !ok || l.value != zBlank {
return zp.setParseError("garbage after $GENERATE range", l)
return zp.setParseError("garbage after $GENERATE range", l)
}

// Create a complete new string, which we then parse again.
Expand All @@ -72,6 +72,10 @@ func (zp *ZoneParser) generate(l lex) (RR, bool) {
s += l.token
}

if (end-start)/step > 65536 {
return zp.setParseError("bad range in $GENERATE directive", l)
}

r := &generateReader{
s: s,

Expand Down
39 changes: 33 additions & 6 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,11 @@ $GENERATE 32-158 dhcp-${-32,4,d} A 10.0.0.$
}
}



func TestCrasherString(t *testing.T) {
tests := []struct{
in string
err string
}{
tests := []struct {
in string
err string
}{
{"$GENERATE 0-300103\"$$GENERATE 2-2", "dns: garbage after $GENERATE range: \"\\\"\" at line: 1:19"},
{"$GENERATE 0-5414137360", "dns: garbage after $GENERATE range: \"\\n\" at line: 1:22"},
{"$GENERATE 11522-3668518066406258", "dns: garbage after $GENERATE range: \"\\n\" at line: 1:38"},
Expand All @@ -229,3 +227,32 @@ func TestCrasherString(t *testing.T) {
})
}
}

func TestOutofRangeRange(t *testing.T) {
// outside limit
data := `$GENERATE 0-65537 dhcp A 10.0.0.1`
zp := NewZoneParser(strings.NewReader(data), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
if !strings.Contains(zp.Err().Error(), "dns: bad range in $GENERATE directive:") {
t.Errorf("Expected %q error, got %s", "bad range", zp.Err())
}

// within limit
data = `$GENERATE 0-65535 dhcp A 10.0.0.1`
zp = NewZoneParser(strings.NewReader(data), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
if zp.Err() != nil {
t.Errorf("Expected nil error, got %s", zp.Err())
}

// within limit, due to /2
data = `$GENERATE 0-65537/2 dhcp A 10.0.0.1`
zp = NewZoneParser(strings.NewReader(data), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
if zp.Err() != nil {
t.Errorf("Expected nil error, got %s", zp.Err())
}
}

0 comments on commit 6a985c6

Please sign in to comment.