From 964eff595442d641f44b8a951f7dbefc1441dbb5 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Tue, 5 Aug 2025 13:47:59 +0200 Subject: [PATCH] Throw ValueError when 1 is provided to the second argument of `log()` --- NEWS | 4 ++++ ext/standard/math.c | 8 ++------ ext/standard/tests/math/log_error.phpt | 9 ++++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 2213ff10b1db7..36a5e705107a2 100644 --- a/NEWS +++ b/NEWS @@ -7,4 +7,8 @@ PHP NEWS with a given skeleton, locale, collapse type and identity fallback. (BogdanUngureanu) +- Standard: + . Passing 1 or negative base to log() now throws a ValueError. + (alexandre-daubois) + <<< NOTE: Insert NEWS from last stable release here prior to actual release! >>> diff --git a/ext/standard/math.c b/ext/standard/math.c index 142d473864f75..0d394941569e7 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -667,12 +667,8 @@ 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"); + 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(); } diff --git a/ext/standard/tests/math/log_error.phpt b/ext/standard/tests/math/log_error.phpt index 4c90c20a2052f..d1d1effcd2818 100644 --- a/ext/standard/tests/math/log_error.phpt +++ b/ext/standard/tests/math/log_error.phpt @@ -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