mirror of
https://github.com/prometheus/prometheus
synced 2025-05-05 09:41:23 +00:00
Add missing check for nil expression
This commit is contained in:
parent
ba24948ec7
commit
c20e25f718
@ -464,9 +464,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 +511,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 +546,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 +660,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
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user