From db2869346ce033a379c4d3440a7708465adbc8e1 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 8 Apr 2024 22:26:14 +0100 Subject: [PATCH] ext/posix: posix_isatty set errno for it too. Close GH-13918 --- NEWS | 2 ++ UPGRADING | 4 ++++ ext/posix/posix.c | 2 ++ ext/posix/tests/posix_isatty_value_errors.phpt | 6 ++++++ 4 files changed, 14 insertions(+) diff --git a/NEWS b/NEWS index 84b3a976bd4..89d14dea9a0 100644 --- a/NEWS +++ b/NEWS @@ -173,6 +173,8 @@ PHP NEWS - POSIX: . Added POSIX_SC_CHILD_MAX and POSIX_SC_CLK_TCK constants. (Jakub Zelenka) + . Updated posix_isatty to set the error number on file descriptors. + (David Carlier) - PSpell: . Moved to PECL. (Derick Rethans) diff --git a/UPGRADING b/UPGRADING index eb4993b5281..3450825a2a5 100644 --- a/UPGRADING +++ b/UPGRADING @@ -383,6 +383,10 @@ PHP 8.4 UPGRADE NOTES - PGSQL: . pg_select, the conditions arguments accepts an empty array and is optional. +- POSIX: + . posix_isatty now sets the error number when the file descriptor/stream argument + is invalid. + - SPL: . SplPriorityQueue::insert() and SplPriorityQueue::recoverFromCorruption() now has a tentative return type of true diff --git a/ext/posix/posix.c b/ext/posix/posix.c index f6e028f9a96..787b18ae281 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -521,11 +521,13 @@ PHP_FUNCTION(posix_isatty) /* A valid file descriptor must fit in an int and be positive */ if (fd < 0 || fd > INT_MAX) { + POSIX_G(last_error) = EBADF; RETURN_FALSE; } if (isatty(fd)) { RETURN_TRUE; } else { + POSIX_G(last_error) = errno; RETURN_FALSE; } } diff --git a/ext/posix/tests/posix_isatty_value_errors.phpt b/ext/posix/tests/posix_isatty_value_errors.phpt index 19dfe62c930..e57564e7eac 100644 --- a/ext/posix/tests/posix_isatty_value_errors.phpt +++ b/ext/posix/tests/posix_isatty_value_errors.phpt @@ -11,13 +11,19 @@ if (PHP_INT_SIZE != 8) die('skip C int is same size as zend_long'); $values = [ -1, + 10024, 2**50+1, ]; foreach ($values as $value) { var_dump(posix_isatty($value)); + var_dump(posix_strerror(posix_get_last_error())); } ?> --EXPECT-- bool(false) +string(19) "Bad file descriptor" bool(false) +string(19) "Bad file descriptor" +bool(false) +string(19) "Bad file descriptor"