Fix GH-11761: Bcmath numbers with trailing zeros (#11798)

This commit is contained in:
Jorg Adam Sowa 2023-07-26 14:48:50 +02:00 committed by GitHub
parent 7273349a1f
commit 549c657897
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 52 deletions

3
NEWS
View file

@ -2,6 +2,9 @@ PHP NEWS
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.3.0beta2 ?? ??? ????, PHP 8.3.0beta2
- Bcmath
. Fixed GH-11761 (removing trailing zeros from numbers) (jorgsowa)
- Core: - Core:
. Fixed oss-fuzz #60741 (Leak in open_basedir). (ilutov) . Fixed oss-fuzz #60741 (Leak in open_basedir). (ilutov)

View file

@ -40,6 +40,7 @@ bool bc_str2num (bc_num *num, char *str, size_t scale)
size_t digits, strscale; size_t digits, strscale;
char *ptr, *nptr; char *ptr, *nptr;
bool zero_int = false; bool zero_int = false;
size_t trailing_zeros = 0;
/* Prepare num. */ /* Prepare num. */
bc_free_num (num); bc_free_num (num);
@ -68,9 +69,19 @@ bool bc_str2num (bc_num *num, char *str, size_t scale)
} }
/* digits after the decimal point */ /* digits after the decimal point */
while (*ptr >= '0' && *ptr <= '9') { while (*ptr >= '0' && *ptr <= '9') {
if (*ptr == '0') {
trailing_zeros++;
} else {
trailing_zeros = 0;
}
ptr++; ptr++;
strscale++; strscale++;
} }
if (trailing_zeros > 0) {
/* Trailining zeros should not take part in the computation of the overall scale, as it is pointless. */
strscale = strscale - trailing_zeros;
}
if ((*ptr != '\0') || (digits+strscale == 0)) { if ((*ptr != '\0') || (digits+strscale == 0)) {
*num = bc_copy_num (BCG(_zero_)); *num = bc_copy_num (BCG(_zero_));
return *ptr == '\0'; return *ptr == '\0';

View file

@ -10,8 +10,6 @@ require(__DIR__ . "/run_bcmath_tests_function.inc");
$exponents = ["15", "-15", "1", "-9", "0", "-0"]; $exponents = ["15", "-15", "1", "-9", "0", "-0"];
$baseNumbers = array_merge($exponents, [ $baseNumbers = array_merge($exponents, [
"0.00",
"-0.00",
"14.14", "14.14",
"-16.60", "-16.60",
"0.15", "0.15",
@ -76,22 +74,6 @@ Number "-0" (scale 0)
-0 ** 0 = 1 -0 ** 0 = 1
-0 ** -0 = 1 -0 ** -0 = 1
Number "0.00" (scale 0)
0.00 ** 15 = 0
0.00 ** -15 = 0
0.00 ** 1 = 0
0.00 ** -9 = 0
0.00 ** 0 = 1
0.00 ** -0 = 1
Number "-0.00" (scale 0)
-0.00 ** 15 = 0
-0.00 ** -15 = 0
-0.00 ** 1 = 0
-0.00 ** -9 = 0
-0.00 ** 0 = 1
-0.00 ** -0 = 1
Number "14.14" (scale 0) Number "14.14" (scale 0)
14.14 ** 15 = 180609729388653367 14.14 ** 15 = 180609729388653367
14.14 ** -15 = 0 14.14 ** -15 = 0
@ -220,22 +202,6 @@ Number "-0" (scale 10)
-0 ** 0 = 1.0000000000 -0 ** 0 = 1.0000000000
-0 ** -0 = 1.0000000000 -0 ** -0 = 1.0000000000
Number "0.00" (scale 10)
0.00 ** 15 = 0.0000000000
0.00 ** -15 = 0.0000000000
0.00 ** 1 = 0.0000000000
0.00 ** -9 = 0.0000000000
0.00 ** 0 = 1.0000000000
0.00 ** -0 = 1.0000000000
Number "-0.00" (scale 10)
-0.00 ** 15 = 0.0000000000
-0.00 ** -15 = 0.0000000000
-0.00 ** 1 = 0.0000000000
-0.00 ** -9 = 0.0000000000
-0.00 ** 0 = 1.0000000000
-0.00 ** -0 = 1.0000000000
Number "14.14" (scale 10) Number "14.14" (scale 10)
14.14 ** 15 = 180609729388653367.2586094856 14.14 ** 15 = 180609729388653367.2586094856
14.14 ** -15 = 0.0000000000 14.14 ** -15 = 0.0000000000

View file

@ -10,8 +10,6 @@ require(__DIR__ . "/run_bcmath_tests_function.inc");
$exponents = ["252", "-112"]; $exponents = ["252", "-112"];
$baseNumbers = array_merge($exponents, [ $baseNumbers = array_merge($exponents, [
"0.00",
"-0.00",
"14.14", "14.14",
"-16.60", "-16.60",
"0.15", "0.15",
@ -36,14 +34,6 @@ Number "-112" (scale 0)
-112 ** 252 = 2528958559509291693508169626548016243760525615786379976651267082288161074806837567146354242426675448089179587223689918667106749284121965637887934532709154725528081671334320643858146400820666287506930741579829377389324701928200642059190071982670723834096616484481264403390509754202256821825886418273918096647296731861068941003342202311747852949747419032091731983573265045376333262301085406296848909763397511959941544798830761308589095475080050282796818166220650002245272286955283850226653729461037771414650027776147456 -112 ** 252 = 2528958559509291693508169626548016243760525615786379976651267082288161074806837567146354242426675448089179587223689918667106749284121965637887934532709154725528081671334320643858146400820666287506930741579829377389324701928200642059190071982670723834096616484481264403390509754202256821825886418273918096647296731861068941003342202311747852949747419032091731983573265045376333262301085406296848909763397511959941544798830761308589095475080050282796818166220650002245272286955283850226653729461037771414650027776147456
-112 ** -112 = 0 -112 ** -112 = 0
Number "0.00" (scale 0)
0.00 ** 252 = 0
0.00 ** -112 = 0
Number "-0.00" (scale 0)
-0.00 ** 252 = 0
-0.00 ** -112 = 0
Number "14.14" (scale 0) Number "14.14" (scale 0)
14.14 ** 252 = 81893830287657387446140079713887806093576884288543703328783802176508922753380870094003376900318176725198165627317176488631295332728458521626072637389422651709754634844150191850264235770478926203337416116264674564206565124289082609409927041547210316199545928823591027686585730497632744792533 14.14 ** 252 = 81893830287657387446140079713887806093576884288543703328783802176508922753380870094003376900318176725198165627317176488631295332728458521626072637389422651709754634844150191850264235770478926203337416116264674564206565124289082609409927041547210316199545928823591027686585730497632744792533
14.14 ** -112 = 0 14.14 ** -112 = 0
@ -92,14 +82,6 @@ Number "-112" (scale 10)
-112 ** 252 = 2528958559509291693508169626548016243760525615786379976651267082288161074806837567146354242426675448089179587223689918667106749284121965637887934532709154725528081671334320643858146400820666287506930741579829377389324701928200642059190071982670723834096616484481264403390509754202256821825886418273918096647296731861068941003342202311747852949747419032091731983573265045376333262301085406296848909763397511959941544798830761308589095475080050282796818166220650002245272286955283850226653729461037771414650027776147456.0000000000 -112 ** 252 = 2528958559509291693508169626548016243760525615786379976651267082288161074806837567146354242426675448089179587223689918667106749284121965637887934532709154725528081671334320643858146400820666287506930741579829377389324701928200642059190071982670723834096616484481264403390509754202256821825886418273918096647296731861068941003342202311747852949747419032091731983573265045376333262301085406296848909763397511959941544798830761308589095475080050282796818166220650002245272286955283850226653729461037771414650027776147456.0000000000
-112 ** -112 = 0.0000000000 -112 ** -112 = 0.0000000000
Number "0.00" (scale 10)
0.00 ** 252 = 0.0000000000
0.00 ** -112 = 0.0000000000
Number "-0.00" (scale 10)
-0.00 ** 252 = 0.0000000000
-0.00 ** -112 = 0.0000000000
Number "14.14" (scale 10) Number "14.14" (scale 10)
14.14 ** 252 = 81893830287657387446140079713887806093576884288543703328783802176508922753380870094003376900318176725198165627317176488631295332728458521626072637389422651709754634844150191850264235770478926203337416116264674564206565124289082609409927041547210316199545928823591027686585730497632744792533.0727815413 14.14 ** 252 = 81893830287657387446140079713887806093576884288543703328783802176508922753380870094003376900318176725198165627317176488631295332728458521626072637389422651709754634844150191850264235770478926203337416116264674564206565124289082609409927041547210316199545928823591027686585730497632744792533.0727815413
14.14 ** -112 = 0.0000000000 14.14 ** -112 = 0.0000000000

View file

@ -0,0 +1,95 @@
--TEST--
bcpow() function with number zero
--EXTENSIONS--
bcmath
--INI--
bcmath.scale=0
--FILE--
<?php
require(__DIR__ . "/run_bcmath_tests_function.inc");
$exponents = ["0", "-0", "1", "1128321638"];
$baseNumbers = [
"0.00",
"-0.00",
"0.000000000000000000000000",
"-0.000000000000000000000000",
"-0",
"0",
];
run_bcmath_tests($baseNumbers, $exponents, "**", bcpow(...));
?>
--EXPECT--
Number "0.00" (scale 0)
0.00 ** 0 = 1
0.00 ** -0 = 1
0.00 ** 1 = 0
0.00 ** 1128321638 = 0
Number "-0.00" (scale 0)
-0.00 ** 0 = 1
-0.00 ** -0 = 1
-0.00 ** 1 = 0
-0.00 ** 1128321638 = 0
Number "0.000000000000000000000000" (scale 0)
0.000000000000000000000000 ** 0 = 1
0.000000000000000000000000 ** -0 = 1
0.000000000000000000000000 ** 1 = 0
0.000000000000000000000000 ** 1128321638 = 0
Number "-0.000000000000000000000000" (scale 0)
-0.000000000000000000000000 ** 0 = 1
-0.000000000000000000000000 ** -0 = 1
-0.000000000000000000000000 ** 1 = 0
-0.000000000000000000000000 ** 1128321638 = 0
Number "-0" (scale 0)
-0 ** 0 = 1
-0 ** -0 = 1
-0 ** 1 = 0
-0 ** 1128321638 = 0
Number "0" (scale 0)
0 ** 0 = 1
0 ** -0 = 1
0 ** 1 = 0
0 ** 1128321638 = 0
Number "0.00" (scale 10)
0.00 ** 0 = 1.0000000000
0.00 ** -0 = 1.0000000000
0.00 ** 1 = 0.0000000000
0.00 ** 1128321638 = 0.0000000000
Number "-0.00" (scale 10)
-0.00 ** 0 = 1.0000000000
-0.00 ** -0 = 1.0000000000
-0.00 ** 1 = 0.0000000000
-0.00 ** 1128321638 = 0.0000000000
Number "0.000000000000000000000000" (scale 10)
0.000000000000000000000000 ** 0 = 1.0000000000
0.000000000000000000000000 ** -0 = 1.0000000000
0.000000000000000000000000 ** 1 = 0.0000000000
0.000000000000000000000000 ** 1128321638 = 0.0000000000
Number "-0.000000000000000000000000" (scale 10)
-0.000000000000000000000000 ** 0 = 1.0000000000
-0.000000000000000000000000 ** -0 = 1.0000000000
-0.000000000000000000000000 ** 1 = 0.0000000000
-0.000000000000000000000000 ** 1128321638 = 0.0000000000
Number "-0" (scale 10)
-0 ** 0 = 1.0000000000
-0 ** -0 = 1.0000000000
-0 ** 1 = 0.0000000000
-0 ** 1128321638 = 0.0000000000
Number "0" (scale 10)
0 ** 0 = 1.0000000000
0 ** -0 = 1.0000000000
0 ** 1 = 0.0000000000
0 ** 1128321638 = 0.0000000000