Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 14 additions & 32 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,7 @@ PHP_FUNCTION(socket_set_option)
#endif
zend_long level, optname;
void *opt_ptr;
HashTable *opt_ht, *opt_props = NULL;
HashTable *opt_ht;
zval *l_onoff, *l_linger;
zval *sec, *usec;

Expand Down Expand Up @@ -1951,39 +1951,30 @@ PHP_FUNCTION(socket_set_option)
const char l_linger_key[] = "l_linger";

if (Z_TYPE_P(arg4) != IS_ARRAY) {
if (UNEXPECTED(Z_TYPE_P(arg4) != IS_OBJECT)) {
zend_argument_type_error(4, "must be of type array when argument #3 ($option) is SO_LINGER, %s given", zend_zval_value_name(arg4));
RETURN_THROWS();
} else {
opt_props = zend_get_properties_for(arg4, ZEND_PROP_PURPOSE_ARRAY_CAST);
opt_ht = opt_props ? opt_props : (HashTable *) &zend_empty_array;
}
} else {
opt_ht = Z_ARRVAL_P(arg4);
zend_argument_type_error(4, "must be of type array when argument #3 ($option) is SO_LINGER, %s given", zend_zval_value_name(arg4));
RETURN_THROWS();
}
opt_ht = Z_ARRVAL_P(arg4);

if ((l_onoff = zend_hash_str_find_ind(opt_ht, l_onoff_key, sizeof(l_onoff_key) - 1)) == NULL) {
zend_release_properties(opt_props);
if ((l_onoff = zend_hash_str_find(opt_ht, l_onoff_key, sizeof(l_onoff_key) - 1)) == NULL) {
zend_argument_value_error(4, "must have key \"%s\"", l_onoff_key);
RETURN_THROWS();
}
if ((l_linger = zend_hash_str_find_ind(opt_ht, l_linger_key, sizeof(l_linger_key) - 1)) == NULL) {
zend_release_properties(opt_props);
if ((l_linger = zend_hash_str_find(opt_ht, l_linger_key, sizeof(l_linger_key) - 1)) == NULL) {
zend_argument_value_error(4, "must have key \"%s\"", l_linger_key);
RETURN_THROWS();
}

zend_long val_lonoff = zval_get_long(l_onoff);
zend_long val_linger = zval_get_long(l_linger);
zend_release_properties(opt_props);

if (val_lonoff < 0 || val_lonoff > USHRT_MAX) {
zend_argument_value_error(4, "\"%s\" must be between 0 and %u", l_onoff_key, USHRT_MAX);
RETURN_THROWS();
}

if (val_linger < 0 || val_linger > USHRT_MAX) {
zend_argument_value_error(4, "\"%s\" must be between 0 and %d", l_linger_key, USHRT_MAX);
zend_argument_value_error(4, "\"%s\" must be between 0 and %d", l_linger, USHRT_MAX);
RETURN_THROWS();
}

Expand All @@ -2001,33 +1992,24 @@ PHP_FUNCTION(socket_set_option)
const char usec_key[] = "usec";

if (Z_TYPE_P(arg4) != IS_ARRAY) {
if (UNEXPECTED(Z_TYPE_P(arg4) != IS_OBJECT)) {
zend_argument_type_error(4, "must be of type array when argument #3 ($option) is %s, %s given",
optname == SO_RCVTIMEO ? "SO_RCVTIMEO" : "SO_SNDTIMEO",
zend_zval_value_name(arg4));
RETURN_THROWS();
} else {
opt_props = zend_get_properties_for(arg4, ZEND_PROP_PURPOSE_ARRAY_CAST);
opt_ht = opt_props ? opt_props : (HashTable *) &zend_empty_array;
}
} else {
opt_ht = Z_ARRVAL_P(arg4);
zend_argument_type_error(4, "must be of type array when argument #3 ($option) is %s, %s given",
optname == SO_RCVTIMEO ? "SO_RCVTIMEO" : "SO_SNDTIMEO",
zend_zval_value_name(arg4));
RETURN_THROWS();
}
opt_ht = Z_ARRVAL_P(arg4);

if ((sec = zend_hash_str_find_ind(opt_ht, sec_key, sizeof(sec_key) - 1)) == NULL) {
zend_release_properties(opt_props);
if ((sec = zend_hash_str_find(opt_ht, sec_key, sizeof(sec_key) - 1)) == NULL) {
zend_argument_value_error(4, "must have key \"%s\"", sec_key);
RETURN_THROWS();
}
if ((usec = zend_hash_str_find_ind(opt_ht, usec_key, sizeof(usec_key) - 1)) == NULL) {
zend_release_properties(opt_props);
if ((usec = zend_hash_str_find(opt_ht, usec_key, sizeof(usec_key) - 1)) == NULL) {
zend_argument_value_error(4, "must have key \"%s\"", usec_key);
RETURN_THROWS();
}

zend_long valsec = zval_get_long(sec);
zend_long valusec = zval_get_long(usec);
zend_release_properties(opt_props);
#ifndef PHP_WIN32
tv.tv_sec = valsec;
tv.tv_usec = valusec;
Expand Down
64 changes: 0 additions & 64 deletions ext/sockets/tests/gh24748.phpt

This file was deleted.

18 changes: 9 additions & 9 deletions ext/sockets/tests/socket_set_option_timeo_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,49 @@ $options_5 = array("l_onoff" => PHP_INT_MAX, "l_linger" => "1");

try {
socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, new stdClass);
} catch (\ValueError $e) {
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, $options_1);
} catch (\TypeError $e) {
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
socket_set_option( $socket, SOL_SOCKET, SO_SNDTIMEO, $options_2);
} catch (\TypeError $e) {
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, "not good");
} catch (\TypeError $e) {
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_set_option( $socket, SOL_SOCKET, SO_LINGER, "not good neither");
} catch (\TypeError $e) {
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_set_option( $socket, SOL_SOCKET, SO_LINGER, $options_3);
} catch (\TypeError $e) {
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_set_option( $socket, SOL_SOCKET, SO_LINGER, $options_4);
} catch (\TypeError $e) {
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_set_option( $socket, SOL_SOCKET, SO_LINGER, $options_5);
} catch (\ValueError $e) {
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
?>
--EXPECTF--
socket_set_option(): Argument #4 ($value) must have key "sec"
socket_set_option(): Argument #4 ($value) must be of type array when argument #3 ($option) is SO_RCVTIMEO, stdClass given

Warning: Object of class stdClass could not be converted to int in %s on line %d
socket_set_option(): Argument #4 ($value) must be of type array when argument #3 ($option) is SO_RCVTIMEO, string given
Expand Down
Loading