Promote some warnings in BCMath to Errors

Also do a bit of refactoring at the same time.

Closes GH-6105
This commit is contained in:
George Peter Banyard 2020-09-11 17:40:06 +02:00
parent f29bfc0bd8
commit cd05b56a6f
18 changed files with 99 additions and 152 deletions

View File

@ -393,16 +393,8 @@ PHP_FUNCTION(bcpowmod)
php_str2num(&second, ZSTR_VAL(right)); php_str2num(&second, ZSTR_VAL(right));
php_str2num(&mod, ZSTR_VAL(modulus)); php_str2num(&mod, ZSTR_VAL(modulus));
switch (bc_raisemod(first, second, mod, &result, scale)) { if (bc_raisemod(first, second, mod, &result, scale) == SUCCESS) {
case 0: RETVAL_STR(bc_num2str_ex(result, scale));
RETVAL_STR(bc_num2str_ex(result, scale));
break;
case -1:
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
break;
case -2:
zend_argument_value_error(2, "must be greater than 0");
break;
} }
bc_free_num(&first); bc_free_num(&first);
@ -481,7 +473,7 @@ PHP_FUNCTION(bcsqrt)
if (bc_sqrt (&result, scale) != 0) { if (bc_sqrt (&result, scale) != 0) {
RETVAL_STR(bc_num2str_ex(result, scale)); RETVAL_STR(bc_num2str_ex(result, scale));
} else { } else {
zend_value_error("Square root of negative number"); zend_argument_value_error(1, "must be greater than or equal to 0");
} }
bc_free_num(&result); bc_free_num(&result);

View File

@ -5,7 +5,7 @@ PHP_ARG_ENABLE([bcmath],
if test "$PHP_BCMATH" != "no"; then if test "$PHP_BCMATH" != "no"; then
PHP_NEW_EXTENSION(bcmath, bcmath.c \ PHP_NEW_EXTENSION(bcmath, bcmath.c \
libbcmath/src/add.c libbcmath/src/div.c libbcmath/src/init.c libbcmath/src/neg.c libbcmath/src/outofmem.c libbcmath/src/raisemod.c libbcmath/src/sub.c \ libbcmath/src/add.c libbcmath/src/div.c libbcmath/src/init.c libbcmath/src/neg.c libbcmath/src/raisemod.c libbcmath/src/sub.c \
libbcmath/src/compare.c libbcmath/src/divmod.c libbcmath/src/int2num.c libbcmath/src/num2long.c libbcmath/src/output.c libbcmath/src/recmul.c \ libbcmath/src/compare.c libbcmath/src/divmod.c libbcmath/src/int2num.c libbcmath/src/num2long.c libbcmath/src/output.c libbcmath/src/recmul.c \
libbcmath/src/sqrt.c libbcmath/src/zero.c libbcmath/src/debug.c libbcmath/src/doaddsub.c libbcmath/src/nearzero.c libbcmath/src/num2str.c libbcmath/src/raise.c \ libbcmath/src/sqrt.c libbcmath/src/zero.c libbcmath/src/debug.c libbcmath/src/doaddsub.c libbcmath/src/nearzero.c libbcmath/src/num2str.c libbcmath/src/raise.c \
libbcmath/src/rmzero.c libbcmath/src/str2num.c, libbcmath/src/rmzero.c libbcmath/src/str2num.c,

View File

@ -5,7 +5,7 @@ ARG_ENABLE("bcmath", "bc style precision math functions", "yes");
if (PHP_BCMATH == "yes") { if (PHP_BCMATH == "yes") {
EXTENSION("bcmath", "bcmath.c", null, "-Iext/bcmath/libbcmath/src /DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); EXTENSION("bcmath", "bcmath.c", null, "-Iext/bcmath/libbcmath/src /DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
ADD_SOURCES("ext/bcmath/libbcmath/src", "add.c div.c init.c neg.c \ ADD_SOURCES("ext/bcmath/libbcmath/src", "add.c div.c init.c neg.c \
outofmem.c raisemod.c sub.c compare.c divmod.c int2num.c \ raisemod.c sub.c compare.c divmod.c int2num.c \
num2long.c output.c recmul.c sqrt.c zero.c debug.c doaddsub.c \ num2long.c output.c recmul.c sqrt.c zero.c debug.c doaddsub.c \
nearzero.c num2str.c raise.c rmzero.c str2num.c", "bcmath"); nearzero.c num2str.c raise.c rmzero.c str2num.c", "bcmath");

View File

@ -150,9 +150,6 @@ _PROTOTYPE(void bc_out_num, (bc_num num, int o_base, void (* out_char)(int),
int leading_zero)); int leading_zero));
/* Prototypes needed for external utility routines. */ /* Prototypes needed for external utility routines. */
_PROTOTYPE(void bc_out_of_memory, (void));
#define bc_new_num(length, scale) _bc_new_num_ex((length), (scale), 0) #define bc_new_num(length, scale) _bc_new_num_ex((length), (scale), 0)
#define bc_free_num(num) _bc_free_num_ex((num), 0) #define bc_free_num(num) _bc_free_num_ex((num), 0)
#define bc_num2str(num) bc_num2str_ex((num), (num->n_scale)) #define bc_num2str(num) bc_num2str_ex((num), (num->n_scale))

View File

@ -44,10 +44,6 @@ _bc_new_num_ex (length, scale, persistent)
int length, scale, persistent; int length, scale, persistent;
{ {
bc_num temp; bc_num temp;
/* PHP Change: add length check */
if ((size_t)length+(size_t)scale > INT_MAX) {
zend_error(E_ERROR, "Result too long, max is %d", INT_MAX);
}
/* PHP Change: malloc() -> pemalloc(), removed free_list code */ /* PHP Change: malloc() -> pemalloc(), removed free_list code */
temp = (bc_num) safe_pemalloc (1, sizeof(bc_struct)+length, scale, persistent); temp = (bc_num) safe_pemalloc (1, sizeof(bc_struct)+length, scale, persistent);
temp->n_sign = PLUS; temp->n_sign = PLUS;

View File

@ -55,7 +55,6 @@ zend_string
str = zend_string_alloc(num->n_len + scale + signch + 1, 0); str = zend_string_alloc(num->n_len + scale + signch + 1, 0);
else else
str = zend_string_alloc(num->n_len + signch, 0); str = zend_string_alloc(num->n_len + signch, 0);
if (str == NULL) bc_out_of_memory();
/* The negative sign if needed. */ /* The negative sign if needed. */
sptr = ZSTR_VAL(str); sptr = ZSTR_VAL(str);

View File

@ -1,44 +0,0 @@
/* outofmem.c: bcmath library file. */
/*
Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
Copyright (C) 2000 Philip A. Nelson
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. (LICENSE)
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to:
The Free Software Foundation, Inc.
59 Temple Place, Suite 330
Boston, MA 02111-1307 USA.
You may contact the author by:
e-mail: philnelson@acm.org
us-mail: Philip A. Nelson
Computer Science Department, 9062
Western Washington University
Bellingham, WA 98226-9062
*************************************************************************/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include "bcmath.h"
#include "private.h"
void bc_out_of_memory (void)
{
zend_error(E_ERROR, "bcmath: out of memory!");
}

View File

@ -52,12 +52,18 @@ bc_raise (bc_num num1, bc_num num2, bc_num *result, int scale)
int calcscale; int calcscale;
char neg; char neg;
/* Check the exponent for scale digits and convert to a long. */ /* Check the exponent for scale digits and convert to a long. */
if (num2->n_scale != 0) if (num2->n_scale != 0) {
php_error_docref (NULL, E_WARNING, "Non-zero scale in exponent"); /* 2nd argument from PHP_FUNCTION(bcpow) */
exponent = bc_num2long (num2); zend_argument_value_error(2, "cannot have a fractional part");
if (exponent == 0 && (num2->n_len > 1 || num2->n_value[0] != 0)) return;
php_error_docref (NULL, E_WARNING, "Exponent too large"); }
exponent = bc_num2long (num2);
if (exponent == 0 && (num2->n_len > 1 || num2->n_value[0] != 0)) {
/* 2nd argument from PHP_FUNCTION(bcpow) */
zend_argument_value_error(2, "is too large");
return;
}
/* Special case if exponent is a zero. */ /* Special case if exponent is a zero. */
if (exponent == 0) if (exponent == 0)

View File

@ -36,38 +36,41 @@
#include <stdarg.h> #include <stdarg.h>
#include "bcmath.h" #include "bcmath.h"
#include "private.h" #include "private.h"
#include "zend_exceptions.h"
/* Raise BASE to the EXPO power, reduced modulo MOD. The result is placed in RESULT. */
/* Truncate a number to zero scale. To avoid sharing issues (refcount and zend_result bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
shared n_value) the number is copied, this copy is truncated, and the
original number is "freed". */
static void
_bc_truncate (bc_num *num)
{
bc_num temp;
temp = bc_new_num ((*num)->n_len, 0);
temp->n_sign = (*num)->n_sign;
memcpy (temp->n_value, (*num)->n_value, (*num)->n_len);
bc_free_num (num);
*num = temp;
}
/* Raise BASE to the EXPO power, reduced modulo MOD. The result is
placed in RESULT. If a EXPO is not an integer,
only the integer part is used. */
int
bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
{ {
bc_num power, exponent, modulus, parity, temp; bc_num power, exponent, modulus, parity, temp;
int rscale; int rscale;
/* Check for correct numbers. */ /* Check the base for scale digits. */
if (bc_is_zero(mod)) return -1; if (base->n_scale != 0) {
if (bc_is_neg(expo)) return -2; /* 1st argument from PHP_FUNCTION(bcpowmod) */
zend_argument_value_error(1, "cannot have a fractional part");
return FAILURE;
}
/* Check the exponent for scale digits. */
if (expo->n_scale != 0) {
/* 2nd argument from PHP_FUNCTION(bcpowmod) */
zend_argument_value_error(2, "cannot have a fractional part");
return FAILURE;
}
if (bc_is_neg(expo)) {
zend_argument_value_error(2, "must be greater than or equal to 0");
return FAILURE;
}
/* Check the modulus for scale digits. */
if (mod->n_scale != 0) {
/* 3rd argument from PHP_FUNCTION(bcpowmod) */
zend_argument_value_error(3, "cannot have a fractional part");
return FAILURE;
}
/* Modulus cannot be 0 */
if (bc_is_zero(mod)) {
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
return FAILURE;
}
/* Set initial values. */ /* Set initial values. */
power = bc_copy_num (base); power = bc_copy_num (base);
@ -76,27 +79,6 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
temp = bc_copy_num (BCG(_one_)); temp = bc_copy_num (BCG(_one_));
bc_init_num(&parity); bc_init_num(&parity);
/* Check the base for scale digits. */
if (power->n_scale != 0)
{
php_error_docref (NULL, E_WARNING, "Non-zero scale in base");
_bc_truncate (&power);
}
/* Check the exponent for scale digits. */
if (exponent->n_scale != 0)
{
php_error_docref (NULL, E_WARNING, "Non-zero scale in exponent");
_bc_truncate (&exponent);
}
/* Check the modulus for scale digits. */
if (modulus->n_scale != 0)
{
php_error_docref (NULL, E_WARNING, "Non-zero scale in modulus");
_bc_truncate (&modulus);
}
/* Do the calculation. */ /* Do the calculation. */
rscale = MAX(scale, power->n_scale); rscale = MAX(scale, power->n_scale);
if ( !bc_compare(modulus, BCG(_one_)) ) if ( !bc_compare(modulus, BCG(_one_)) )
@ -127,5 +109,5 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
bc_free_num (result); bc_free_num (result);
bc_free_num (&parity); bc_free_num (&parity);
*result = temp; *result = temp;
return 0; /* Everything is OK. */ return SUCCESS; /* Everything is OK. */
} }

View File

@ -6,8 +6,11 @@ if (!extension_loaded('bcmath')) die('skip bcmath extension is not available');
?> ?>
--FILE-- --FILE--
<?php <?php
var_dump(bcpow('1', '1.1', 2)); try {
var_dump(bcpow('1', '1.1', 2));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?> ?>
--EXPECTF-- --EXPECT--
Warning: bcpow(): Non-zero scale in exponent in %s on line %d bcpow(): Argument #2 ($exponent) cannot have a fractional part
string(4) "1.00"

View File

@ -6,8 +6,11 @@ if (!extension_loaded('bcmath')) die('skip bcmath extension is not available');
?> ?>
--FILE-- --FILE--
<?php <?php
var_dump(bcpow('0', '9223372036854775808', 2)); try {
var_dump(bcpow('0', '9223372036854775808', 2));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?> ?>
--EXPECTF-- --EXPECT--
Warning: bcpow(): Exponent too large in %s on line %d bcpow(): Argument #2 ($exponent) is too large
string(4) "1.00"

View File

@ -6,11 +6,13 @@ bcpowmod() - Raise an arbitrary precision number to another, reduced by a specif
bcmath.scale=0 bcmath.scale=0
--FILE-- --FILE--
<?php <?php
echo bcpowmod("5", "2", "7") . "\n"; var_dump(bcpowmod("5", "2", "7"));
echo bcpowmod("-2", "5", "7") . "\n"; var_dump(bcpowmod("-2", "5", "7"));
echo bcpowmod("10", "2147483648", "2047"); var_dump(bcpowmod("10", "2147483648", "2047"));
var_dump(bcpowmod("10", "0", "2047"));
?> ?>
--EXPECT-- --EXPECT--
4 string(1) "4"
-4 string(2) "-4"
790 string(3) "790"
string(1) "1"

View File

@ -13,4 +13,4 @@ try {
} }
?> ?>
--EXPECT-- --EXPECT--
bcpowmod(): Argument #2 ($exponent) must be greater than 0 bcpowmod(): Argument #2 ($exponent) must be greater than or equal to 0

View File

@ -7,7 +7,7 @@ Gabriel Caruso (carusogabriel34@gmail.com)
--FILE-- --FILE--
<?php <?php
try { try {
var_dump(bcpowmod('1', '-1', '0')); var_dump(bcpowmod('1', '1', '0'));
} catch (DivisionByZeroError $ex) { } catch (DivisionByZeroError $ex) {
echo $ex->getMessage(), PHP_EOL; echo $ex->getMessage(), PHP_EOL;
} }

View File

@ -14,4 +14,4 @@ try {
} }
?> ?>
--EXPECT-- --EXPECT--
Square root of negative number bcsqrt(): Argument #1 ($operand) must be greater than or equal to 0

View File

@ -11,10 +11,12 @@ try {
} catch (\ValueError $e) { } catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL; echo $e->getMessage() . \PHP_EOL;
} }
var_dump(bcpowmod(1, 1.2, 1, 1)); try {
var_dump(bcpowmod(1, 1.2, 1, 1));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?> ?>
--EXPECTF-- --EXPECT--
bcpowmod(): Argument #4 ($scale) must be between 0 and 2147483647 bcpowmod(): Argument #4 ($scale) must be between 0 and 2147483647
bcpowmod(): Argument #2 ($exponent) cannot have a fractional part
Warning: bcpowmod(): Non-zero scale in exponent in %s on line %d
string(3) "0.0"

View File

@ -6,12 +6,17 @@ if (!extension_loaded('bcmath')) die('skip bcmath extension is not available');
?> ?>
--FILE-- --FILE--
<?php <?php
var_dump(bcpowmod('4.1', '4', '3', 3)); try {
var_dump(bcpowmod('4', '4', '3.1', 3)); var_dump(bcpowmod('4.1', '4', '3', 3));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
var_dump(bcpowmod('4', '4', '3.1', 3));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?> ?>
--EXPECTF-- --EXPECT--
Warning: bcpowmod(): Non-zero scale in base in %s on line %d bcpowmod(): Argument #1 ($base) cannot have a fractional part
string(5) "1.000" bcpowmod(): Argument #3 ($modulus) cannot have a fractional part
Warning: bcpowmod(): Non-zero scale in modulus in %s on line %d
string(5) "1.000"

View File

@ -6,7 +6,11 @@ if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
?> ?>
--FILE-- --FILE--
<?php <?php
print @bcmul("\xB26483605105519922841849335928742092", bcpowmod(2, 65535, -4e-4)); try {
print bcmul("\xB26483605105519922841849335928742092", bcpowmod(2, 65535, -4e-4));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?> ?>
--EXPECT-- --EXPECT--
0 bcpowmod(): Argument #3 ($modulus) cannot have a fractional part