Skip to content

Commit 668f0e8

Browse files
committed
Promote warnings to Errors in sockets's extension.
1 parent 1589266 commit 668f0e8

File tree

2 files changed

+59
-60
lines changed

2 files changed

+59
-60
lines changed

ext/sockets/conversions.c

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -241,26 +241,26 @@ static void from_zval_write_aggregation(const zval *container,
241241
zval *elem;
242242

243243
if (Z_TYPE_P(container) != IS_ARRAY) {
244-
do_from_zval_err(ctx, "%s", "expected an array here");
244+
zend_type_error("Expected array");
245+
return;
245246
}
246247

247248
for (descr = descriptors; descr->name != NULL && !ctx->err.has_error; descr++) {
248249
if ((elem = zend_hash_str_find(Z_ARRVAL_P(container),
249250
descr->name, descr->name_size - 1)) != NULL) {
250251

251252
if (descr->from_zval == NULL) {
252-
do_from_zval_err(ctx, "No information on how to convert value "
253-
"of key '%s'", descr->name);
254-
break;
253+
zend_value_error("Impossible to convert value of key '%s'", descr->name);
254+
return;
255255
}
256256

257257
zend_llist_add_element(&ctx->keys, (void*)&descr->name);
258258
descr->from_zval(elem, ((char*)structure) + descr->field_offset, ctx);
259259
zend_llist_remove_tail(&ctx->keys);
260260

261261
} else if (descr->required) {
262-
do_from_zval_err(ctx, "The key '%s' is required", descr->name);
263-
break;
262+
zend_value_error("The key '%s' is required", descr->name);
263+
return;
264264
}
265265
}
266266
}
@@ -339,15 +339,13 @@ static zend_long from_zval_integer_common(const zval *arr_value, ser_context *ct
339339
}
340340

341341
/* if we get here, we don't have a numeric string */
342-
do_from_zval_err(ctx, "expected an integer, but got a non numeric "
343-
"string (possibly from a converted object): '%s'", Z_STRVAL_P(arr_value));
344-
break;
342+
zend_type_error("Expected an integer, received a non numeric string: '%s'", Z_STRVAL_P(arr_value));
343+
return ret;
345344
}
346345

347346
default:
348-
do_from_zval_err(ctx, "%s", "expected an integer, either of a PHP "
349-
"integer type or of a convertible type");
350-
break;
347+
zend_type_error("Expected an integer");
348+
return ret;
351349
}
352350

353351
zval_ptr_dtor(&lzval);
@@ -365,8 +363,7 @@ void from_zval_write_int(const zval *arr_value, char *field, ser_context *ctx)
365363
}
366364

367365
if (lval > INT_MAX || lval < INT_MIN) {
368-
do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
369-
"for a native int");
366+
zend_value_error("Provided PHP integer is out of bounds for a native int");
370367
return;
371368
}
372369

@@ -384,8 +381,7 @@ static void from_zval_write_uint32(const zval *arr_value, char *field, ser_conte
384381
}
385382

386383
if (sizeof(zend_long) > sizeof(uint32_t) && (lval < 0 || lval > 0xFFFFFFFF)) {
387-
do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
388-
"for an unsigned 32-bit integer");
384+
zend_value_error("Provided PHP integer is out of bounds for an unsigned 32-bit integer");
389385
return;
390386
}
391387

@@ -403,8 +399,7 @@ static void from_zval_write_net_uint16(const zval *arr_value, char *field, ser_c
403399
}
404400

405401
if (lval < 0 || lval > 0xFFFF) {
406-
do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
407-
"for an unsigned 16-bit integer");
402+
zend_value_error("Provided PHP integer is out of bounds for an unsigned 16-bit integer");
408403
return;
409404
}
410405

@@ -422,8 +417,7 @@ static void from_zval_write_sa_family(const zval *arr_value, char *field, ser_co
422417
}
423418

424419
if (lval < 0 || lval > (sa_family_t)-1) { /* sa_family_t is unsigned */
425-
do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
426-
"for a sa_family_t value");
420+
zend_value_error("Provided PHP integer is out of bounds for a sa_family_t value");
427421
return;
428422
}
429423

@@ -443,8 +437,7 @@ static void from_zval_write_pid_t(const zval *arr_value, char *field, ser_contex
443437
}
444438

445439
if (lval < 0 || (pid_t)lval != lval) { /* pid_t is signed */
446-
do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
447-
"for a pid_t value");
440+
zend_value_error("Provided PHP integer is out of bounds for a pid_t value");
448441
return;
449442
}
450443

@@ -464,14 +457,12 @@ static void from_zval_write_uid_t(const zval *arr_value, char *field, ser_contex
464457
/* uid_t can be signed or unsigned (generally unsigned) */
465458
if ((uid_t)-1 > (uid_t)0) {
466459
if (sizeof(zend_long) > sizeof(uid_t) && (lval < 0 || (uid_t)lval != lval)) {
467-
do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
468-
"for a uid_t value");
460+
zend_value_error("Provided PHP integer is out of bounds for a uid_t value");
469461
return;
470462
}
471463
} else {
472464
if (sizeof(zend_long) > sizeof(uid_t) && (uid_t)lval != lval) {
473-
do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
474-
"for a uid_t value");
465+
zend_value_error("Provided PHP integer is out of bounds for a uid_t value");
475466
return;
476467
}
477468
}
@@ -546,6 +537,7 @@ static void from_zval_write_sin_addr(const zval *zaddr_str, char *inaddr, ser_co
546537
memcpy(inaddr, &saddr.sin_addr, sizeof saddr.sin_addr);
547538
} else {
548539
/* error already emitted, but let's emit another more relevant */
540+
/* TODO Convert to ValueError? */
549541
do_from_zval_err(ctx, "could not resolve address '%s' to get an AF_INET "
550542
"address", ZSTR_VAL(addr_str));
551543
}
@@ -562,8 +554,7 @@ static void to_zval_read_sin_addr(const char *data, zval *zv, res_context *ctx)
562554
ZVAL_NEW_STR(zv, str);
563555

564556
if (inet_ntop(AF_INET, addr, Z_STRVAL_P(zv), size) == NULL) {
565-
do_to_zval_err(ctx, "could not convert IPv4 address to string "
566-
"(errno %d)", errno);
557+
zend_value_error("Could not convert IPv4 address to string (errno %d)", errno);
567558
return;
568559
}
569560

@@ -596,6 +587,7 @@ static void from_zval_write_sin6_addr(const zval *zaddr_str, char *addr6, ser_co
596587
memcpy(addr6, &saddr6.sin6_addr, sizeof saddr6.sin6_addr);
597588
} else {
598589
/* error already emitted, but let's emit another more relevant */
590+
/* TODO Convert to ValueError? */
599591
do_from_zval_err(ctx, "could not resolve address '%s' to get an AF_INET6 "
600592
"address", Z_STRVAL_P(zaddr_str));
601593
}
@@ -613,8 +605,7 @@ static void to_zval_read_sin6_addr(const char *data, zval *zv, res_context *ctx)
613605
ZVAL_NEW_STR(zv, str);
614606

615607
if (inet_ntop(AF_INET6, addr, Z_STRVAL_P(zv), size) == NULL) {
616-
do_to_zval_err(ctx, "could not convert IPv6 address to string "
617-
"(errno %d)", errno);
608+
zend_value_error("Could not convert IPv6 address to string (errno %d)", errno);
618609
return;
619610
}
620611

@@ -648,13 +639,12 @@ static void from_zval_write_sun_path(const zval *path, char *sockaddr_un_c, ser_
648639
* this is not required, at least on linux for abstract paths. It also
649640
* assumes that the path is not empty */
650641
if (ZSTR_LEN(path_str) == 0) {
651-
do_from_zval_err(ctx, "%s", "the path is cannot be empty");
642+
zend_value_error("The path cannot be empty");
652643
zend_tmp_string_release(tmp_path_str);
653644
return;
654645
}
655646
if (ZSTR_LEN(path_str) >= sizeof(saddr->sun_path)) {
656-
do_from_zval_err(ctx, "the path is too long, the maximum permitted "
657-
"length is %zd", sizeof(saddr->sun_path) - 1);
647+
zend_value_error("The path is too long, maximum permitted length is %zd", sizeof(saddr->sun_path) - 1);
658648
zend_tmp_string_release(tmp_path_str);
659649
return;
660650
}
@@ -670,7 +660,7 @@ static void to_zval_read_sun_path(const char *data, zval *zv, res_context *ctx)
670660

671661
nul_pos = memchr(&saddr->sun_path, '\0', sizeof(saddr->sun_path));
672662
if (nul_pos == NULL) {
673-
do_to_zval_err(ctx, "could not find a NUL in the path");
663+
zend_value_error("Could not find a NUL in the path");
674664
return;
675665
}
676666

@@ -702,7 +692,8 @@ static void from_zval_write_sockaddr_aux(const zval *container,
702692
*sockaddr_len = 0;
703693

704694
if (Z_TYPE_P(container) != IS_ARRAY) {
705-
do_from_zval_err(ctx, "%s", "expected an array here");
695+
/* TODO improve error message? */
696+
zend_type_error("Expected array");
706697
return;
707698
}
708699

@@ -865,16 +856,15 @@ static void from_zval_write_control(const zval *arr,
865856

866857
entry = get_ancillary_reg_entry(level, type);
867858
if (entry == NULL) {
868-
do_from_zval_err(ctx, "cmsghdr with level %d and type %d not supported",
869-
level, type);
859+
zend_value_error("cmsghdr with level %d and type %d not supported", level, type);
870860
return;
871861
}
872862

873863
if (entry->calc_space) {
874864
zval *data_elem;
875865
/* arr must be an array at this point */
876866
if ((data_elem = zend_hash_str_find(Z_ARRVAL_P(arr), "data", sizeof("data") - 1)) == NULL) {
877-
do_from_zval_err(ctx, "cmsghdr should have a 'data' element here");
867+
zend_value_error("cmsghdr should have a 'data' element here");
878868
return;
879869
}
880870
data_len = entry->calc_space(data_elem, ctx);
@@ -919,7 +909,8 @@ static void from_zval_write_control_array(const zval *arr, char *msghdr_c, ser_c
919909
struct msghdr *msg = (struct msghdr*)msghdr_c;
920910

921911
if (Z_TYPE_P(arr) != IS_ARRAY) {
922-
do_from_zval_err(ctx, "%s", "expected an array here");
912+
/* TODO Improve error message */
913+
zend_type_error("Expected array");
923914
return;
924915
}
925916

@@ -961,11 +952,11 @@ static void to_zval_read_cmsg_data(const char *cmsghdr_c, zval *zv, res_context
961952

962953
entry = get_ancillary_reg_entry(cmsg->cmsg_level, cmsg->cmsg_type);
963954
if (entry == NULL) {
964-
do_to_zval_err(ctx, "cmsghdr with level %d and type %d not supported",
965-
cmsg->cmsg_level, cmsg->cmsg_type);
955+
zend_value_error("cmsghdr with level %d and type %d not supported", cmsg->cmsg_level, cmsg->cmsg_type);
966956
return;
967957
}
968958
if (CMSG_LEN(entry->size) > cmsg->cmsg_len) {
959+
/* TODO Convert? */
969960
do_to_zval_err(ctx, "the cmsghdr structure is unexpectedly small; "
970961
"expected a length of at least " ZEND_LONG_FMT ", but got " ZEND_LONG_FMT,
971962
(zend_long)CMSG_LEN(entry->size), (zend_long)cmsg->cmsg_len);
@@ -1057,8 +1048,8 @@ static void from_zval_write_msghdr_buffer_size(const zval *elem, char *msghdr_c,
10571048
}
10581049

10591050
if (lval < 0 || (zend_ulong)lval > MAX_USER_BUFF_SIZE) {
1060-
do_from_zval_err(ctx, "the buffer size must be between 1 and " ZEND_LONG_FMT "; "
1061-
"given " ZEND_LONG_FMT, (zend_long)MAX_USER_BUFF_SIZE, lval);
1051+
zend_value_error("The buffer size must be between 1 and " ZEND_LONG_FMT "; given " ZEND_LONG_FMT,
1052+
(zend_long) MAX_USER_BUFF_SIZE, lval);
10621053
return;
10631054
}
10641055

@@ -1086,7 +1077,8 @@ static void from_zval_write_iov_array(const zval *arr, char *msghdr_c, ser_conte
10861077
struct msghdr *msg = (struct msghdr*)msghdr_c;
10871078

10881079
if (Z_TYPE_P(arr) != IS_ARRAY) {
1089-
do_from_zval_err(ctx, "%s", "expected an array here");
1080+
/* TODO Improve error */
1081+
zend_type_error("Expected array");
10901082
return;
10911083
}
10921084

@@ -1110,7 +1102,7 @@ static void from_zval_write_controllen(const zval *elem, char *msghdr_c, ser_con
11101102
*/
11111103
from_zval_write_uint32(elem, (char*)&len, ctx);
11121104
if (!ctx->err.has_error && len == 0) {
1113-
do_from_zval_err(ctx, "controllen cannot be 0");
1105+
zend_value_error("controllen cannot be 0");
11141106
return;
11151107
}
11161108
msghdr->msg_control = accounted_emalloc(len, ctx);
@@ -1154,6 +1146,7 @@ void from_zval_write_msghdr_recv(const zval *container, char *msghdr_c, ser_cont
11541146
*falsevp = &falsev;
11551147

11561148
if (zend_hash_str_add_ptr(&ctx->params, KEY_FILL_SOCKADDR, sizeof(KEY_FILL_SOCKADDR) - 1, (void *)falsevp) == NULL) {
1149+
/* Should throw? */
11571150
do_from_zval_err(ctx, "could not add fill_sockaddr; this is a bug");
11581151
return;
11591152
}
@@ -1182,12 +1175,12 @@ static void to_zval_read_iov(const char *msghdr_c, zval *zv, res_context *ctx)
11821175
uint32_t i;
11831176

11841177
if (iovlen > UINT_MAX) {
1185-
do_to_zval_err(ctx, "unexpectedly large value for iov_len: %lu",
1186-
(unsigned long)iovlen);
1178+
zend_value_error("Unexpectedly large value for iov_len: %lu", (unsigned long) iovlen);
11871179
}
11881180
array_init_size(zv, (uint32_t)iovlen);
11891181

11901182
if ((recvmsg_ret = zend_hash_str_find_ptr(&ctx->params, KEY_RECVMSG_RET, sizeof(KEY_RECVMSG_RET) - 1)) == NULL) {
1183+
/* Should throw? */
11911184
do_to_zval_err(ctx, "recvmsg_ret not found in params. This is a bug");
11921185
return;
11931186
}
@@ -1228,8 +1221,8 @@ static void from_zval_write_ifindex(const zval *zv, char *uinteger, ser_context
12281221

12291222
if (Z_TYPE_P(zv) == IS_LONG) {
12301223
if (Z_LVAL_P(zv) < 0 || (zend_ulong)Z_LVAL_P(zv) > UINT_MAX) { /* allow 0 (unspecified interface) */
1231-
do_from_zval_err(ctx, "the interface index cannot be negative or "
1232-
"larger than %u; given " ZEND_LONG_FMT, UINT_MAX, Z_LVAL_P(zv));
1224+
zend_value_error("The interface index cannot be negative or larger than %u; given " ZEND_LONG_FMT,
1225+
UINT_MAX, Z_LVAL_P(zv));
12331226
} else {
12341227
ret = (unsigned)Z_LVAL_P(zv);
12351228
}
@@ -1323,13 +1316,14 @@ size_t calculate_scm_rights_space(const zval *arr, ser_context *ctx)
13231316
int num_elems;
13241317

13251318
if (Z_TYPE_P(arr) != IS_ARRAY) {
1326-
do_from_zval_err(ctx, "%s", "expected an array here");
1319+
/* TODO Improve error */
1320+
zend_type_error("Expected array");
13271321
return (size_t)-1;
13281322
}
13291323

13301324
num_elems = zend_hash_num_elements(Z_ARRVAL_P(arr));
13311325
if (num_elems == 0) {
1332-
do_from_zval_err(ctx, "%s", "expected at least one element in this array");
1326+
zend_value_error("Expected at least one element in this array");
13331327
return (size_t)-1;
13341328
}
13351329

@@ -1351,23 +1345,25 @@ static void from_zval_write_fd_array_aux(zval *elem, unsigned i, void **args, se
13511345

13521346
stream = (php_stream *)zend_fetch_resource2_ex(elem, NULL, php_file_le_stream(), php_file_le_pstream());
13531347
if (stream == NULL) {
1354-
do_from_zval_err(ctx, "resource is not a stream or a socket");
1348+
zend_type_error("Resource is not a stream nor a scoket");
13551349
return;
13561350
}
13571351

13581352
if (php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&iarr[i - 1],
13591353
REPORT_ERRORS) == FAILURE) {
1354+
/* Throw? */
13601355
do_from_zval_err(ctx, "cast stream to file descriptor failed");
13611356
return;
13621357
}
13631358
} else {
1364-
do_from_zval_err(ctx, "expected a resource variable");
1359+
zend_type_error("Expected a resource");
13651360
}
13661361
}
13671362
void from_zval_write_fd_array(const zval *arr, char *int_arr, ser_context *ctx)
13681363
{
13691364
if (Z_TYPE_P(arr) != IS_ARRAY) {
1370-
do_from_zval_err(ctx, "%s", "expected an array here");
1365+
/* TODO Improve error */
1366+
zend_type_error("Expected array");
13711367
return;
13721368
}
13731369

@@ -1390,8 +1386,8 @@ void to_zval_read_fd_array(const char *data, zval *zv, res_context *ctx)
13901386
}
13911387

13921388
if (*cmsg_len < data_offset) {
1393-
do_to_zval_err(ctx, "length of cmsg is smaller than its data member "
1394-
"offset (" ZEND_LONG_FMT " vs " ZEND_LONG_FMT ")", (zend_long)*cmsg_len, (zend_long)data_offset);
1389+
zend_value_error("Length of cmsg is smaller than its data member offset (" ZEND_LONG_FMT
1390+
" vs " ZEND_LONG_FMT ")", (zend_long)*cmsg_len, (zend_long)data_offset);
13951391
return;
13961392
}
13971393
num_elems = (*cmsg_len - data_offset) / sizeof(int);

ext/sockets/tests/socket_set_option_in6_pktinfo.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ if (!defined('IPV6_PKTINFO')) {
2121
<?php
2222

2323
$s = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP) or die("err");
24-
var_dump(socket_set_option($s, IPPROTO_IPV6, IPV6_PKTINFO, []));
24+
try {
25+
var_dump(socket_set_option($s, IPPROTO_IPV6, IPV6_PKTINFO, []));
26+
} catch (\ValueError $e) {
27+
echo $e->getMessage() . \PHP_EOL;
28+
}
2529
var_dump(socket_set_option($s, IPPROTO_IPV6, IPV6_PKTINFO, [
2630
"addr" => '::1',
2731
"ifindex" => 0
@@ -31,7 +35,6 @@ var_dump(socket_set_option($s, IPPROTO_IPV6, IPV6_PKTINFO, [
3135
//A work-around with is sort-of possible (with IPV6_2292PKTOPTIONS),
3236
//but not worth it
3337
//var_dump(socket_get_option($s, IPPROTO_IPV6, IPV6_PKTINFO));
34-
--EXPECTF--
35-
Warning: socket_set_option(): error converting user data (path: in6_pktinfo): The key 'addr' is required in %s on line %d
36-
bool(false)
38+
--EXPECT--
39+
The key 'addr' is required
3740
bool(true)

0 commit comments

Comments
 (0)