Merge pull request #954 from prometheus/fabxc/fuzz-fix

Add missing check for nil expression
This commit is contained in:
Fabian Reinartz 2015-08-03 16:48:20 +02:00
commit c322422412
2 changed files with 63 additions and 11 deletions

View File

@ -321,6 +321,8 @@ func (p *parser) expectOneOf(exp1, exp2 itemType, context string) item {
return token return token
} }
var errUnexpected = fmt.Errorf("unexpected error")
// recover is the handler that turns panics into returns from the top level of Parse. // recover is the handler that turns panics into returns from the top level of Parse.
func (p *parser) recover(errp *error) { func (p *parser) recover(errp *error) {
e := recover() e := recover()
@ -331,7 +333,7 @@ func (p *parser) recover(errp *error) {
buf = buf[:runtime.Stack(buf, false)] buf = buf[:runtime.Stack(buf, false)]
log.Errorf("parser panic: %v\n%s", e, buf) log.Errorf("parser panic: %v\n%s", e, buf)
*errp = fmt.Errorf("unexpected error") *errp = errUnexpected
} else { } else {
*errp = e.(error) *errp = e.(error)
} }
@ -464,9 +466,6 @@ func (p *parser) recordStmt() *RecordStmt {
func (p *parser) expr() Expr { func (p *parser) expr() Expr {
// Parse the starting expression. // Parse the starting expression.
expr := p.unaryExpr() expr := p.unaryExpr()
if expr == nil {
p.errorf("no valid expression found")
}
// Loop through the operations and construct a binary operation tree based // Loop through the operations and construct a binary operation tree based
// on the operators' precedence. // on the operators' precedence.
@ -514,9 +513,6 @@ func (p *parser) expr() Expr {
// Parse the next operand. // Parse the next operand.
rhs := p.unaryExpr() rhs := p.unaryExpr()
if rhs == nil {
p.errorf("missing right-hand side in binary expression")
}
// Assign the new root based on the precendence of the LHS and RHS operators. // Assign the new root based on the precendence of the LHS and RHS operators.
if lhs, ok := expr.(*BinaryExpr); ok && lhs.Op.precedence() < op.precedence() { if lhs, ok := expr.(*BinaryExpr); ok && lhs.Op.precedence() < op.precedence() {
@ -552,6 +548,7 @@ func (p *parser) unaryExpr() Expr {
case itemADD, itemSUB: case itemADD, itemSUB:
p.next() p.next()
e := p.unaryExpr() e := p.unaryExpr()
// Simplify unary expressions for number literals. // Simplify unary expressions for number literals.
if nl, ok := e.(*NumberLiteral); ok { if nl, ok := e.(*NumberLiteral); ok {
if t.typ == itemSUB { if t.typ == itemSUB {
@ -665,6 +662,9 @@ func (p *parser) primaryExpr() Expr {
case t.typ.isAggregator(): case t.typ.isAggregator():
p.backup() p.backup()
return p.aggrExpr() return p.aggrExpr()
default:
p.errorf("no valid expression found")
} }
return nil return nil
} }

View File

@ -134,7 +134,7 @@ var testExpr = []struct {
}, { }, {
input: "1+", input: "1+",
fail: true, fail: true,
errMsg: "missing right-hand side in binary expression", errMsg: "no valid expression found",
}, { }, {
input: ".", input: ".",
fail: true, fail: true,
@ -154,7 +154,7 @@ var testExpr = []struct {
}, { }, {
input: "1 /", input: "1 /",
fail: true, fail: true,
errMsg: "missing right-hand side in binary expression", errMsg: "no valid expression found",
}, { }, {
input: "*1", input: "*1",
fail: true, fail: true,
@ -945,6 +945,27 @@ var testExpr = []struct {
fail: true, fail: true,
errMsg: "expected type matrix in call to function \"rate\", got vector", errMsg: "expected type matrix in call to function \"rate\", got vector",
}, },
// Fuzzing regression tests.
{
input: "-=",
fail: true,
errMsg: `no valid expression found`,
},
{
input: "++-++-+-+-<",
fail: true,
errMsg: `no valid expression found`,
},
{
input: "e-+=/(0)",
fail: true,
errMsg: `no valid expression found`,
},
{
input: "-If",
fail: true,
errMsg: `no valid expression found`,
},
} }
func TestParseExpressions(t *testing.T) { func TestParseExpressions(t *testing.T) {
@ -952,6 +973,12 @@ func TestParseExpressions(t *testing.T) {
parser := newParser(test.input) parser := newParser(test.input)
expr, err := parser.parseExpr() expr, err := parser.parseExpr()
// Unexpected errors are always caused by a bug.
if err == errUnexpected {
t.Fatalf("unexpected error occurred")
}
if !test.fail && err != nil { if !test.fail && err != nil {
t.Errorf("error in input '%s'", test.input) t.Errorf("error in input '%s'", test.input)
t.Fatalf("could not parse: %s", err) t.Fatalf("could not parse: %s", err)
@ -1198,6 +1225,19 @@ var testStatement = []struct {
`, `,
fail: true, fail: true,
}, },
// Fuzzing regression tests.
{
input: `I=-/`,
fail: true,
},
{
input: `I=3E8/-=`,
fail: true,
},
{
input: `M=-=-0-0`,
fail: true,
},
} }
func TestParseStatements(t *testing.T) { func TestParseStatements(t *testing.T) {
@ -1205,6 +1245,12 @@ func TestParseStatements(t *testing.T) {
parser := newParser(test.input) parser := newParser(test.input)
stmts, err := parser.parseStmts() stmts, err := parser.parseStmts()
// Unexpected errors are always caused by a bug.
if err == errUnexpected {
t.Fatalf("unexpected error occurred")
}
if !test.fail && err != nil { if !test.fail && err != nil {
t.Errorf("error in input: \n\n%s\n", test.input) t.Errorf("error in input: \n\n%s\n", test.input)
t.Fatalf("could not parse: %s", err) t.Fatalf("could not parse: %s", err)
@ -1332,6 +1378,12 @@ func TestParseSeries(t *testing.T) {
parser.lex.seriesDesc = true parser.lex.seriesDesc = true
metric, vals, err := parser.parseSeriesDesc() metric, vals, err := parser.parseSeriesDesc()
// Unexpected errors are always caused by a bug.
if err == errUnexpected {
t.Fatalf("unexpected error occurred")
}
if !test.fail && err != nil { if !test.fail && err != nil {
t.Errorf("error in input: \n\n%s\n", test.input) t.Errorf("error in input: \n\n%s\n", test.input)
t.Fatalf("could not parse: %s", err) t.Fatalf("could not parse: %s", err)
@ -1364,8 +1416,8 @@ func TestRecoverRuntime(t *testing.T) {
var a []int var a []int
a[123] = 1 a[123] = 1
if err.Error() != "unexpected error" { if err != errUnexpected {
t.Fatalf("wrong error message: %q, expected %q", err, "unexpected error") t.Fatalf("wrong error message: %q, expected %q", err, errUnexpected)
} }
} }