Skip to content

Commit

Permalink
go/types: don't panic in complex division
Browse files Browse the repository at this point in the history
Make sure that in complex division we reject divisors that would
underflow to zero when using the textbook complex-division method we
currently use.

This change does for go/types what golang.org/cl/42650 did for gc.

Fixes #20227

Change-Id: Iaa784ac5e60141f51c501eb0e3ce0e9c1c2993d0
Reviewed-on: https://go-review.googlesource.com/44590
Run-TryBot: Alberto Donizetti <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
ALTree committed Jun 5, 2017
1 parent b1af539 commit 3c745d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/go/types/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,10 +800,24 @@ func (check *Checker) binary(x *operand, e *ast.BinaryExpr, lhs, rhs ast.Expr, o
return
}

if (op == token.QUO || op == token.REM) && (x.mode == constant_ || isInteger(x.typ)) && y.mode == constant_ && constant.Sign(y.val) == 0 {
check.invalidOp(y.pos(), "division by zero")
x.mode = invalid
return
if op == token.QUO || op == token.REM {
// check for zero divisor
if (x.mode == constant_ || isInteger(x.typ)) && y.mode == constant_ && constant.Sign(y.val) == 0 {
check.invalidOp(y.pos(), "division by zero")
x.mode = invalid
return
}

// check for divisor underflow in complex division (see issue 20227)
if x.mode == constant_ && y.mode == constant_ && isComplex(x.typ) {
re, im := constant.Real(y.val), constant.Imag(y.val)
re2, im2 := constant.BinaryOp(re, token.MUL, re), constant.BinaryOp(im, token.MUL, im)
if constant.Sign(re2) == 0 && constant.Sign(im2) == 0 {
check.invalidOp(y.pos(), "division by zero")
x.mode = invalid
return
}
}
}

if x.mode == constant_ && y.mode == constant_ {
Expand Down
1 change: 0 additions & 1 deletion src/go/types/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ func TestStdFixed(t *testing.T) {
"issue18459.go", // go/types doesn't check validity of //go:xxx directives
"issue18882.go", // go/types doesn't check validity of //go:xxx directives
"issue20232.go", // go/types handles larger constants than gc
"issue20227.go", // go/types does not handle this yet
"issue20529.go", // go/types does not have constraints on stack size
)
}
Expand Down

0 comments on commit 3c745d7

Please sign in to comment.