Skip to content

Commit 695861e

Browse files
committed
changes from review
1 parent 45270c6 commit 695861e

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

ext/sockets/sockets.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ PHP_FUNCTION(socket_set_option)
18111811
HashTable *opt_ht;
18121812
zval *l_onoff, *l_linger;
18131813
zval *sec, *usec;
1814-
bool failed;
1814+
bool failed;
18151815

18161816
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollz", &arg1, socket_ce, &level, &optname, &arg4) == FAILURE) {
18171817
RETURN_THROWS();
@@ -1872,6 +1872,11 @@ PHP_FUNCTION(socket_set_option)
18721872
const char l_onoff_key[] = "l_onoff";
18731873
const char l_linger_key[] = "l_linger";
18741874

1875+
if (Z_TYPE_P(arg4) != IS_ARRAY) {
1876+
zend_argument_type_error(4, "must be of type array, %s given", zend_zval_value_name(arg4));
1877+
RETURN_THROWS();
1878+
}
1879+
18751880
convert_to_array(arg4);
18761881
opt_ht = Z_ARRVAL_P(arg4);
18771882

@@ -1886,12 +1891,22 @@ PHP_FUNCTION(socket_set_option)
18861891

18871892
zend_long zl_onoff = zval_try_get_long(l_onoff, &failed);
18881893
if (failed) {
1889-
zend_argument_type_error(4, "\"%s\" must be an int, %s given", l_onoff_key, zend_zval_value_name(l_onoff));
1894+
zend_argument_type_error(4, "\"%s\" must be of type int, %s given", l_onoff_key, zend_zval_value_name(l_onoff));
18901895
RETURN_THROWS();
18911896
}
18921897
zend_long zl_linger = zval_try_get_long(l_linger, &failed);
18931898
if (failed) {
1894-
zend_argument_type_error(4, "\"%s\" must be an int, %s given", l_linger_key, zend_zval_value_name(l_linger));
1899+
zend_argument_type_error(4, "\"%s\" must be of type int, %s given", l_linger_key, zend_zval_value_name(l_linger));
1900+
RETURN_THROWS();
1901+
}
1902+
1903+
if (zl_onoff < 0 || zl_onoff > USHRT_MAX) {
1904+
zend_argument_value_error(4, "\"%s\" must be between 0 and %u", l_onoff_key, USHRT_MAX);
1905+
RETURN_THROWS();
1906+
}
1907+
1908+
if (zl_linger < 0 || zl_linger > USHRT_MAX) {
1909+
zend_argument_value_error(4, "\"%s\" must be between 0 and %d", l_linger, USHRT_MAX);
18951910
RETURN_THROWS();
18961911
}
18971912

@@ -1907,7 +1922,11 @@ PHP_FUNCTION(socket_set_option)
19071922
case SO_SNDTIMEO: {
19081923
const char sec_key[] = "sec";
19091924
const char usec_key[] = "usec";
1910-
bool failed;
1925+
1926+
if (Z_TYPE_P(arg4) != IS_ARRAY) {
1927+
zend_argument_type_error(4, "must be of type array, %s given", zend_zval_value_name(arg4));
1928+
RETURN_THROWS();
1929+
}
19111930

19121931
convert_to_array(arg4);
19131932
opt_ht = Z_ARRVAL_P(arg4);
@@ -1923,12 +1942,12 @@ PHP_FUNCTION(socket_set_option)
19231942

19241943
zend_long zsec = zval_try_get_long(sec, &failed);
19251944
if (failed) {
1926-
zend_argument_type_error(4, "\"%s\" must be an int, %s given", sec_key, zend_zval_value_name(sec));
1945+
zend_argument_type_error(4, "\"%s\" must be of type int, %s given", sec_key, zend_zval_value_name(sec));
19271946
RETURN_THROWS();
19281947
}
19291948
zend_long zusec = zval_try_get_long(usec, &failed);
19301949
if (failed) {
1931-
zend_argument_type_error(4, "\"%s\" must be an int, %s given", usec_key, zend_zval_value_name(usec));
1950+
zend_argument_type_error(4, "\"%s\" must be of type int, %s given", usec_key, zend_zval_value_name(usec));
19321951
RETURN_THROWS();
19331952
}
19341953
#ifndef PHP_WIN32
@@ -1991,7 +2010,7 @@ PHP_FUNCTION(socket_set_option)
19912010
case SO_ATTACH_REUSEPORT_CBPF: {
19922011
zend_long fval = zval_try_get_long(arg4, &failed);
19932012
if (failed) {
1994-
zend_argument_type_error(4, "must be an int, %s given", zend_zval_value_name(arg4));
2013+
zend_argument_type_error(4, "must be of type int, %s given", zend_zval_value_name(arg4));
19952014
RETURN_THROWS();
19962015
}
19972016

ext/sockets/tests/socket_reuseport_cbpf.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ socket_close($socket);
3131
--EXPECT--
3232
bool(true)
3333
bool(true)
34-
socket_set_option(): Argument #4 ($value) must be an int, array given
34+
socket_set_option(): Argument #4 ($value) must be of type int, array given
3535
bool(true)
3636
bool(true)

ext/sockets/tests/socket_set_option_timeo_error.phpt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ $options_2 = array("sec" => new stdClass(), "usec" => "1");
1313
$options_3 = array("l_onoff" => "aaaa", "l_linger" => "1");
1414
$options_4 = array("l_onoff" => "1", "l_linger" => []);
1515

16+
try {
17+
socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, new stdClass);
18+
} catch (\TypeError $e) {
19+
echo $e->getMessage() . PHP_EOL;
20+
}
21+
1622
try {
1723
socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, $options_1);
1824
} catch (\TypeError $e) {
@@ -24,6 +30,11 @@ try {
2430
} catch (\TypeError $e) {
2531
echo $e->getMessage() . PHP_EOL;
2632
}
33+
try {
34+
socket_set_option( $socket, SOL_SOCKET, SO_LINGER, "not good");
35+
} catch (\TypeError $e) {
36+
echo $e->getMessage() . PHP_EOL;
37+
}
2738
try {
2839
socket_set_option( $socket, SOL_SOCKET, SO_LINGER, $options_3);
2940
} catch (\TypeError $e) {
@@ -36,7 +47,9 @@ try {
3647
}
3748
?>
3849
--EXPECT--
39-
socket_set_option(): Argument #4 ($value) "usec" must be an int, string given
40-
socket_set_option(): Argument #4 ($value) "sec" must be an int, stdClass given
41-
socket_set_option(): Argument #4 ($value) "l_onoff" must be an int, string given
42-
socket_set_option(): Argument #4 ($value) "l_linger" must be an int, array given
50+
socket_set_option(): Argument #4 ($value) must be of type array, stdClass given
51+
socket_set_option(): Argument #4 ($value) "usec" must be of type int, string given
52+
socket_set_option(): Argument #4 ($value) "sec" must be of type int, stdClass given
53+
socket_set_option(): Argument #4 ($value) must be of type array, string given
54+
socket_set_option(): Argument #4 ($value) "l_onoff" must be of type int, string given
55+
socket_set_option(): Argument #4 ($value) "l_linger" must be of type int, array given

0 commit comments

Comments
 (0)