From 11aae0be7487bfa24173e6ce58f20a39b6c3a921 Mon Sep 17 00:00:00 2001
From: Jens de Nies
Date: Sun, 27 Dec 2020 21:15:06 +0100
Subject: [PATCH 1/7] Converted some type warnings to type errors in the
"bcmath" extension.
---
ext/bcmath/bcmath.c | 14 ++-
ext/bcmath/tests/bug80545.phpt | 16 ++++
ext/bcmath/tests/str2num_formatting.phpt | 106 ++++++++++++++++-------
3 files changed, 97 insertions(+), 39 deletions(-)
create mode 100644 ext/bcmath/tests/bug80545.phpt
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index 95b820e4c4dfd..8adf25c012086 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -135,13 +135,13 @@ static void php_str2num(bc_num *num, char *str)
if (!(p = strchr(str, '.'))) {
if (!bc_str2num(num, str, 0)) {
- php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
+ zend_type_error("bcmath function argument is not well-formed");
}
return;
}
if (!bc_str2num(num, str, strlen(p+1))) {
- php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
+ zend_type_error("bcmath function argument is not well-formed");
}
}
/* }}} */
@@ -509,12 +509,10 @@ PHP_FUNCTION(bccomp)
bc_init_num(&first);
bc_init_num(&second);
- 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");
- }
+ if (!bc_str2num(&first, ZSTR_VAL(left), scale) || !bc_str2num(&second, ZSTR_VAL(right), scale)) {
+ zend_type_error("bcmath function argument is not well-formed");
+ }
+
RETVAL_LONG(bc_compare(first, second));
bc_free_num(&first);
diff --git a/ext/bcmath/tests/bug80545.phpt b/ext/bcmath/tests/bug80545.phpt
new file mode 100644
index 0000000000000..61bd97b2aacdb
--- /dev/null
+++ b/ext/bcmath/tests/bug80545.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Bug #80545 (bcadd('a', 'a') doesn't throw an exception)
+--SKIPIF--
+
+--FILE--
+getMessage();
+}
+?>
+--EXPECT--
+bcmath function argument is not well-formed
\ No newline at end of file
diff --git a/ext/bcmath/tests/str2num_formatting.phpt b/ext/bcmath/tests/str2num_formatting.phpt
index 090dd44d53d66..1cdc39fa8ada0 100644
--- a/ext/bcmath/tests/str2num_formatting.phpt
+++ b/ext/bcmath/tests/str2num_formatting.phpt
@@ -14,12 +14,39 @@ 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 "\n";
+
+try {
+ echo bcadd(" 0", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bcadd("1e1", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bcadd("1,1", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bcadd("Hello", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bcadd("1 1", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+echo "\n";
echo bccomp("1", "2"),"\n";
echo bccomp("1.1", "2", 2),"\n";
@@ -27,11 +54,38 @@ 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");
+echo "\n";
+
+try {
+ echo bccomp(" 0", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bccomp("1e1", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bccomp("1,1", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bccomp("Hello", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bccomp("1 1", "2");
+} catch (\TypeError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
?>
--EXPECTF--
3
@@ -40,16 +94,11 @@ echo bccomp("1 1", "2");
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
+bcmath function argument is not well-formed
+bcmath function argument is not well-formed
+bcmath function argument is not well-formed
+bcmath function argument is not well-formed
+bcmath function argument is not well-formed
-1
-1
@@ -57,13 +106,8 @@ Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
-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
+bcmath function argument is not well-formed
+bcmath function argument is not well-formed
+bcmath function argument is not well-formed
+bcmath function argument is not well-formed
+bcmath function argument is not well-formed
\ No newline at end of file
From 927ee08b4f3d6c353ef7f9a2ec3c10693a6086e8 Mon Sep 17 00:00:00 2001
From: Jens de Nies
Date: Sun, 27 Dec 2020 21:23:13 +0100
Subject: [PATCH 2/7] Used tabs instead of spaces.
---
ext/bcmath/bcmath.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index 8adf25c012086..232b047739263 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -135,13 +135,13 @@ static void php_str2num(bc_num *num, char *str)
if (!(p = strchr(str, '.'))) {
if (!bc_str2num(num, str, 0)) {
- zend_type_error("bcmath function argument is not well-formed");
+ zend_type_error("bcmath function argument is not well-formed");
}
return;
}
if (!bc_str2num(num, str, strlen(p+1))) {
- zend_type_error("bcmath function argument is not well-formed");
+ zend_type_error("bcmath function argument is not well-formed");
}
}
/* }}} */
@@ -509,9 +509,9 @@ PHP_FUNCTION(bccomp)
bc_init_num(&first);
bc_init_num(&second);
- if (!bc_str2num(&first, ZSTR_VAL(left), scale) || !bc_str2num(&second, ZSTR_VAL(right), scale)) {
- zend_type_error("bcmath function argument is not well-formed");
- }
+ if (!bc_str2num(&first, ZSTR_VAL(left), scale) || !bc_str2num(&second, ZSTR_VAL(right), scale)) {
+ zend_type_error("bcmath function argument is not well-formed");
+ }
RETVAL_LONG(bc_compare(first, second));
From f64be0769ba752a9d1a5ce449e2b90c99b33ac1f Mon Sep 17 00:00:00 2001
From: Jens de Nies
Date: Tue, 29 Dec 2020 11:12:20 +0100
Subject: [PATCH 3/7] Changed TypeError to ValueError/
Signed-off-by: Jens de Nies
---
ext/bcmath/bcmath.c | 46 +++++++++++++-----------
ext/bcmath/tests/bug80545.phpt | 4 +--
ext/bcmath/tests/str2num_formatting.phpt | 40 ++++++++++-----------
3 files changed, 47 insertions(+), 43 deletions(-)
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index 232b047739263..68e9bb8067991 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -129,19 +129,19 @@ PHP_MINFO_FUNCTION(bcmath)
/* {{{ php_str2num
Convert to bc_num detecting scale */
-static void php_str2num(bc_num *num, char *str)
+static void php_str2num(bc_num *num, char *str, uint32_t arg_num)
{
char *p;
if (!(p = strchr(str, '.'))) {
if (!bc_str2num(num, str, 0)) {
- zend_type_error("bcmath function argument is not well-formed");
+ zend_argument_value_error(arg_num, "bcmath function argument is not well-formed");
}
return;
}
if (!bc_str2num(num, str, strlen(p+1))) {
- zend_type_error("bcmath function argument is not well-formed");
+ zend_argument_value_error(arg_num, "bcmath function argument is not well-formed");
}
}
/* }}} */
@@ -174,8 +174,8 @@ PHP_FUNCTION(bcadd)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+ php_str2num(&first, ZSTR_VAL(left), 1);
+ php_str2num(&second, ZSTR_VAL(right), 2);
bc_add (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -214,8 +214,8 @@ PHP_FUNCTION(bcsub)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+ php_str2num(&first, ZSTR_VAL(left), 1);
+ php_str2num(&second, ZSTR_VAL(right), 2);
bc_sub (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -254,8 +254,8 @@ PHP_FUNCTION(bcmul)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+ php_str2num(&first, ZSTR_VAL(left), 1);
+ php_str2num(&second, ZSTR_VAL(right), 2);
bc_multiply (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -294,8 +294,8 @@ PHP_FUNCTION(bcdiv)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+ php_str2num(&first, ZSTR_VAL(left), 1);
+ php_str2num(&second, ZSTR_VAL(right), 2);
switch (bc_divide(first, second, &result, scale)) {
case 0: /* OK */
@@ -341,8 +341,8 @@ PHP_FUNCTION(bcmod)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+ php_str2num(&first, ZSTR_VAL(left), 1);
+ php_str2num(&second, ZSTR_VAL(right), 2);
switch (bc_modulo(first, second, &result, scale)) {
case 0:
@@ -389,9 +389,9 @@ PHP_FUNCTION(bcpowmod)
bc_init_num(&second);
bc_init_num(&mod);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
- php_str2num(&mod, ZSTR_VAL(modulus));
+ php_str2num(&first, ZSTR_VAL(left), 1);
+ php_str2num(&second, ZSTR_VAL(right), 2);
+ php_str2num(&mod, ZSTR_VAL(modulus), 3);
if (bc_raisemod(first, second, mod, &result, scale) == SUCCESS) {
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -432,8 +432,8 @@ PHP_FUNCTION(bcpow)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+ php_str2num(&first, ZSTR_VAL(left), 1);
+ php_str2num(&second, ZSTR_VAL(right), 2);
bc_raise (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -468,7 +468,7 @@ PHP_FUNCTION(bcsqrt)
}
bc_init_num(&result);
- php_str2num(&result, ZSTR_VAL(left));
+ php_str2num(&result, ZSTR_VAL(left), 1);
if (bc_sqrt (&result, scale) != 0) {
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -509,8 +509,12 @@ PHP_FUNCTION(bccomp)
bc_init_num(&first);
bc_init_num(&second);
- if (!bc_str2num(&first, ZSTR_VAL(left), scale) || !bc_str2num(&second, ZSTR_VAL(right), scale)) {
- zend_type_error("bcmath function argument is not well-formed");
+ if (!bc_str2num(&first, ZSTR_VAL(left), scale)) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ }
+
+ if (!bc_str2num(&second, ZSTR_VAL(right), scale)) {
+ zend_argument_value_error(2, "bcmath function argument is not well-formed");
}
RETVAL_LONG(bc_compare(first, second));
diff --git a/ext/bcmath/tests/bug80545.phpt b/ext/bcmath/tests/bug80545.phpt
index 61bd97b2aacdb..cfa1ec2ec6ae2 100644
--- a/ext/bcmath/tests/bug80545.phpt
+++ b/ext/bcmath/tests/bug80545.phpt
@@ -8,9 +8,9 @@ if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
getMessage();
}
?>
--EXPECT--
-bcmath function argument is not well-formed
\ No newline at end of file
+bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
\ No newline at end of file
diff --git a/ext/bcmath/tests/str2num_formatting.phpt b/ext/bcmath/tests/str2num_formatting.phpt
index 1cdc39fa8ada0..a6e2935f77684 100644
--- a/ext/bcmath/tests/str2num_formatting.phpt
+++ b/ext/bcmath/tests/str2num_formatting.phpt
@@ -18,31 +18,31 @@ echo "\n";
try {
echo bcadd(" 0", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
echo bcadd("1e1", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
echo bcadd("1,1", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
echo bcadd("Hello", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
echo bcadd("1 1", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
@@ -58,31 +58,31 @@ echo "\n";
try {
echo bccomp(" 0", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
echo bccomp("1e1", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
echo bccomp("1,1", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
echo bccomp("Hello", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
echo bccomp("1 1", "2");
-} catch (\TypeError $e) {
+} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
@@ -94,11 +94,11 @@ try {
2
2
-bcmath function argument is not well-formed
-bcmath function argument is not well-formed
-bcmath function argument is not well-formed
-bcmath function argument is not well-formed
-bcmath function argument is not well-formed
+bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
+bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
+bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
+bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
+bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
-1
-1
@@ -106,8 +106,8 @@ bcmath function argument is not well-formed
-1
-1
-bcmath function argument is not well-formed
-bcmath function argument is not well-formed
-bcmath function argument is not well-formed
-bcmath function argument is not well-formed
-bcmath function argument is not well-formed
\ No newline at end of file
+bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
+bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
+bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
+bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
+bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
\ No newline at end of file
From 6ca7f944c7aa19ae38ee59471211b9eab60eea14 Mon Sep 17 00:00:00 2001
From: Jens de Nies
Date: Mon, 4 Jan 2021 22:06:09 +0100
Subject: [PATCH 4/7] Added an early return to various bcmath functions.
Signed-off-by: Jens de Nies
---
ext/bcmath/bcmath.c | 117 ++++++++++++++++++++++++++++++++++----------
1 file changed, 91 insertions(+), 26 deletions(-)
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index 68e9bb8067991..d16dd1d017319 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -129,20 +129,23 @@ PHP_MINFO_FUNCTION(bcmath)
/* {{{ php_str2num
Convert to bc_num detecting scale */
-static void php_str2num(bc_num *num, char *str, uint32_t arg_num)
+static zend_result php_str2num(bc_num *num, char *str)
{
char *p;
if (!(p = strchr(str, '.'))) {
if (!bc_str2num(num, str, 0)) {
- zend_argument_value_error(arg_num, "bcmath function argument is not well-formed");
+ return FAILURE;
}
- return;
+
+ return SUCCESS;
}
if (!bc_str2num(num, str, strlen(p+1))) {
- zend_argument_value_error(arg_num, "bcmath function argument is not well-formed");
+ return FAILURE;
}
+
+ return SUCCESS;
}
/* }}} */
@@ -174,15 +177,23 @@ PHP_FUNCTION(bcadd)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left), 1);
- php_str2num(&second, ZSTR_VAL(right), 2);
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
bc_add (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
- return;
}
/* }}} */
@@ -214,15 +225,23 @@ PHP_FUNCTION(bcsub)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left), 1);
- php_str2num(&second, ZSTR_VAL(right), 2);
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
bc_sub (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
- return;
}
/* }}} */
@@ -254,15 +273,23 @@ PHP_FUNCTION(bcmul)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left), 1);
- php_str2num(&second, ZSTR_VAL(right), 2);
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
bc_multiply (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
- return;
}
/* }}} */
@@ -294,8 +321,16 @@ PHP_FUNCTION(bcdiv)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left), 1);
- php_str2num(&second, ZSTR_VAL(right), 2);
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
switch (bc_divide(first, second, &result, scale)) {
case 0: /* OK */
@@ -309,7 +344,6 @@ PHP_FUNCTION(bcdiv)
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
- return;
}
/* }}} */
@@ -341,8 +375,16 @@ PHP_FUNCTION(bcmod)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left), 1);
- php_str2num(&second, ZSTR_VAL(right), 2);
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
switch (bc_modulo(first, second, &result, scale)) {
case 0:
@@ -389,9 +431,21 @@ PHP_FUNCTION(bcpowmod)
bc_init_num(&second);
bc_init_num(&mod);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left), 1);
- php_str2num(&second, ZSTR_VAL(right), 2);
- php_str2num(&mod, ZSTR_VAL(modulus), 3);
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
+ if (php_str2num(&mod, ZSTR_VAL(modulus)) == FAILURE) {
+ zend_argument_value_error(3, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
if (bc_raisemod(first, second, mod, &result, scale) == SUCCESS) {
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -432,8 +486,17 @@ PHP_FUNCTION(bcpow)
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left), 1);
- php_str2num(&second, ZSTR_VAL(right), 2);
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
+
bc_raise (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -468,7 +531,11 @@ PHP_FUNCTION(bcsqrt)
}
bc_init_num(&result);
- php_str2num(&result, ZSTR_VAL(left), 1);
+
+ if (php_str2num(&result, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
+ }
if (bc_sqrt (&result, scale) != 0) {
RETVAL_STR(bc_num2str_ex(result, scale));
@@ -477,7 +544,6 @@ PHP_FUNCTION(bcsqrt)
}
bc_free_num(&result);
- return;
}
/* }}} */
@@ -521,7 +587,6 @@ PHP_FUNCTION(bccomp)
bc_free_num(&first);
bc_free_num(&second);
- return;
}
/* }}} */
From 214f9d90e0384727e34f347ac348b83241d1fdc7 Mon Sep 17 00:00:00 2001
From: Jens de Nies
Date: Mon, 4 Jan 2021 22:26:19 +0100
Subject: [PATCH 5/7] Added an early return to various bcmath functions.
Signed-off-by: Jens de Nies
---
ext/bcmath/bcmath.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index d16dd1d017319..0805e3a21ec1e 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -577,10 +577,12 @@ PHP_FUNCTION(bccomp)
if (!bc_str2num(&first, ZSTR_VAL(left), scale)) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
}
if (!bc_str2num(&second, ZSTR_VAL(right), scale)) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ RETURN_THROWS();
}
RETVAL_LONG(bc_compare(first, second));
From 537403313c057d9ffb6514f0409f3e228a2e3e64 Mon Sep 17 00:00:00 2001
From: Jens de Nies
Date: Wed, 6 Jan 2021 23:28:26 +0100
Subject: [PATCH 6/7] Added goto cleanups.
Signed-off-by: Jens de Nies
---
ext/bcmath/bcmath.c | 108 ++++++++++++++++++++-------------
ext/bcmath/tests/bug80545.phpt | 13 +++-
2 files changed, 76 insertions(+), 45 deletions(-)
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index 0805e3a21ec1e..3c2c91e1b3d2a 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -180,20 +180,23 @@ PHP_FUNCTION(bcadd)
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
bc_add (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
+
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
@@ -228,20 +231,23 @@ PHP_FUNCTION(bcsub)
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
bc_sub (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
+
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
@@ -276,20 +282,23 @@ PHP_FUNCTION(bcmul)
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
bc_multiply (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
+
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
@@ -324,12 +333,12 @@ PHP_FUNCTION(bcdiv)
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
switch (bc_divide(first, second, &result, scale)) {
@@ -341,9 +350,11 @@ PHP_FUNCTION(bcdiv)
break;
}
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
@@ -378,12 +389,12 @@ PHP_FUNCTION(bcmod)
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
switch (bc_modulo(first, second, &result, scale)) {
@@ -395,9 +406,11 @@ PHP_FUNCTION(bcmod)
break;
}
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
@@ -434,27 +447,29 @@ PHP_FUNCTION(bcpowmod)
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (php_str2num(&mod, ZSTR_VAL(modulus)) == FAILURE) {
zend_argument_value_error(3, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (bc_raisemod(first, second, mod, &result, scale) == SUCCESS) {
RETVAL_STR(bc_num2str_ex(result, scale));
}
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&mod);
- bc_free_num(&result);
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&mod);
+ bc_free_num(&result);
+ };
}
/* }}} */
@@ -489,20 +504,23 @@ PHP_FUNCTION(bcpow)
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
bc_raise (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
+
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
@@ -534,7 +552,7 @@ PHP_FUNCTION(bcsqrt)
if (php_str2num(&result, ZSTR_VAL(left)) == FAILURE) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (bc_sqrt (&result, scale) != 0) {
@@ -543,7 +561,9 @@ PHP_FUNCTION(bcsqrt)
zend_argument_value_error(1, "must be greater than or equal to 0");
}
- bc_free_num(&result);
+ cleanup: {
+ bc_free_num(&result);
+ };
}
/* }}} */
@@ -577,18 +597,20 @@ PHP_FUNCTION(bccomp)
if (!bc_str2num(&first, ZSTR_VAL(left), scale)) {
zend_argument_value_error(1, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
if (!bc_str2num(&second, ZSTR_VAL(right), scale)) {
zend_argument_value_error(2, "bcmath function argument is not well-formed");
- RETURN_THROWS();
+ goto cleanup;
}
RETVAL_LONG(bc_compare(first, second));
- bc_free_num(&first);
- bc_free_num(&second);
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ };
}
/* }}} */
diff --git a/ext/bcmath/tests/bug80545.phpt b/ext/bcmath/tests/bug80545.phpt
index cfa1ec2ec6ae2..b3acee183243c 100644
--- a/ext/bcmath/tests/bug80545.phpt
+++ b/ext/bcmath/tests/bug80545.phpt
@@ -1,16 +1,25 @@
--TEST--
-Bug #80545 (bcadd('a', 'a') doesn't throw an exception)
+Bug #80545 (bcadd('a', 'a') and bcadd('1', 'a') doesn't throw an exception)
--SKIPIF--
--FILE--
getMessage() . PHP_EOL;
+}
+
+try {
+ bcadd('1', 'a');
} catch (\ValueError $e) {
echo $e->getMessage();
}
+
?>
--EXPECT--
-bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
\ No newline at end of file
+bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
+bcadd(): Argument #2 ($num2) bcmath function argument is not well-formed
\ No newline at end of file
From 1f4c9c78fbcb26fa9e5fc5074b175bbb53acbf0d Mon Sep 17 00:00:00 2001
From: Jens de Nies
Date: Fri, 8 Jan 2021 19:51:32 +0100
Subject: [PATCH 7/7] Fixed the exception message.
Signed-off-by: Jens de Nies
---
ext/bcmath/bcmath.c | 36 ++++++++++++------------
ext/bcmath/tests/bug80545.phpt | 4 +--
ext/bcmath/tests/str2num_formatting.phpt | 20 ++++++-------
3 files changed, 30 insertions(+), 30 deletions(-)
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index 3c2c91e1b3d2a..870749af502db 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -179,12 +179,12 @@ PHP_FUNCTION(bcadd)
bc_init_num(&result);
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
- zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
goto cleanup;
}
@@ -230,12 +230,12 @@ PHP_FUNCTION(bcsub)
bc_init_num(&result);
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
- zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
goto cleanup;
}
@@ -281,12 +281,12 @@ PHP_FUNCTION(bcmul)
bc_init_num(&result);
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
- zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
goto cleanup;
}
@@ -332,12 +332,12 @@ PHP_FUNCTION(bcdiv)
bc_init_num(&result);
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
- zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
goto cleanup;
}
@@ -388,12 +388,12 @@ PHP_FUNCTION(bcmod)
bc_init_num(&result);
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
- zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
goto cleanup;
}
@@ -446,17 +446,17 @@ PHP_FUNCTION(bcpowmod)
bc_init_num(&result);
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
- zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
goto cleanup;
}
if (php_str2num(&mod, ZSTR_VAL(modulus)) == FAILURE) {
- zend_argument_value_error(3, "bcmath function argument is not well-formed");
+ zend_argument_value_error(3, "is not well-formed");
goto cleanup;
}
@@ -503,12 +503,12 @@ PHP_FUNCTION(bcpow)
bc_init_num(&result);
if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
- zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
goto cleanup;
}
@@ -551,7 +551,7 @@ PHP_FUNCTION(bcsqrt)
bc_init_num(&result);
if (php_str2num(&result, ZSTR_VAL(left)) == FAILURE) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
@@ -596,12 +596,12 @@ PHP_FUNCTION(bccomp)
bc_init_num(&second);
if (!bc_str2num(&first, ZSTR_VAL(left), scale)) {
- zend_argument_value_error(1, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
if (!bc_str2num(&second, ZSTR_VAL(right), scale)) {
- zend_argument_value_error(2, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
goto cleanup;
}
diff --git a/ext/bcmath/tests/bug80545.phpt b/ext/bcmath/tests/bug80545.phpt
index b3acee183243c..680c4c0631570 100644
--- a/ext/bcmath/tests/bug80545.phpt
+++ b/ext/bcmath/tests/bug80545.phpt
@@ -21,5 +21,5 @@ try {
?>
--EXPECT--
-bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
-bcadd(): Argument #2 ($num2) bcmath function argument is not well-formed
\ No newline at end of file
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #2 ($num2) is not well-formed
\ No newline at end of file
diff --git a/ext/bcmath/tests/str2num_formatting.phpt b/ext/bcmath/tests/str2num_formatting.phpt
index a6e2935f77684..83e633bdaf1b4 100644
--- a/ext/bcmath/tests/str2num_formatting.phpt
+++ b/ext/bcmath/tests/str2num_formatting.phpt
@@ -94,11 +94,11 @@ try {
2
2
-bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
-bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
-bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
-bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
-bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
-1
-1
@@ -106,8 +106,8 @@ bcadd(): Argument #1 ($num1) bcmath function argument is not well-formed
-1
-1
-bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
-bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
-bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
-bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
-bccomp(): Argument #1 ($num1) bcmath function argument is not well-formed
\ No newline at end of file
+bccomp(): Argument #1 ($num1) is not well-formed
+bccomp(): Argument #1 ($num1) is not well-formed
+bccomp(): Argument #1 ($num1) is not well-formed
+bccomp(): Argument #1 ($num1) is not well-formed
+bccomp(): Argument #1 ($num1) is not well-formed
\ No newline at end of file