Skip to content

Throw ValueError when 1 is provided to the second argument of log() #19370

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ PHP 8.5 UPGRADE NOTES
. Using a printf-family function with a formatter that did not specify the
precision previously incorrectly reset the precision instead of treating
it as a precision of 0. See GH-18897.
. Passing 1 as the second argument to log() now throws a ValueError.
Previously, it returned NaN.

========================================
2. New Features
Expand Down
10 changes: 3 additions & 7 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,9 @@ PHP_FUNCTION(log)
RETURN_DOUBLE(log10(num));
}

if (base == 1.0) {
RETURN_DOUBLE(ZEND_NAN);
}

if (base <= 0.0) {
zend_argument_value_error(2, "must be greater than 0");
RETURN_THROWS();
if (base <= 0.0 || base == 1.0) {
zend_argument_value_error(2, "must not be 1 or less than or equal to 0");
RETURN_THROWS();
}

RETURN_DOUBLE(log(num) / log(base));
Expand Down
9 changes: 8 additions & 1 deletion ext/standard/tests/math/log_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ try {
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
log(36, 1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
?>
--EXPECT--
log(): Argument #2 ($base) must be greater than 0
log(): Argument #2 ($base) must not be 1 or less than or equal to 0
log(): Argument #2 ($base) must not be 1 or less than or equal to 0