Skip to content

Commit 4932922

Browse files
author
Tor Didriksen
committed
Bug#20768820 MAIN.BIGINT TEST FAILS WHEN BUILT WITH GCC 5 IN RELEASE BUILD
Problem: with gcc5 in optmized mode, (- LLONG_MIN ) yields integer overflow. Fix: In ull2dec() change the loop which counts the number of decimal_digit_t's (cherry picked from commit b37d8bcc24f82f8e15c5f6e2243c8937af74acb7)
1 parent 6e16179 commit 4932922

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

strings/decimal.c

+19-11
Original file line numberDiff line numberDiff line change
@@ -1064,26 +1064,34 @@ int double2decimal(double from, decimal_t *to)
10641064

10651065
static int ull2dec(ulonglong from, decimal_t *to)
10661066
{
1067-
int intg1, error=E_DEC_OK;
1068-
ulonglong x=from;
1067+
int intg1;
1068+
int error= E_DEC_OK;
1069+
ulonglong x= from;
10691070
dec1 *buf;
10701071

10711072
sanity(to);
10721073

1073-
for (intg1=1; from >= DIG_BASE; intg1++, from/=DIG_BASE) ;
1074+
if (from == 0)
1075+
intg1= 1;
1076+
else
1077+
{
1078+
/* Count the number of decimal_digit_t's we need. */
1079+
for (intg1= 0; from != 0; intg1++, from/= DIG_BASE)
1080+
;
1081+
}
10741082
if (unlikely(intg1 > to->len))
10751083
{
1076-
intg1=to->len;
1077-
error=E_DEC_OVERFLOW;
1084+
intg1= to->len;
1085+
error= E_DEC_OVERFLOW;
10781086
}
1079-
to->frac=0;
1080-
to->intg=intg1*DIG_PER_DEC1;
1087+
to->frac= 0;
1088+
to->intg= intg1 * DIG_PER_DEC1;
10811089

1082-
for (buf=to->buf+intg1; intg1; intg1--)
1090+
for (buf= to->buf + intg1; intg1; intg1--)
10831091
{
1084-
ulonglong y=x/DIG_BASE;
1085-
*--buf=(dec1)(x-y*DIG_BASE);
1086-
x=y;
1092+
ulonglong y= x / DIG_BASE;
1093+
*--buf=(dec1)(x - y * DIG_BASE);
1094+
x= y;
10871095
}
10881096
return error;
10891097
}

0 commit comments

Comments
 (0)