Skip to content

Commit 54767b1

Browse files
committed
Manually handle int ZPP for posix_isatty()/posix_ttyname()
1 parent b15d0a9 commit 54767b1

9 files changed

+163
-240
lines changed

ext/posix/posix.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ PHP_FUNCTION(posix_ctermid)
417417
/* }}} */
418418

419419
/* Checks if the provides resource is a stream and if it provides a file descriptor */
420-
static int php_posix_stream_get_fd(zval *zfp, int *fd) /* {{{ */
420+
static int php_posix_stream_get_fd(zval *zfp, zend_long *fd) /* {{{ */
421421
{
422422
php_stream *stream;
423423

@@ -444,7 +444,7 @@ PHP_FUNCTION(posix_ttyname)
444444
{
445445
zval *z_fd;
446446
char *p;
447-
int fd;
447+
zend_long fd;
448448
#if defined(ZTS) && defined(HAVE_TTYNAME_R) && defined(_SC_TTY_NAME_MAX)
449449
zend_long buflen;
450450
#endif
@@ -453,14 +453,16 @@ PHP_FUNCTION(posix_ttyname)
453453
Z_PARAM_ZVAL(z_fd)
454454
ZEND_PARSE_PARAMETERS_END();
455455

456-
switch (Z_TYPE_P(z_fd)) {
457-
case IS_RESOURCE:
458-
if (!php_posix_stream_get_fd(z_fd, &fd)) {
459-
RETURN_FALSE;
460-
}
461-
break;
462-
default:
456+
if (Z_TYPE_P(z_fd) == IS_RESOURCE) {
457+
if (!php_posix_stream_get_fd(z_fd, &fd)) {
458+
RETURN_FALSE;
459+
}
460+
} else {
461+
if (!zend_parse_arg_long(z_fd, &fd, /* is_null */ false, /* check_null */ false, /* arg_num */ 1)) {
462+
php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be of type int|resource, %s given",
463+
zend_zval_type_name(z_fd));
463464
fd = zval_get_long(z_fd);
465+
}
464466
}
465467
#if defined(ZTS) && defined(HAVE_TTYNAME_R) && defined(_SC_TTY_NAME_MAX)
466468
buflen = sysconf(_SC_TTY_NAME_MAX);
@@ -490,20 +492,22 @@ PHP_FUNCTION(posix_ttyname)
490492
PHP_FUNCTION(posix_isatty)
491493
{
492494
zval *z_fd;
493-
int fd;
495+
zend_long fd;
494496

495497
ZEND_PARSE_PARAMETERS_START(1, 1)
496498
Z_PARAM_ZVAL(z_fd)
497499
ZEND_PARSE_PARAMETERS_END();
498500

499-
switch (Z_TYPE_P(z_fd)) {
500-
case IS_RESOURCE:
501-
if (!php_posix_stream_get_fd(z_fd, &fd)) {
502-
RETURN_FALSE;
503-
}
504-
break;
505-
default:
501+
if (Z_TYPE_P(z_fd) == IS_RESOURCE) {
502+
if (!php_posix_stream_get_fd(z_fd, &fd)) {
503+
RETURN_FALSE;
504+
}
505+
} else {
506+
if (!zend_parse_arg_long(z_fd, &fd, /* is_null */ false, /* check_null */ false, /* arg_num */ 1)) {
507+
php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be of type int|resource, %s given",
508+
zend_zval_type_name(z_fd));
506509
fd = zval_get_long(z_fd);
510+
}
507511
}
508512

509513
if (isatty(fd)) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
posix_isatty(): manually validating int ZPP param
3+
--EXTENSIONS--
4+
posix
5+
gmp
6+
--FILE--
7+
<?php
8+
9+
class classWithToString {
10+
public function __toString() {
11+
return "string";
12+
}
13+
}
14+
15+
$types = [
16+
'null' => null,
17+
'false' => false,
18+
'true' => true,
19+
'int' => 1,
20+
'float no decimal' => 1.0,
21+
'float decimal' => 5.5,
22+
'string int' => "1",
23+
'string float no decimal' => "1.0",
24+
'string float decimal' => "5.5",
25+
'string' => "Hello",
26+
'array' => [],
27+
'class' => new stdClass(),
28+
'stringable class' => new classWithToString(),
29+
'int castable class' => gmp_init(1),
30+
];
31+
32+
foreach ($types as $description => $type) {
33+
echo $description, ':';
34+
var_dump(posix_isatty($type));
35+
}
36+
?>
37+
--EXPECTF--
38+
null:
39+
Deprecated: posix_isatty(): Passing null to parameter #1 ($file_descriptor) of type int is deprecated in %s on line %d
40+
bool(false)
41+
false:bool(false)
42+
true:bool(false)
43+
int:bool(false)
44+
float no decimal:bool(false)
45+
float decimal:
46+
Deprecated: Implicit conversion from float 5.5 to int loses precision in %s on line %d
47+
bool(false)
48+
string int:bool(false)
49+
string float no decimal:bool(false)
50+
string float decimal:
51+
Deprecated: Implicit conversion from float-string "5.5" to int loses precision in %s on line %d
52+
bool(false)
53+
string:
54+
Warning: posix_isatty(): Argument #1 ($file_descriptor) must be of type int|resource, string given in %s on line %d
55+
bool(false)
56+
array:
57+
Warning: posix_isatty(): Argument #1 ($file_descriptor) must be of type int|resource, array given in %s on line %d
58+
bool(false)
59+
class:
60+
Warning: posix_isatty(): Argument #1 ($file_descriptor) must be of type int|resource, stdClass given in %s on line %d
61+
62+
Warning: Object of class stdClass could not be converted to int in %s on line %d
63+
bool(false)
64+
stringable class:
65+
Warning: posix_isatty(): Argument #1 ($file_descriptor) must be of type int|resource, classWithToString given in %s on line %d
66+
67+
Warning: Object of class classWithToString could not be converted to int in %s on line %d
68+
bool(false)
69+
int castable class:
70+
Warning: posix_isatty(): Argument #1 ($file_descriptor) must be of type int|resource, GMP given in %s on line %d
71+
bool(false)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
posix_ttyname(): manually validating int ZPP param
3+
--EXTENSIONS--
4+
posix
5+
gmp
6+
--FILE--
7+
<?php
8+
9+
class classWithToString {
10+
public function __toString() {
11+
return "string";
12+
}
13+
}
14+
15+
$types = [
16+
'null' => null,
17+
'false' => false,
18+
'true' => true,
19+
'int' => 1,
20+
'float no decimal' => 1.0,
21+
'float decimal' => 5.5,
22+
'string int' => "1",
23+
'string float no decimal' => "1.0",
24+
'string float decimal' => "5.5",
25+
'string' => "Hello",
26+
'array' => [],
27+
'class' => new stdClass(),
28+
'stringable class' => new classWithToString(),
29+
'int castable class' => gmp_init(1),
30+
];
31+
32+
foreach ($types as $description => $type) {
33+
echo $description, ':';
34+
var_dump(posix_ttyname($type));
35+
}
36+
?>
37+
--EXPECTF--
38+
null:
39+
Deprecated: posix_ttyname(): Passing null to parameter #1 ($file_descriptor) of type int is deprecated in %s on line %d
40+
bool(false)
41+
false:bool(false)
42+
true:bool(false)
43+
int:bool(false)
44+
float no decimal:bool(false)
45+
float decimal:
46+
Deprecated: Implicit conversion from float 5.5 to int loses precision in %s on line %d
47+
bool(false)
48+
string int:bool(false)
49+
string float no decimal:bool(false)
50+
string float decimal:
51+
Deprecated: Implicit conversion from float-string "5.5" to int loses precision in %s on line %d
52+
bool(false)
53+
string:
54+
Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, string given in %s on line %d
55+
bool(false)
56+
array:
57+
Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, array given in %s on line %d
58+
bool(false)
59+
class:
60+
Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, stdClass given in %s on line %d
61+
62+
Warning: Object of class stdClass could not be converted to int in %s on line %d
63+
bool(false)
64+
stringable class:
65+
Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, classWithToString given in %s on line %d
66+
67+
Warning: Object of class classWithToString could not be converted to int in %s on line %d
68+
bool(false)
69+
int castable class:
70+
Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be of type int|resource, GMP given in %s on line %d
71+
bool(false)

ext/posix/tests/posix_ttyname_variation1.phpt

Lines changed: 0 additions & 37 deletions
This file was deleted.

ext/posix/tests/posix_ttyname_variation2.phpt

Lines changed: 0 additions & 34 deletions
This file was deleted.

ext/posix/tests/posix_ttyname_variation3.phpt

Lines changed: 0 additions & 41 deletions
This file was deleted.

ext/posix/tests/posix_ttyname_variation5.phpt

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)