Skip to content

Commit a07d422

Browse files
vv12131415nikiccmb69
committed
Warn about non well-formed arguments in bcmath
Co-Authored-By: Nikita Popov <nikita.ppv@googlemail.com> Co-Authored-By: Christoph M. Becker <cmbecker69@gmx.de>
1 parent 3f19f51 commit a07d422

File tree

7 files changed

+93
-10
lines changed

7 files changed

+93
-10
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ PHP 7.4 UPGRADE NOTES
4141
consistently disallowed now. Previously this worked if the right hand side
4242
was a simple (CV) variable and did not occur as part of the list().
4343

44+
- BCMath:
45+
. BCMath functions will now warn if a non well-formed number is passed, such
46+
as "32foo". The argument will be interpreted as zero (as before).
47+
4448
- Curl:
4549
. Attempting to serialize a CURLFile class will now generate an exception.
4650
Previously the exception was only thrown on unserialization.

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-formed");
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-formed");
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-formed");
536+
}
537+
if (!bc_str2num(&second, ZSTR_VAL(right), scale)) {
538+
php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
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 *ptr == '\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 1;
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--
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--TEST--
2+
bcmath lib arguments formatting
3+
--DESCRIPTION--
4+
1 and 2 argument of bcadd/bcsub/bcmul/bcdiv/bcmod/bcpowmod/bcpow/bccomp (last one works different then others internally);
5+
1 argument of bcsqrt
6+
All of the name above must be well-formed
7+
--SKIPIF--
8+
<?php if(!extension_loaded("bcmath")) print "skip"; ?>
9+
--FILE--
10+
<?php
11+
echo bcadd("1", "2"),"\n";
12+
echo bcadd("1.1", "2", 2),"\n";
13+
echo bcadd("", "2", 2),"\n";
14+
echo bcadd("+0", "2"), "\n";
15+
echo bcadd("-0", "2"), "\n";
16+
17+
echo bcadd(" 0", "2");
18+
echo bcadd("1e1", "2");
19+
echo bcadd("1,1", "2");
20+
echo bcadd("Hello", "2");
21+
echo bcadd("1 1", "2");
22+
echo "\n", "\n";
23+
24+
echo bccomp("1", "2"),"\n";
25+
echo bccomp("1.1", "2", 2),"\n";
26+
echo bccomp("", "2"),"\n";
27+
echo bccomp("+0", "2"), "\n";
28+
echo bccomp("-0", "2"), "\n";
29+
30+
echo bccomp(" 0", "2");
31+
echo bccomp("1e1", "2");
32+
echo bccomp("1,1", "2");
33+
echo bccomp("Hello", "2");
34+
echo bccomp("1 1", "2");
35+
?>
36+
--EXPECTF--
37+
3
38+
3.10
39+
2.00
40+
2
41+
2
42+
43+
Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
44+
2
45+
Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
46+
2
47+
Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
48+
2
49+
Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
50+
2
51+
Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
52+
2
53+
54+
-1
55+
-1
56+
-1
57+
-1
58+
-1
59+
60+
Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
61+
-1
62+
Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
63+
-1
64+
Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
65+
-1
66+
Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
67+
-1
68+
Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
69+
-1

0 commit comments

Comments
 (0)