summaryrefslogtreecommitdiff
path: root/ext/bcmath/tests
diff options
context:
space:
mode:
authorVladyslav Startsev <17382248+vladyslavstartsev@users.noreply.github.com>2020-04-25 02:18:09 +0300
committerNikita Popov <nikita.ppv@gmail.com>2020-04-27 11:53:26 +0200
commitb64aee97069fc77c141c787e5408d9e12f5971b4 (patch)
tree645099cfe50b3bbcff19d83374e4f7cc94d833f5 /ext/bcmath/tests
parent48a34bc1202e9664121c9e9aa004c79ac71af3f5 (diff)
downloadphp-git-b64aee97069fc77c141c787e5408d9e12f5971b4.tar.gz
Ensure bcmath scale is between 0 and INT_MAX
Make sure bcmatch scale is between 0 and INT_MAX, both for the ini setting, and all the functions accepting a scale argument. A ValueError is thrown if a function argument is out of range. Closes GH-5455.
Diffstat (limited to 'ext/bcmath/tests')
-rw-r--r--ext/bcmath/tests/bcscale_variation001.phpt9
-rw-r--r--ext/bcmath/tests/bug60377.phpt10
-rw-r--r--ext/bcmath/tests/bug72093.phpt10
-rw-r--r--ext/bcmath/tests/negative_scale.phpt70
4 files changed, 91 insertions, 8 deletions
diff --git a/ext/bcmath/tests/bcscale_variation001.phpt b/ext/bcmath/tests/bcscale_variation001.phpt
index 51c6767bd4..0718d724c2 100644
--- a/ext/bcmath/tests/bcscale_variation001.phpt
+++ b/ext/bcmath/tests/bcscale_variation001.phpt
@@ -1,13 +1,18 @@
--TEST--
-bcscale() with negative argument
+bcscale() fails with negative argument
--SKIPIF--
<?php if(!extension_loaded("bcmath")) print "skip"; ?>
--INI--
bcmath.scale=0
--FILE--
<?php
-bcscale(-4);
echo bcdiv("20.56", "4");
+try {
+ bcscale(-4);
+} catch (\ValueError $e) {
+ echo \PHP_EOL . $e->getMessage() . \PHP_EOL;
+}
?>
--EXPECT--
5
+bcscale(): Argument #1 ($scale) must be between 0 and 2147483647
diff --git a/ext/bcmath/tests/bug60377.phpt b/ext/bcmath/tests/bug60377.phpt
index eb140d92cf..6caf7d4661 100644
--- a/ext/bcmath/tests/bug60377.phpt
+++ b/ext/bcmath/tests/bug60377.phpt
@@ -5,10 +5,14 @@ bcscale related problem on 64bits platforms
if (PHP_INT_SIZE != 8) die("skip: 64-bit only"); ?>
--FILE--
<?php
-$var48 = bcscale(634314234334311);
+try {
+ $var48 = bcscale(634314234334311);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
$var67 = bcsqrt(0);
$var414 = bcadd(0,-1,10);
-die('ALIVE');
?>
+
--EXPECT--
-ALIVE
+bcscale(): Argument #1 ($scale) must be between 0 and 2147483647
diff --git a/ext/bcmath/tests/bug72093.phpt b/ext/bcmath/tests/bug72093.phpt
index 235a4e04a3..7111bf6e3a 100644
--- a/ext/bcmath/tests/bug72093.phpt
+++ b/ext/bcmath/tests/bug72093.phpt
@@ -1,16 +1,20 @@
--TEST--
-Bug 72093: bcpowmod accepts negative scale and corrupts _one_ definition
+Bug 72093: bcpowmod fails on negative scale and corrupts _one_ definition
--SKIPIF--
<?php
if(!extension_loaded("bcmath")) print "skip";
?>
--FILE--
<?php
-var_dump(bcpowmod(1, 0, 128, -200));
+try {
+ var_dump(bcpowmod(1, 0, 128, -200));
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
var_dump(bcpowmod(1, 1.2, 1, 1));
?>
--EXPECTF--
-string(1) "1"
+bcpowmod(): Argument #4 ($scale) must be between 0 and 2147483647
Warning: bcpowmod(): Non-zero scale in exponent in %s on line %d
string(3) "0.0"
diff --git a/ext/bcmath/tests/negative_scale.phpt b/ext/bcmath/tests/negative_scale.phpt
new file mode 100644
index 0000000000..96d1e1b600
--- /dev/null
+++ b/ext/bcmath/tests/negative_scale.phpt
@@ -0,0 +1,70 @@
+--TEST--
+all errors on negative scale
+--SKIPIF--
+<?php if(!extension_loaded("bcmath")) print "skip"; ?>
+--INI--
+bcmath.scale=0
+--FILE--
+<?php
+try {
+ bcadd('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcsub('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcmul('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcdiv('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcmod('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcpowmod('1', '2', '3', -9);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcpow('1', '2', -1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcsqrt('9', -1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bccomp('1', '2', -1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcscale(-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+?>
+--EXPECT--
+bcadd(): Argument #3 ($scale) must be between 0 and 2147483647
+bcsub(): Argument #3 ($scale) must be between 0 and 2147483647
+bcmul(): Argument #3 ($scale) must be between 0 and 2147483647
+bcdiv(): Argument #3 ($scale) must be between 0 and 2147483647
+bcmod(): Argument #3 ($scale) must be between 0 and 2147483647
+bcpowmod(): Argument #4 ($scale) must be between 0 and 2147483647
+bcpow(): Argument #3 ($scale) must be between 0 and 2147483647
+bcsqrt(): Argument #2 ($scale) must be between 0 and 2147483647
+bccomp(): Argument #3 ($scale) must be between 0 and 2147483647
+bcscale(): Argument #1 ($scale) must be between 0 and 2147483647