From 79db6ada48d09dbbf47c4fb0f49ebbd2a044a35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Oudompheng?= Date: Thu, 16 Feb 2012 00:19:42 +0100 Subject: [PATCH] cmd/gc: error on constant shift overflows. Fixes #3019. R=golang-dev, rsc CC=golang-dev, remy https://golang.org/cl/5674044 --- src/cmd/gc/mparith2.c | 26 +++++++++++++++++--------- test/const2.go | 4 +++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/cmd/gc/mparith2.c b/src/cmd/gc/mparith2.c index c802e4468a..8e52ff2162 100644 --- a/src/cmd/gc/mparith2.c +++ b/src/cmd/gc/mparith2.c @@ -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= 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>= 1; } } @@ -606,7 +614,7 @@ mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d) for(i=0; i 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); diff --git a/test/const2.go b/test/const2.go index b0837354ab..12c5c24af0 100644 --- a/test/const2.go +++ b/test/const2.go @@ -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"