Skip to content

Commit 9f3d5ca

Browse files
Throw ValueError when 1 is provided to the second argument of log()
1 parent b6bee2c commit 9f3d5ca

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ PHP NEWS
148148
. Add enum SortDirection. (timwolla)
149149
. pathinfo() raises a ValueError with an invalid $flags argument.
150150
(David Carlier)
151+
. Passing 1 or negative base to log() now throws a ValueError.
152+
(alexandre-daubois)
151153

152154
- Streams:
153155
. Added so_keepalive, tcp_keepidle, tcp_keepintvl and tcp_keepcnt stream

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ PHP 8.6 UPGRADE NOTES
132132
constants).
133133

134134
- Standard:
135+
. log() now raises a ValueError when the base argument is 1 or less
136+
than or equal to 0. Previously, base 1 silently returned NAN.
135137
. pathinfo() now raises a ValueError when an invalid $flag argument
136138
value is passed.
137139

ext/standard/math.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,8 @@ PHP_FUNCTION(log)
721721
RETURN_DOUBLE(log10(num));
722722
}
723723

724-
if (base == 1.0) {
725-
RETURN_DOUBLE(ZEND_NAN);
726-
}
727-
728-
if (base <= 0.0) {
729-
zend_argument_value_error(2, "must be greater than 0");
724+
if (base <= 0.0 || base == 1.0) {
725+
zend_argument_value_error(2, "must not be 1 or less than or equal to 0");
730726
RETURN_THROWS();
731727
}
732728

ext/standard/tests/math/log_error.phpt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ try {
99
} catch (ValueError $exception) {
1010
echo $exception->getMessage() . "\n";
1111
}
12+
13+
try {
14+
log(36, 1);
15+
} catch (ValueError $exception) {
16+
echo $exception->getMessage() . "\n";
17+
}
1218
?>
1319
--EXPECT--
14-
log(): Argument #2 ($base) must be greater than 0
20+
log(): Argument #2 ($base) must not be 1 or less than or equal to 0
21+
log(): Argument #2 ($base) must not be 1 or less than or equal to 0

0 commit comments

Comments
 (0)