aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@google.com>2013-06-26 22:42:35 +0000
committerIan Lance Taylor <iant@google.com>2013-06-26 22:42:35 +0000
commit3c5da106adaca5a29d1f6220453262467fd456b6 (patch)
tree80c3c4288b5dd0d2a516d77d36fc58e905b7d0de
parent460ad2b4c7a8a4e3655bf0e6fae052fdc61c280b (diff)
compiler: reject integer division by zero constant.
git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@200436 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--gcc/go/gofrontend/expressions.cc14
-rw-r--r--gcc/testsuite/go.test/test/64bit.go25
-rw-r--r--gcc/testsuite/go.test/test/fixedbugs/bug410.go2
3 files changed, 38 insertions, 3 deletions
diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc
index 9085a46b73d..2b60d90a5dc 100644
--- a/gcc/go/gofrontend/expressions.cc
+++ b/gcc/go/gofrontend/expressions.cc
@@ -5848,6 +5848,20 @@ Binary_expression::do_check_types(Gogo*)
this->set_is_error();
return;
}
+ if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD)
+ {
+ // Division by a zero integer constant is an error.
+ Numeric_constant rconst;
+ unsigned long rval;
+ if (left_type->integer_type() != NULL
+ && this->right_->numeric_constant_value(&rconst)
+ && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID
+ && rval == 0)
+ {
+ this->report_error(_("integer division by zero"));
+ return;
+ }
+ }
}
else
{
diff --git a/gcc/testsuite/go.test/test/64bit.go b/gcc/testsuite/go.test/test/64bit.go
index 7ad28ad4bc0..d99d8e83f0d 100644
--- a/gcc/testsuite/go.test/test/64bit.go
+++ b/gcc/testsuite/go.test/test/64bit.go
@@ -594,6 +594,19 @@ const binaryConstR = "func test%vBinaryR%v(a, add, sub, mul, div, mod, and, or,
"}\n" +
"\n"
+const binaryConstR0 = "func test%vBinaryR%v(a, add, sub, mul, div, mod, and, or, xor, andnot %v, dodiv bool) {\n" +
+ " const b %v = %v;\n" +
+ " const typ = `%s`;\n" +
+ " if n, op, want := a + b, `+`, add; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
+ " if n, op, want := a - b, `-`, sub; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
+ " if n, op, want := a * b, `*`, mul; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
+ " if n, op, want := a & b, `&`, and; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
+ " if n, op, want := a | b, `|`, or; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
+ " if n, op, want := a ^ b, `^`, xor; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
+ " if n, op, want := a &^ b, `&^`, andnot; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
+ "}\n" +
+ "\n"
+
const shiftConstL = "func test%vShiftL%v(b uint64, left, right %v) {\n" +
" const a %v = %v;\n" +
" const typ = `%s`;\n" +
@@ -621,12 +634,20 @@ const shiftConstR = "func test%vShiftR%v(a, left, right %v) {\n" +
func constTests() {
for i, a := range int64Values {
fmt.Fprintf(bout, binaryConstL, "Int64", i, "int64", "int64", a, "int64")
- fmt.Fprintf(bout, binaryConstR, "Int64", i, "int64", "int64", a, "int64")
+ if a.hi == 0 && a.lo == 0 {
+ fmt.Fprintf(bout, binaryConstR0, "Int64", i, "int64", "int64", a, "int64")
+ } else {
+ fmt.Fprintf(bout, binaryConstR, "Int64", i, "int64", "int64", a, "int64")
+ }
fmt.Fprintf(bout, shiftConstL, "Int64", i, "int64", "int64", a, "int64")
}
for i, a := range uint64Values {
fmt.Fprintf(bout, binaryConstL, "Uint64", i, "uint64", "uint64", a, "uint64")
- fmt.Fprintf(bout, binaryConstR, "Uint64", i, "uint64", "uint64", a, "uint64")
+ if a.hi == 0 && a.lo == 0 {
+ fmt.Fprintf(bout, binaryConstR0, "Uint64", i, "uint64", "uint64", a, "uint64")
+ } else {
+ fmt.Fprintf(bout, binaryConstR, "Uint64", i, "uint64", "uint64", a, "uint64")
+ }
fmt.Fprintf(bout, shiftConstL, "Uint64", i, "uint64", "uint64", a, "uint64")
}
for i, a := range shiftValues {
diff --git a/gcc/testsuite/go.test/test/fixedbugs/bug410.go b/gcc/testsuite/go.test/test/fixedbugs/bug410.go
index 35ecbfc05cb..430ddcbb523 100644
--- a/gcc/testsuite/go.test/test/fixedbugs/bug410.go
+++ b/gcc/testsuite/go.test/test/fixedbugs/bug410.go
@@ -18,7 +18,7 @@ func zzz () {
for s := range arr {
x := make([]byte, 10)
for i := 0; i < 100 ; i++ {
- x[i] ^= k[i-arr[s].num%0]
+ x[i] ^= k[i-arr[s].num%3]
}
}
}