]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/gc: Error out on division by constant zero.
authorDaniel Morsing <daniel.morsing@gmail.com>
Wed, 30 Jan 2013 19:21:08 +0000 (20:21 +0100)
committerDaniel Morsing <daniel.morsing@gmail.com>
Wed, 30 Jan 2013 19:21:08 +0000 (20:21 +0100)
Fixes #4264.

R=cldorian, rsc
CC=golang-dev
https://golang.org/cl/6845113

src/cmd/gc/typecheck.c
test/64bit.go
test/fixedbugs/bug410.go
test/fixedbugs/issue4264.go [new file with mode: 0644]

index d00e436719f88b41c2fee88db19bbb853ba56055..3846f7b8f186f3ac7c1e196885016d0798d1ffc7 100644 (file)
@@ -640,6 +640,13 @@ reswitch:
                                n->op = OCMPIFACE;
                        }
                }
+
+               if((op == ODIV || op == OMOD) && isconst(r, CTINT))
+               if(mpcmpfixc(r->val.u.xval, 0) == 0) {
+                       yyerror("division by zero");
+                       goto error;
+               } 
+
                n->type = t;
                goto ret;
 
index 7ad28ad4bc0e13bda3f0082aec42928352b6f38b..d99d8e83f0d544dc195cb2bb53ba8434b1c04e36 100644 (file)
@@ -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 {
index 35ecbfc05cb011f863500f59c44cb28dc02e8267..430ddcbb5237511a829aa5ceefb7b5cca95e5373 100644 (file)
@@ -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]
         }
     }
 }
diff --git a/test/fixedbugs/issue4264.go b/test/fixedbugs/issue4264.go
new file mode 100644 (file)
index 0000000..6273932
--- /dev/null
@@ -0,0 +1,19 @@
+// errorcheck
+
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// issue 4264: reject int division by const 0
+
+package main
+
+func main() {
+       var x int
+       var y float64
+       var z complex128
+
+       println(x/0) // ERROR "division by zero"
+       println(y/0)
+       println(z/0)
+}
\ No newline at end of file