diff options
author | George Peter Banyard <girgias@php.net> | 2020-09-11 17:40:06 +0200 |
---|---|---|
committer | George Peter Banyard <girgias@php.net> | 2020-09-11 17:40:06 +0200 |
commit | cd05b56a6f7d4f03a80f86c15248eda995b1762e (patch) | |
tree | be55bb068dc85f3d9c4853b22924356aa5a081a5 /ext/bcmath/libbcmath/src | |
parent | f29bfc0bd8c0dc152e6115ec260957e1617d08dc (diff) | |
download | php-git-cd05b56a6f7d4f03a80f86c15248eda995b1762e.tar.gz |
Promote some warnings in BCMath to Errors
Also do a bit of refactoring at the same time.
Closes GH-6105
Diffstat (limited to 'ext/bcmath/libbcmath/src')
-rw-r--r-- | ext/bcmath/libbcmath/src/bcmath.h | 3 | ||||
-rw-r--r-- | ext/bcmath/libbcmath/src/init.c | 4 | ||||
-rw-r--r-- | ext/bcmath/libbcmath/src/num2str.c | 1 | ||||
-rw-r--r-- | ext/bcmath/libbcmath/src/outofmem.c | 44 | ||||
-rw-r--r-- | ext/bcmath/libbcmath/src/raise.c | 18 | ||||
-rw-r--r-- | ext/bcmath/libbcmath/src/raisemod.c | 80 |
6 files changed, 43 insertions, 107 deletions
diff --git a/ext/bcmath/libbcmath/src/bcmath.h b/ext/bcmath/libbcmath/src/bcmath.h index becba7ec3e..24425544bb 100644 --- a/ext/bcmath/libbcmath/src/bcmath.h +++ b/ext/bcmath/libbcmath/src/bcmath.h @@ -150,9 +150,6 @@ _PROTOTYPE(void bc_out_num, (bc_num num, int o_base, void (* out_char)(int), int leading_zero)); /* 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_free_num(num) _bc_free_num_ex((num), 0) #define bc_num2str(num) bc_num2str_ex((num), (num->n_scale)) diff --git a/ext/bcmath/libbcmath/src/init.c b/ext/bcmath/libbcmath/src/init.c index 676076f89d..96e934b34d 100644 --- a/ext/bcmath/libbcmath/src/init.c +++ b/ext/bcmath/libbcmath/src/init.c @@ -44,10 +44,6 @@ _bc_new_num_ex (length, scale, persistent) int length, scale, persistent; { 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 */ temp = (bc_num) safe_pemalloc (1, sizeof(bc_struct)+length, scale, persistent); temp->n_sign = PLUS; diff --git a/ext/bcmath/libbcmath/src/num2str.c b/ext/bcmath/libbcmath/src/num2str.c index 23988c0d6c..3e23158125 100644 --- a/ext/bcmath/libbcmath/src/num2str.c +++ b/ext/bcmath/libbcmath/src/num2str.c @@ -55,7 +55,6 @@ zend_string str = zend_string_alloc(num->n_len + scale + signch + 1, 0); else str = zend_string_alloc(num->n_len + signch, 0); - if (str == NULL) bc_out_of_memory(); /* The negative sign if needed. */ sptr = ZSTR_VAL(str); diff --git a/ext/bcmath/libbcmath/src/outofmem.c b/ext/bcmath/libbcmath/src/outofmem.c deleted file mode 100644 index cb2f1085a5..0000000000 --- a/ext/bcmath/libbcmath/src/outofmem.c +++ /dev/null @@ -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!"); -} diff --git a/ext/bcmath/libbcmath/src/raise.c b/ext/bcmath/libbcmath/src/raise.c index 700a50f50c..2e843f179a 100644 --- a/ext/bcmath/libbcmath/src/raise.c +++ b/ext/bcmath/libbcmath/src/raise.c @@ -52,12 +52,18 @@ bc_raise (bc_num num1, bc_num num2, bc_num *result, int scale) int calcscale; char neg; - /* Check the exponent for scale digits and convert to a long. */ - if (num2->n_scale != 0) - php_error_docref (NULL, E_WARNING, "Non-zero scale in exponent"); - exponent = bc_num2long (num2); - if (exponent == 0 && (num2->n_len > 1 || num2->n_value[0] != 0)) - php_error_docref (NULL, E_WARNING, "Exponent too large"); + /* Check the exponent for scale digits and convert to a long. */ + if (num2->n_scale != 0) { + /* 2nd argument from PHP_FUNCTION(bcpow) */ + zend_argument_value_error(2, "cannot have a fractional part"); + return; + } + 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. */ if (exponent == 0) diff --git a/ext/bcmath/libbcmath/src/raisemod.c b/ext/bcmath/libbcmath/src/raisemod.c index 46dfd7a8a8..bb0d16f6f0 100644 --- a/ext/bcmath/libbcmath/src/raisemod.c +++ b/ext/bcmath/libbcmath/src/raisemod.c @@ -36,38 +36,41 @@ #include <stdarg.h> #include "bcmath.h" #include "private.h" +#include "zend_exceptions.h" - -/* Truncate a number to zero scale. To avoid sharing issues (refcount and - 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) +/* Raise BASE to the EXPO power, reduced modulo MOD. The result is placed in RESULT. */ +zend_result bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale) { bc_num power, exponent, modulus, parity, temp; int rscale; - /* Check for correct numbers. */ - if (bc_is_zero(mod)) return -1; - if (bc_is_neg(expo)) return -2; + /* Check the base for scale digits. */ + if (base->n_scale != 0) { + /* 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. */ 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_)); 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. */ rscale = MAX(scale, power->n_scale); 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 (&parity); *result = temp; - return 0; /* Everything is OK. */ + return SUCCESS; /* Everything is OK. */ } |