Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.3.0beta2

- Bcmath
. Fixed GH-11761 (removing trailing zeros from numbers) (jorgsowa)

- Core:
. Fixed oss-fuzz #60741 (Leak in open_basedir). (ilutov)

Expand Down
11 changes: 11 additions & 0 deletions ext/bcmath/libbcmath/src/str2num.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ bool bc_str2num (bc_num *num, char *str, size_t scale)
size_t digits, strscale;
char *ptr, *nptr;
bool zero_int = false;
size_t trailing_zeros = 0;

/* Prepare num. */
bc_free_num (num);
Expand Down Expand Up @@ -68,9 +69,19 @@ bool bc_str2num (bc_num *num, char *str, size_t scale)
}
/* digits after the decimal point */
while (*ptr >= '0' && *ptr <= '9') {
if (*ptr == '0') {
trailing_zeros++;
} else {
trailing_zeros = 0;
}
ptr++;
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)) {
*num = bc_copy_num (BCG(_zero_));
return *ptr == '\0';
Expand Down
34 changes: 0 additions & 34 deletions ext/bcmath/tests/bcpow.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ require(__DIR__ . "/run_bcmath_tests_function.inc");

$exponents = ["15", "-15", "1", "-9", "0", "-0"];
$baseNumbers = array_merge($exponents, [
"0.00",
"-0.00",
"14.14",
"-16.60",
"0.15",
Expand Down Expand Up @@ -76,22 +74,6 @@ Number "-0" (scale 0)
-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)
14.14 ** 15 = 180609729388653367
14.14 ** -15 = 0
Expand Down Expand Up @@ -220,22 +202,6 @@ Number "-0" (scale 10)
-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)
14.14 ** 15 = 180609729388653367.2586094856
14.14 ** -15 = 0.0000000000
Expand Down
18 changes: 0 additions & 18 deletions ext/bcmath/tests/bcpow_large_numbers.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ require(__DIR__ . "/run_bcmath_tests_function.inc");

$exponents = ["252", "-112"];
$baseNumbers = array_merge($exponents, [
"0.00",
"-0.00",
"14.14",
"-16.60",
"0.15",
Expand All @@ -36,14 +34,6 @@ Number "-112" (scale 0)
-112 ** 252 = 2528958559509291693508169626548016243760525615786379976651267082288161074806837567146354242426675448089179587223689918667106749284121965637887934532709154725528081671334320643858146400820666287506930741579829377389324701928200642059190071982670723834096616484481264403390509754202256821825886418273918096647296731861068941003342202311747852949747419032091731983573265045376333262301085406296848909763397511959941544798830761308589095475080050282796818166220650002245272286955283850226653729461037771414650027776147456
-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)
14.14 ** 252 = 81893830287657387446140079713887806093576884288543703328783802176508922753380870094003376900318176725198165627317176488631295332728458521626072637389422651709754634844150191850264235770478926203337416116264674564206565124289082609409927041547210316199545928823591027686585730497632744792533
14.14 ** -112 = 0
Expand Down Expand Up @@ -92,14 +82,6 @@ Number "-112" (scale 10)
-112 ** 252 = 2528958559509291693508169626548016243760525615786379976651267082288161074806837567146354242426675448089179587223689918667106749284121965637887934532709154725528081671334320643858146400820666287506930741579829377389324701928200642059190071982670723834096616484481264403390509754202256821825886418273918096647296731861068941003342202311747852949747419032091731983573265045376333262301085406296848909763397511959941544798830761308589095475080050282796818166220650002245272286955283850226653729461037771414650027776147456.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)
14.14 ** 252 = 81893830287657387446140079713887806093576884288543703328783802176508922753380870094003376900318176725198165627317176488631295332728458521626072637389422651709754634844150191850264235770478926203337416116264674564206565124289082609409927041547210316199545928823591027686585730497632744792533.0727815413
14.14 ** -112 = 0.0000000000
Expand Down
95 changes: 95 additions & 0 deletions ext/bcmath/tests/bcpow_zero.phpt
Original file line number Diff line number Diff line change
@@ -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