Commit 79db6ada authored by Rémy Oudompheng's avatar Rémy Oudompheng

cmd/gc: error on constant shift overflows.

Fixes #3019.

R=golang-dev, rsc
CC=golang-dev, remy
https://golang.org/cl/5674044
parent 88f8af12
...@@ -27,10 +27,10 @@ mplen(Mpint *a) ...@@ -27,10 +27,10 @@ mplen(Mpint *a)
// //
// left shift mpint by one // left shift mpint by one
// ignores sign and overflow // ignores sign
// //
static void static void
mplsh(Mpint *a) mplsh(Mpint *a, int quiet)
{ {
long *a1, x; long *a1, x;
int i, c; int i, c;
...@@ -46,19 +46,27 @@ mplsh(Mpint *a) ...@@ -46,19 +46,27 @@ mplsh(Mpint *a)
} }
*a1++ = x; *a1++ = x;
} }
a->ovf = c;
if(a->ovf && !quiet)
yyerror("constant shift overflow");
} }
// //
// left shift mpint by Mpscale // left shift mpint by Mpscale
// ignores sign and overflow // ignores sign
// //
static void static void
mplshw(Mpint *a) mplshw(Mpint *a, int quiet)
{ {
long *a1; long *a1;
int i; int i;
a1 = &a->a[Mpprec-1]; a1 = &a->a[Mpprec-1];
if(*a1) {
a->ovf = 1;
if(!quiet)
yyerror("constant shift overflow");
}
for(i=1; i<Mpprec; i++) { for(i=1; i<Mpprec; i++) {
a1[0] = a1[-1]; a1[0] = a1[-1];
a1--; a1--;
...@@ -168,11 +176,11 @@ mpshiftfix(Mpint *a, int s) ...@@ -168,11 +176,11 @@ mpshiftfix(Mpint *a, int s)
{ {
if(s >= 0) { if(s >= 0) {
while(s >= Mpscale) { while(s >= Mpscale) {
mplshw(a); mplshw(a, 0);
s -= Mpscale; s -= Mpscale;
} }
while(s > 0) { while(s > 0) {
mplsh(a); mplsh(a, 0);
s--; s--;
} }
} else { } else {
...@@ -294,7 +302,7 @@ mpmulfixfix(Mpint *a, Mpint *b) ...@@ -294,7 +302,7 @@ mpmulfixfix(Mpint *a, Mpint *b)
for(j=0; j<Mpscale; j++) { for(j=0; j<Mpscale; j++) {
if(x & 1) if(x & 1)
mpaddfixfix(&q, &s, 1); mpaddfixfix(&q, &s, 1);
mplsh(&s); mplsh(&s, 1);
x >>= 1; x >>= 1;
} }
} }
...@@ -606,7 +614,7 @@ mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d) ...@@ -606,7 +614,7 @@ mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d)
for(i=0; i<Mpprec*Mpscale; i++) { for(i=0; i<Mpprec*Mpscale; i++) {
if(mpcmp(d, r) > 0) if(mpcmp(d, r) > 0)
break; break;
mplsh(d); mplsh(d, 1);
} }
// if it never happens // if it never happens
...@@ -625,7 +633,7 @@ mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d) ...@@ -625,7 +633,7 @@ mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d)
// when done the remaining numerator // when done the remaining numerator
// will be the remainder // will be the remainder
for(; i>0; i--) { for(; i>0; i--) {
mplsh(q); mplsh(q, 1);
mprsh(d); mprsh(d);
if(mpcmp(d, r) <= 0) { if(mpcmp(d, r) <= 0) {
mpaddcfix(q, 1); mpaddcfix(q, 1);
......
...@@ -13,4 +13,6 @@ const ( ...@@ -13,4 +13,6 @@ const (
const LargeA = 1000000000000000000 const LargeA = 1000000000000000000
const LargeB = LargeA * LargeA * LargeA 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"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment