com php-src: Warn about non well-formed arguments in bcmath: UPGRADING ext/bcmath/bcmath.c ext/bcmath/libbcmath/src/bcmath.h
ext/bcmath/libbcmath/src/str2num.c ext/bcmath/tests/bug60377.phpt ext/bcmath/tests/bug72093.phpt ext/bcmath/tests/str2num_for matting.phpt

From: Date: Tue, 14 May 2019 13:04:21 +0000
Subject: com php-src: Warn about non well-formed arguments in bcmath: UPGRADING ext/bcmath/bcmath.c ext/bcmath/libbcmath/src/bcmath.h
ext/bcmath/libbcmath/src/str2num.c ext/bcmath/tests/bug60377.phpt ext/bcmath/tests/bug72093.phpt ext/bcmath/tests/str2num_for matting.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-106190@lists.php.net to get a copy of this message
Commit: a07d422ade48e875740a6733543179e7f67a573e Author: vladyslavstartsev <vladyslavstartsev@gmail.com> Tue, 30 Apr 2019 17:33:04 +0300 Committer: Nikita Popov <nikita.ppv@gmail.com> Tue, 14 May 2019 15:04:21 +0200 Parents: 3f19f5112a7be3e4aa7ab1704d25de54645be373 Branches: PHP-7.4 master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=a07d422ade48e875740a6733543179e7f67a573e Log: 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> Changed paths: M UPGRADING M ext/bcmath/bcmath.c M ext/bcmath/libbcmath/src/bcmath.h M ext/bcmath/libbcmath/src/str2num.c M ext/bcmath/tests/bug60377.phpt M ext/bcmath/tests/bug72093.phpt A ext/bcmath/tests/str2num_formatting.phpt Diff: diff --git a/UPGRADING b/UPGRADING index d5f904e..47a80a7 100644 --- a/UPGRADING +++ b/UPGRADING @@ -41,6 +41,10 @@ PHP 7.4 UPGRADE NOTES consistently disallowed now. Previously this worked if the right hand side was a simple (CV) variable and did not occur as part of the list(). +- BCMath: + . BCMath functions will now warn if a non well-formed number is passed, such + as "32foo". The argument will be interpreted as zero (as before). + - Curl: . Attempting to serialize a CURLFile class will now generate an exception. Previously the exception was only thrown on unserialization. diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 33be846..ecfce4f 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -198,11 +198,15 @@ static void php_str2num(bc_num *num, char *str) char *p; if (!(p = strchr(str, '.'))) { - bc_str2num(num, str, 0); + if (!bc_str2num(num, str, 0)) { + php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed"); + } return; } - bc_str2num(num, str, strlen(p+1)); + if (!bc_str2num(num, str, strlen(p+1))) { + php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed"); + } } /* }}} */ @@ -527,8 +531,12 @@ PHP_FUNCTION(bccomp) bc_init_num(&first); bc_init_num(&second); - bc_str2num(&first, ZSTR_VAL(left), scale); - bc_str2num(&second, ZSTR_VAL(right), scale); + if (!bc_str2num(&first, ZSTR_VAL(left), scale)) { + php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed"); + } + if (!bc_str2num(&second, ZSTR_VAL(right), scale)) { + php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed"); + } RETVAL_LONG(bc_compare(first, second)); bc_free_num(&first); diff --git a/ext/bcmath/libbcmath/src/bcmath.h b/ext/bcmath/libbcmath/src/bcmath.h index cf6f854...becba7e 100644 --- a/ext/bcmath/libbcmath/src/bcmath.h +++ b/ext/bcmath/libbcmath/src/bcmath.h @@ -108,7 +108,7 @@ _PROTOTYPE(bc_num bc_copy_num, (bc_num num)); _PROTOTYPE(void bc_init_num, (bc_num *num)); -_PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale)); +_PROTOTYPE(int bc_str2num, (bc_num *num, char *str, int scale)); _PROTOTYPE(zend_string *bc_num2str_ex, (bc_num num, int scale)); diff --git a/ext/bcmath/libbcmath/src/str2num.c b/ext/bcmath/libbcmath/src/str2num.c index 0ea37d8..f2d6a73 100644 --- a/ext/bcmath/libbcmath/src/str2num.c +++ b/ext/bcmath/libbcmath/src/str2num.c @@ -39,7 +39,7 @@ /* Convert strings to bc numbers. Base 10 only.*/ -void +int bc_str2num (bc_num *num, char *str, int scale) { int digits, strscale; @@ -62,7 +62,7 @@ bc_str2num (bc_num *num, char *str, int scale) if ((*ptr != '\0') || (digits+strscale == 0)) { *num = bc_copy_num (BCG(_zero_)); - return; + return *ptr == '\0'; } /* Adjust numbers and allocate storage and initialize fields. */ @@ -107,4 +107,6 @@ bc_str2num (bc_num *num, char *str, int scale) if (bc_is_zero (*num)) (*num)->n_sign = PLUS; + + return 1; } diff --git a/ext/bcmath/tests/bug60377.phpt b/ext/bcmath/tests/bug60377.phpt index 929790d..eb140d9 100644 --- a/ext/bcmath/tests/bug60377.phpt +++ b/ext/bcmath/tests/bug60377.phpt @@ -6,8 +6,8 @@ if (PHP_INT_SIZE != 8) die("skip: 64-bit only"); ?> --FILE-- <?php $var48 = bcscale(634314234334311); -$var67 = bcsqrt(false); -$var414 = bcadd(false,null,10); +$var67 = bcsqrt(0); +$var414 = bcadd(0,-1,10); die('ALIVE'); ?> --EXPECT-- diff --git a/ext/bcmath/tests/bug72093.phpt b/ext/bcmath/tests/bug72093.phpt index 3aca87a..4295384 100644 --- a/ext/bcmath/tests/bug72093.phpt +++ b/ext/bcmath/tests/bug72093.phpt @@ -6,7 +6,7 @@ if(!extension_loaded("bcmath")) print "skip"; ?> --FILE-- <?php -var_dump(bcpowmod(1, "A", 128, -200)); +var_dump(bcpowmod(1, 0, 128, -200)); var_dump(bcpowmod(1, 1.2, 1, 1)); ?> --EXPECTF-- diff --git a/ext/bcmath/tests/str2num_formatting.phpt b/ext/bcmath/tests/str2num_formatting.phpt new file mode 100644 index 0000000..090dd44 --- /dev/null +++ b/ext/bcmath/tests/str2num_formatting.phpt @@ -0,0 +1,69 @@ +--TEST-- +bcmath lib arguments formatting +--DESCRIPTION-- +1 and 2 argument of bcadd/bcsub/bcmul/bcdiv/bcmod/bcpowmod/bcpow/bccomp (last one works different then others internally); +1 argument of bcsqrt +All of the name above must be well-formed +--SKIPIF-- +<?php if(!extension_loaded("bcmath")) print "skip"; ?> +--FILE-- +<?php +echo bcadd("1", "2"),"\n"; +echo bcadd("1.1", "2", 2),"\n"; +echo bcadd("", "2", 2),"\n"; +echo bcadd("+0", "2"), "\n"; +echo bcadd("-0", "2"), "\n"; + +echo bcadd(" 0", "2"); +echo bcadd("1e1", "2"); +echo bcadd("1,1", "2"); +echo bcadd("Hello", "2"); +echo bcadd("1 1", "2"); +echo "\n", "\n"; + +echo bccomp("1", "2"),"\n"; +echo bccomp("1.1", "2", 2),"\n"; +echo bccomp("", "2"),"\n"; +echo bccomp("+0", "2"), "\n"; +echo bccomp("-0", "2"), "\n"; + +echo bccomp(" 0", "2"); +echo bccomp("1e1", "2"); +echo bccomp("1,1", "2"); +echo bccomp("Hello", "2"); +echo bccomp("1 1", "2"); +?> +--EXPECTF-- +3 +3.10 +2.00 +2 +2 + +Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d +2 +Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d +2 +Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d +2 +Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d +2 +Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d +2 + +-1 +-1 +-1 +-1 +-1 + +Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d +-1 +Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d +-1 +Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d +-1 +Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d +-1 +Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d +-1 \ No newline at end of file

« previous php.cvs (#106190) next »