Skip to content

Commit

Permalink
Fix #78878: Buffer underflow in bc_shift_addsub
Browse files Browse the repository at this point in the history
We must not rely on `isdigit()` to detect digits, since we only support
decimal ASCII digits in the following processing.

(cherry picked from commit eb23c60)
  • Loading branch information
cmb69 authored and remicollet committed Dec 17, 2019
1 parent 8877854 commit 2d07f00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ext/bcmath/libbcmath/src/str2num.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ bc_str2num (bc_num *num, char *str, int scale)
zero_int = FALSE;
if ( (*ptr == '+') || (*ptr == '-')) ptr++; /* Sign */
while (*ptr == '0') ptr++; /* Skip leading zeros. */
while (isdigit((int)*ptr)) ptr++, digits++; /* digits */
while (*ptr >= '0' && *ptr <= '9') ptr++, digits++; /* digits */
if (*ptr == '.') ptr++; /* decimal point */
while (isdigit((int)*ptr)) ptr++, strscale++; /* digits */
while (*ptr >= '0' && *ptr <= '9') ptr++, strscale++; /* digits */
if ((*ptr != '\0') || (digits+strscale == 0))
{
*num = bc_copy_num (BCG(_zero_));
Expand Down
13 changes: 13 additions & 0 deletions ext/bcmath/tests/bug78878.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Bug #78878 (Buffer underflow in bc_shift_addsub)
--SKIPIF--
<?php
if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
?>
--FILE--
<?php
print @bcmul("\xB26483605105519922841849335928742092", bcpowmod(2, 65535, -4e-4));
?>
--EXPECT--
bc math warning: non-zero scale in modulus
0

0 comments on commit 2d07f00

Please sign in to comment.