From 8da9d46303868924d04040f894314309dcc0c674 Mon Sep 17 00:00:00 2001 From: lacatoire Date: Sun, 15 Nov 2026 17:18:54 +0100 Subject: [PATCH] Fix posix_ttyname() returning the stdin terminal for an invalid argument type After warning that the argument is neither an int nor a resource, posix_ttyname() converted it with zval_get_long(). A string, array or object therefore became file descriptor 0 or 1, and the function returned the name of the terminal. Return false after the warning instead, as posix_isatty() already does. --- ext/posix/posix.c | 2 +- ...osix_ttyname_invalid_type_no_fallback.phpt | 24 +++++++++++++++++++ ext/posix/tests/posix_ttyname_manual_zpp.phpt | 2 -- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 ext/posix/tests/posix_ttyname_invalid_type_no_fallback.phpt diff --git a/ext/posix/posix.c b/ext/posix/posix.c index b659207f67b6..a65f545e44bd 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -468,7 +468,7 @@ PHP_FUNCTION(posix_ttyname) if (!zend_parse_arg_long(z_fd, &fd, /* is_null */ NULL, /* check_null */ false, /* arg_num */ 1)) { php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be of type int|resource, %s given", zend_zval_value_name(z_fd)); - fd = zval_get_long(z_fd); + RETURN_FALSE; } /* fd must fit in an int and be positive */ if (fd < 0 || fd > INT_MAX) { diff --git a/ext/posix/tests/posix_ttyname_invalid_type_no_fallback.phpt b/ext/posix/tests/posix_ttyname_invalid_type_no_fallback.phpt new file mode 100644 index 000000000000..be881e79604d --- /dev/null +++ b/ext/posix/tests/posix_ttyname_invalid_type_no_fallback.phpt @@ -0,0 +1,24 @@ +--TEST-- +posix_ttyname(): invalid argument type does not fall back to file descriptor 0 +--EXTENSIONS-- +posix +--FILE-- + +--EXPECTF-- +Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, string given in %s on line %d +bool(false) + +Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, array given in %s on line %d +bool(false) + +Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, stdClass given in %s on line %d +bool(false) + +Warning: posix_isatty(): Argument #1 ($file_descriptor) must be of type int|resource, string given in %s on line %d +bool(false) diff --git a/ext/posix/tests/posix_ttyname_manual_zpp.phpt b/ext/posix/tests/posix_ttyname_manual_zpp.phpt index 0fd24efd8f81..271924865c38 100644 --- a/ext/posix/tests/posix_ttyname_manual_zpp.phpt +++ b/ext/posix/tests/posix_ttyname_manual_zpp.phpt @@ -59,12 +59,10 @@ bool(false) class: Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, stdClass given in %s on line %d -Warning: Object of class stdClass could not be converted to int in %s on line %d bool(false) stringable class: Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, classWithToString given in %s on line %d -Warning: Object of class classWithToString could not be converted to int in %s on line %d bool(false) int castable class: Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, GMP given in %s on line %d