diff --git a/NEWS b/NEWS index a66c4c7b3a9..23980a36ca7 100644 --- a/NEWS +++ b/NEWS @@ -54,6 +54,9 @@ PHP NEWS - POSIX: . Added POSIX_SC_OPEN_MAX constant to get the number of file descriptors a process can handle. (David Carlier) + . posix_ttyname() sets last_error to EBADF on invalid file descriptors, + posix_isatty() raises E_WARNING on invalid file descriptors, + posix_fpathconf checks invalid file descriptors. (David Carlier) - Random: . Moves from /dev/urandom usage to arc4random_buf on Haiku. (David Carlier) diff --git a/UPGRADING b/UPGRADING index 4a1032e4d98..573d09152f7 100644 --- a/UPGRADING +++ b/UPGRADING @@ -115,6 +115,14 @@ PHP 8.5 UPGRADE NOTES - PGSQL: . pg_copy_from also supports inputs as Iterable. +- POSIX: + . posix_ttyname sets last_error to EBADF when encountering + an invalid file descriptor. + . posix_isatty raises an E_WARNING message when encountering + an invalid file descriptor. + . posix_fpathconf checks invalid file descriptors and sets + last_error to EBADF and raises an E_WARNING message. + ======================================== 6. New Functions ======================================== diff --git a/ext/posix/posix.c b/ext/posix/posix.c index fc4928d5d2c..9932b402077 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -470,6 +470,7 @@ PHP_FUNCTION(posix_ttyname) /* fd must fit in an int and be positive */ if (fd < 0 || fd > INT_MAX) { php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be between 0 and %d", INT_MAX); + POSIX_G(last_error) = EBADF; RETURN_FALSE; } } @@ -532,6 +533,7 @@ PHP_FUNCTION(posix_isatty) /* A valid file descriptor must fit in an int and be positive */ if (fd < 0 || fd > INT_MAX) { + php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be between 0 and %d", INT_MAX); POSIX_G(last_error) = EBADF; RETURN_FALSE; } @@ -1325,6 +1327,12 @@ PHP_FUNCTION(posix_fpathconf) RETURN_THROWS(); } } + /* fd must fit in an int and be positive */ + if (fd < 0 || fd > INT_MAX) { + php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be between 0 and %d", INT_MAX); + POSIX_G(last_error) = EBADF; + RETURN_FALSE; + } ret = fpathconf(fd, name); diff --git a/ext/posix/tests/posix_fpathconf.phpt b/ext/posix/tests/posix_fpathconf.phpt index c1140941601..4225852c3a0 100644 --- a/ext/posix/tests/posix_fpathconf.phpt +++ b/ext/posix/tests/posix_fpathconf.phpt @@ -10,6 +10,7 @@ if (!function_exists("posix_pathconf")) die("skip only platforms with posix_path --EXPECTF-- + +Warning: posix_fpathconf(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d bool(false) bool(true) +string(19) "Bad file descriptor" posix_fpathconf(): Argument #1 ($file_descriptor) must be of type int|resource, string given int(%d) diff --git a/ext/posix/tests/posix_isatty_value_errors.phpt b/ext/posix/tests/posix_isatty_value_errors.phpt index e57564e7eac..599d892de0d 100644 --- a/ext/posix/tests/posix_isatty_value_errors.phpt +++ b/ext/posix/tests/posix_isatty_value_errors.phpt @@ -20,10 +20,14 @@ foreach ($values as $value) { var_dump(posix_strerror(posix_get_last_error())); } ?> ---EXPECT-- +--EXPECTF-- + +Warning: posix_isatty(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d bool(false) string(19) "Bad file descriptor" bool(false) string(19) "Bad file descriptor" + +Warning: posix_isatty(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d bool(false) string(19) "Bad file descriptor" diff --git a/ext/posix/tests/posix_ttyname_value_errors.phpt b/ext/posix/tests/posix_ttyname_value_errors.phpt index 0f0c853458f..31014fd6f3c 100644 --- a/ext/posix/tests/posix_ttyname_value_errors.phpt +++ b/ext/posix/tests/posix_ttyname_value_errors.phpt @@ -16,11 +16,14 @@ $values = [ foreach ($values as $value) { var_dump(posix_ttyname($value)); + var_dump(posix_strerror(posix_get_last_error())); } ?> --EXPECTF-- Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d bool(false) +string(19) "Bad file descriptor" Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d bool(false) +string(19) "Bad file descriptor"