]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/gc: error on constant shift overflows.
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Wed, 15 Feb 2012 23:19:42 +0000 (00:19 +0100)
committerRémy Oudompheng <oudomphe@phare.normalesup.org>
Wed, 15 Feb 2012 23:19:42 +0000 (00:19 +0100)
Fixes #3019.

R=golang-dev, rsc
CC=golang-dev, remy
https://golang.org/cl/5674044

src/cmd/gc/mparith2.c
test/const2.go

index c802e4468a836f69a3c2ebb56fc7adbef7502e30..8e52ff21625101fc37a1171bfb3ae2b2121f6939 100644 (file)
@@ -27,10 +27,10 @@ mplen(Mpint *a)
 
 //
 // left shift mpint by one
-// ignores sign and overflow
+// ignores sign
 //
 static void
-mplsh(Mpint *a)
+mplsh(Mpint *a, int quiet)
 {
        long *a1, x;
        int i, c;
@@ -46,19 +46,27 @@ mplsh(Mpint *a)
                }
                *a1++ = x;
        }
+       a->ovf = c;
+       if(a->ovf && !quiet)
+               yyerror("constant shift overflow");
 }
 
 //
 // left shift mpint by Mpscale
-// ignores sign and overflow
+// ignores sign
 //
 static void
-mplshw(Mpint *a)
+mplshw(Mpint *a, int quiet)
 {
        long *a1;
        int i;
 
        a1 = &a->a[Mpprec-1];
+       if(*a1) {
+               a->ovf = 1;
+               if(!quiet)
+                       yyerror("constant shift overflow");
+       }
        for(i=1; i<Mpprec; i++) {
                a1[0] = a1[-1];
                a1--;
@@ -168,11 +176,11 @@ mpshiftfix(Mpint *a, int s)
 {
        if(s >= 0) {
                while(s >= Mpscale) {
-                       mplshw(a);
+                       mplshw(a, 0);
                        s -= Mpscale;
                }
                while(s > 0) {
-                       mplsh(a);
+                       mplsh(a, 0);
                        s--;
                }
        } else {
@@ -294,7 +302,7 @@ mpmulfixfix(Mpint *a, Mpint *b)
                for(j=0; j<Mpscale; j++) {
                        if(x & 1)
                                mpaddfixfix(&q, &s, 1);
-                       mplsh(&s);
+                       mplsh(&s, 1);
                        x >>= 1;
                }
        }
@@ -606,7 +614,7 @@ mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d)
        for(i=0; i<Mpprec*Mpscale; i++) {
                if(mpcmp(d, r) > 0)
                        break;
-               mplsh(d);
+               mplsh(d, 1);
        }
 
        // if it never happens
@@ -625,7 +633,7 @@ mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d)
        // when done the remaining numerator
        // will be the remainder
        for(; i>0; i--) {
-               mplsh(q);
+               mplsh(q, 1);
                mprsh(d);
                if(mpcmp(d, r) <= 0) {
                        mpaddcfix(q, 1);
index b0837354abfeeedf30e2724d6472b41eeead01b0..12c5c24af02bb56bcf7de3d221d299fdb61f8376 100644 (file)
@@ -13,4 +13,6 @@ const (
 
 const LargeA = 1000000000000000000
 const LargeB = LargeA * LargeA * LargeA
-const LargeC = LargeB * LargeB * LargeB  // ERROR "constant multiplication overflow"
+const LargeC = LargeB * LargeB * LargeB // ERROR "constant multiplication overflow"
+
+const AlsoLargeA = LargeA << 400 << 400 >> 400 >> 400 // ERROR "constant shift overflow"