com php-src: Fixed bug #77742: NEWS ext/bcmath/libbcmath/src/num2long.c
| From: | Nikita Popov | Date: | Thu, 14 Mar 2019 16:24:50 +0000 |
| Subject: | com php-src: Fixed bug #77742: NEWS ext/bcmath/libbcmath/src/num2long.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-105142@lists.php.net to get a copy of this message | ||
Commit: e7d40afb7a7984174eb132a14b7a6621c8e76258
Author: Nikita Popov <nikita.ppv@gmail.com> Thu, 14 Mar 2019 17:24:50 +0100
Parents: c7920aba3e1892accca7cd13ef5b8a8fbf48b5c2
Branches: PHP-7.2 PHP-7.3 PHP-7.4 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=e7d40afb7a7984174eb132a14b7a6621c8e76258
Log:
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.
Bugs:
https://bugs.php.net/77742
Changed paths:
M NEWS
M ext/bcmath/libbcmath/src/num2long.c
Diff:
diff --git a/NEWS b/NEWS
index a088343..ad1b28a 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,10 @@ PHP NEWS
. Fixed bug #77676 (Unable to run tests when building shared extension on
AIX). (Kevin Adler)
+- Bcmath:
+ . Fixed bug #77742 (bcpow() implementation related to gcc compiler
+ optimization). (Nikita)
+
- FPM:
. Fixed bug #77677 (FPM fails to build on AIX due to missing WCOREDUMP).
(Kevin Adler)
diff --git a/ext/bcmath/libbcmath/src/num2long.c b/ext/bcmath/libbcmath/src/num2long.c
index 81e82a6..228f664 100644
--- a/ext/bcmath/libbcmath/src/num2long.c
+++ b/ext/bcmath/libbcmath/src/num2long.c
@@ -54,12 +54,19 @@ bc_num2long (num)
/* Extract the int value, ignore the fraction. */
val = 0;
nptr = num->n_value;
- for (index=num->n_len; (index>0) && (val<=(LONG_MAX/BASE)); index--)
- val = val*BASE + *nptr++;
+ for (index = num->n_len; index > 0; index--) {
+ char n = *nptr++;
- /* Check for overflow. If overflow, return zero. */
- if (index>0) val = 0;
- if (val < 0) val = 0;
+ if (val > LONG_MAX/BASE) {
+ return 0;
+ }
+ val *= BASE;
+
+ if (val > LONG_MAX - n) {
+ return 0;
+ }
+ val += n;
+ }
/* Return the value. */
if (num->n_sign == PLUS)