Skip to content

Commit e7d40af

Browse files
committed
Fixed bug #77742
By avoiding integer overflow in the implementation entirely. The multiplication was already explicitly checked for overflow, so also add a check for the addition and remove the overflow checks after the calculation.
1 parent c7920ab commit e7d40af

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ PHP NEWS
1111
. Fixed bug #77676 (Unable to run tests when building shared extension on
1212
AIX). (Kevin Adler)
1313

14+
- Bcmath:
15+
. Fixed bug #77742 (bcpow() implementation related to gcc compiler
16+
optimization). (Nikita)
17+
1418
- FPM:
1519
. Fixed bug #77677 (FPM fails to build on AIX due to missing WCOREDUMP).
1620
(Kevin Adler)

ext/bcmath/libbcmath/src/num2long.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ bc_num2long (num)
5454
/* Extract the int value, ignore the fraction. */
5555
val = 0;
5656
nptr = num->n_value;
57-
for (index=num->n_len; (index>0) && (val<=(LONG_MAX/BASE)); index--)
58-
val = val*BASE + *nptr++;
57+
for (index = num->n_len; index > 0; index--) {
58+
char n = *nptr++;
5959

60-
/* Check for overflow. If overflow, return zero. */
61-
if (index>0) val = 0;
62-
if (val < 0) val = 0;
60+
if (val > LONG_MAX/BASE) {
61+
return 0;
62+
}
63+
val *= BASE;
64+
65+
if (val > LONG_MAX - n) {
66+
return 0;
67+
}
68+
val += n;
69+
}
6370

6471
/* Return the value. */
6572
if (num->n_sign == PLUS)

0 commit comments

Comments
 (0)