Skip to content

Commit 3d51ec2

Browse files
committed
implement bcmath fix to warn about non well formatted arguments
1 parent ec5f7df commit 3d51ec2

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

ext/bcmath/bcmath.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,15 @@ static void php_str2num(bc_num *num, char *str)
198198
char *p;
199199

200200
if (!(p = strchr(str, '.'))) {
201-
bc_str2num(num, str, 0);
201+
if (bc_str2num(num, str, 0)) {
202+
php_error_docref(NULL, E_WARNING, "bcmath function argument is not well formatted");
203+
}
202204
return;
203205
}
204206

205-
bc_str2num(num, str, strlen(p+1));
207+
if (bc_str2num(num, str, strlen(p+1))) {
208+
php_error_docref(NULL, E_WARNING, "bcmath function argument is not well formatted");
209+
}
206210
}
207211
/* }}} */
208212

@@ -527,8 +531,12 @@ PHP_FUNCTION(bccomp)
527531
bc_init_num(&first);
528532
bc_init_num(&second);
529533

530-
bc_str2num(&first, ZSTR_VAL(left), scale);
531-
bc_str2num(&second, ZSTR_VAL(right), scale);
534+
if (bc_str2num(&first, ZSTR_VAL(left), scale)) {
535+
php_error_docref(NULL, E_WARNING, "bcmath function argument is not well formatted");
536+
}
537+
if (bc_str2num(&second, ZSTR_VAL(right), scale)) {
538+
php_error_docref(NULL, E_WARNING, "bcmath function argument is not well formatted");
539+
}
532540
RETVAL_LONG(bc_compare(first, second));
533541

534542
bc_free_num(&first);

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ _PROTOTYPE(bc_num bc_copy_num, (bc_num num));
108108

109109
_PROTOTYPE(void bc_init_num, (bc_num *num));
110110

111-
_PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale));
111+
_PROTOTYPE(int bc_str2num, (bc_num *num, char *str, int scale));
112112

113113
_PROTOTYPE(zend_string *bc_num2str_ex, (bc_num num, int scale));
114114

ext/bcmath/libbcmath/src/str2num.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
/* Convert strings to bc numbers. Base 10 only.*/
4141

42-
void
42+
int
4343
bc_str2num (bc_num *num, char *str, int scale)
4444
{
4545
int digits, strscale;
@@ -62,7 +62,7 @@ bc_str2num (bc_num *num, char *str, int scale)
6262
if ((*ptr != '\0') || (digits+strscale == 0))
6363
{
6464
*num = bc_copy_num (BCG(_zero_));
65-
return;
65+
return str[0] != '0' || str[1] != '\0';
6666
}
6767

6868
/* Adjust numbers and allocate storage and initialize fields. */
@@ -107,4 +107,6 @@ bc_str2num (bc_num *num, char *str, int scale)
107107

108108
if (bc_is_zero (*num))
109109
(*num)->n_sign = PLUS;
110+
111+
return 0;
110112
}

ext/bcmath/tests/bug60377.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ if (PHP_INT_SIZE != 8) die("skip: 64-bit only"); ?>
66
--FILE--
77
<?php
88
$var48 = bcscale(634314234334311);
9-
$var67 = bcsqrt(false);
10-
$var414 = bcadd(false,null,10);
9+
$var67 = bcsqrt(0);
10+
$var414 = bcadd(0,-1,10);
1111
die('ALIVE');
1212
?>
1313
--EXPECT--

ext/bcmath/tests/bug72093.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if(!extension_loaded("bcmath")) print "skip";
66
?>
77
--FILE--
88
<?php
9-
var_dump(bcpowmod(1, "A", 128, -200));
9+
var_dump(bcpowmod(1, 0, 128, -200));
1010
var_dump(bcpowmod(1, 1.2, 1, 1));
1111
?>
1212
--EXPECTF--

0 commit comments

Comments
 (0)