Skip to content

Commit 36fa36a

Browse files
committed
ext/posix posix_ttyname/posix_isatty fd error handling update.
Set to `EBADF` errno for posix_ttyname when out of ranges. posix_fpathconf now returns false on out of range file descriptors.
1 parent 4d140f7 commit 36fa36a

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

ext/posix/posix.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ PHP_FUNCTION(posix_ttyname)
470470
/* fd must fit in an int and be positive */
471471
if (fd < 0 || fd > INT_MAX) {
472472
php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be between 0 and %d", INT_MAX);
473+
POSIX_G(last_error) = EBADF;
473474
RETURN_FALSE;
474475
}
475476
}
@@ -532,6 +533,7 @@ PHP_FUNCTION(posix_isatty)
532533

533534
/* A valid file descriptor must fit in an int and be positive */
534535
if (fd < 0 || fd > INT_MAX) {
536+
php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be between 0 and %d", INT_MAX);
535537
POSIX_G(last_error) = EBADF;
536538
RETURN_FALSE;
537539
}
@@ -1325,6 +1327,12 @@ PHP_FUNCTION(posix_fpathconf)
13251327
RETURN_THROWS();
13261328
}
13271329
}
1330+
/* fd must fit in an int and be positive */
1331+
if (fd < 0 || fd > INT_MAX) {
1332+
php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be between 0 and %d", INT_MAX);
1333+
POSIX_G(last_error) = EBADF;
1334+
RETURN_FALSE;
1335+
}
13281336

13291337
ret = fpathconf(fd, name);
13301338

ext/posix/tests/posix_fpathconf.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if (!function_exists("posix_pathconf")) die("skip only platforms with posix_path
1010
<?php
1111
var_dump(posix_fpathconf(-1, POSIX_PC_PATH_MAX));
1212
var_dump(posix_errno() != 0);
13+
var_dump(posix_strerror(posix_errno()));
1314
try {
1415
posix_fpathconf("string arg", POSIX_PC_PATH_MAX);
1516
} catch (\TypeError $e) {
@@ -20,7 +21,10 @@ var_dump(posix_fpathconf($fd, POSIX_PC_PATH_MAX));
2021
fclose($fd);
2122
?>
2223
--EXPECTF--
24+
25+
Warning: posix_fpathconf(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d
2326
bool(false)
2427
bool(true)
28+
string(19) "Bad file descriptor"
2529
posix_fpathconf(): Argument #1 ($file_descriptor) must be of type int|resource, string given
2630
int(%d)

ext/posix/tests/posix_isatty_value_errors.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ foreach ($values as $value) {
2020
var_dump(posix_strerror(posix_get_last_error()));
2121
}
2222
?>
23-
--EXPECT--
23+
--EXPECTF--
24+
25+
Warning: posix_isatty(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d
2426
bool(false)
2527
string(19) "Bad file descriptor"
2628
bool(false)
2729
string(19) "Bad file descriptor"
30+
31+
Warning: posix_isatty(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d
2832
bool(false)
2933
string(19) "Bad file descriptor"

0 commit comments

Comments
 (0)