Skip to content

Commit 33dbfa2

Browse files
committed
Address review
1 parent bb75f5d commit 33dbfa2

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

ext/sockets/conversions.c

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,15 @@ static void from_zval_write_aggregation(const zval *container,
249249
if ((elem = zend_hash_str_find(Z_ARRVAL_P(container),
250250
descr->name, descr->name_size - 1)) != NULL) {
251251

252-
if (descr->from_zval == NULL) {
253-
zend_value_error("Impossible to convert value of key '%s'", descr->name);
254-
return;
255-
}
252+
ZEND_ASSERT(descr->from_zval);
256253

257254
zend_llist_add_element(&ctx->keys, (void*)&descr->name);
258255
descr->from_zval(elem, ((char*)structure) + descr->field_offset, ctx);
259256
zend_llist_remove_tail(&ctx->keys);
260257

261258
} else if (descr->required) {
259+
ctx->err.has_error = 1;
260+
ctx->err.should_free = 1;
262261
zend_value_error("The key '%s' is required", descr->name);
263262
return;
264263
}
@@ -339,11 +338,15 @@ static zend_long from_zval_integer_common(const zval *arr_value, ser_context *ct
339338
}
340339

341340
/* if we get here, we don't have a numeric string */
341+
ctx->err.has_error = 1;
342+
ctx->err.should_free = 1;
342343
zend_type_error("Expected an integer, received a non numeric string: '%s'", Z_STRVAL_P(arr_value));
343344
return ret;
344345
}
345346

346347
default:
348+
ctx->err.has_error = 1;
349+
ctx->err.should_free = 1;
347350
zend_type_error("Expected an integer");
348351
return ret;
349352
}
@@ -363,6 +366,8 @@ void from_zval_write_int(const zval *arr_value, char *field, ser_context *ctx)
363366
}
364367

365368
if (lval > INT_MAX || lval < INT_MIN) {
369+
ctx->err.has_error = 1;
370+
ctx->err.should_free = 1;
366371
zend_value_error("Provided PHP integer is out of bounds for a native int");
367372
return;
368373
}
@@ -381,6 +386,8 @@ static void from_zval_write_uint32(const zval *arr_value, char *field, ser_conte
381386
}
382387

383388
if (sizeof(zend_long) > sizeof(uint32_t) && (lval < 0 || lval > 0xFFFFFFFF)) {
389+
ctx->err.has_error = 1;
390+
ctx->err.should_free = 1;
384391
zend_value_error("Provided PHP integer is out of bounds for an unsigned 32-bit integer");
385392
return;
386393
}
@@ -399,6 +406,8 @@ static void from_zval_write_net_uint16(const zval *arr_value, char *field, ser_c
399406
}
400407

401408
if (lval < 0 || lval > 0xFFFF) {
409+
ctx->err.has_error = 1;
410+
ctx->err.should_free = 1;
402411
zend_value_error("Provided PHP integer is out of bounds for an unsigned 16-bit integer");
403412
return;
404413
}
@@ -417,6 +426,8 @@ static void from_zval_write_sa_family(const zval *arr_value, char *field, ser_co
417426
}
418427

419428
if (lval < 0 || lval > (sa_family_t)-1) { /* sa_family_t is unsigned */
429+
ctx->err.has_error = 1;
430+
ctx->err.should_free = 1;
420431
zend_value_error("Provided PHP integer is out of bounds for a sa_family_t value");
421432
return;
422433
}
@@ -437,6 +448,8 @@ static void from_zval_write_pid_t(const zval *arr_value, char *field, ser_contex
437448
}
438449

439450
if (lval < 0 || (pid_t)lval != lval) { /* pid_t is signed */
451+
ctx->err.has_error = 1;
452+
ctx->err.should_free = 1;
440453
zend_value_error("Provided PHP integer is out of bounds for a pid_t value");
441454
return;
442455
}
@@ -457,11 +470,15 @@ static void from_zval_write_uid_t(const zval *arr_value, char *field, ser_contex
457470
/* uid_t can be signed or unsigned (generally unsigned) */
458471
if ((uid_t)-1 > (uid_t)0) {
459472
if (sizeof(zend_long) > sizeof(uid_t) && (lval < 0 || (uid_t)lval != lval)) {
473+
ctx->err.has_error = 1;
474+
ctx->err.should_free = 1;
460475
zend_value_error("Provided PHP integer is out of bounds for a uid_t value");
461476
return;
462477
}
463478
} else {
464479
if (sizeof(zend_long) > sizeof(uid_t) && (uid_t)lval != lval) {
480+
ctx->err.has_error = 1;
481+
ctx->err.should_free = 1;
465482
zend_value_error("Provided PHP integer is out of bounds for a uid_t value");
466483
return;
467484
}
@@ -554,6 +571,8 @@ static void to_zval_read_sin_addr(const char *data, zval *zv, res_context *ctx)
554571
ZVAL_NEW_STR(zv, str);
555572

556573
if (inet_ntop(AF_INET, addr, Z_STRVAL_P(zv), size) == NULL) {
574+
ctx->err.has_error = 1;
575+
ctx->err.should_free = 1;
557576
zend_value_error("Could not convert IPv4 address to string (errno %d)", errno);
558577
return;
559578
}
@@ -605,6 +624,8 @@ static void to_zval_read_sin6_addr(const char *data, zval *zv, res_context *ctx)
605624
ZVAL_NEW_STR(zv, str);
606625

607626
if (inet_ntop(AF_INET6, addr, Z_STRVAL_P(zv), size) == NULL) {
627+
ctx->err.has_error = 1;
628+
ctx->err.should_free = 1;
608629
zend_value_error("Could not convert IPv6 address to string (errno %d)", errno);
609630
return;
610631
}
@@ -639,11 +660,15 @@ static void from_zval_write_sun_path(const zval *path, char *sockaddr_un_c, ser_
639660
* this is not required, at least on linux for abstract paths. It also
640661
* assumes that the path is not empty */
641662
if (ZSTR_LEN(path_str) == 0) {
663+
ctx->err.has_error = 1;
664+
ctx->err.should_free = 1;
642665
zend_value_error("The path cannot be empty");
643666
zend_tmp_string_release(tmp_path_str);
644667
return;
645668
}
646669
if (ZSTR_LEN(path_str) >= sizeof(saddr->sun_path)) {
670+
ctx->err.has_error = 1;
671+
ctx->err.should_free = 1;
647672
zend_value_error("The path is too long, maximum permitted length is %zd", sizeof(saddr->sun_path) - 1);
648673
zend_tmp_string_release(tmp_path_str);
649674
return;
@@ -660,6 +685,8 @@ static void to_zval_read_sun_path(const char *data, zval *zv, res_context *ctx)
660685

661686
nul_pos = memchr(&saddr->sun_path, '\0', sizeof(saddr->sun_path));
662687
if (nul_pos == NULL) {
688+
ctx->err.has_error = 1;
689+
ctx->err.should_free = 1;
663690
zend_value_error("Could not find a NUL in the path");
664691
return;
665692
}
@@ -693,6 +720,8 @@ static void from_zval_write_sockaddr_aux(const zval *container,
693720

694721
if (Z_TYPE_P(container) != IS_ARRAY) {
695722
/* TODO improve error message? */
723+
ctx->err.has_error = 1;
724+
ctx->err.should_free = 1;
696725
zend_type_error("Expected array");
697726
return;
698727
}
@@ -856,6 +885,8 @@ static void from_zval_write_control(const zval *arr,
856885

857886
entry = get_ancillary_reg_entry(level, type);
858887
if (entry == NULL) {
888+
ctx->err.has_error = 1;
889+
ctx->err.should_free = 1;
859890
zend_value_error("cmsghdr with level %d and type %d not supported", level, type);
860891
return;
861892
}
@@ -864,6 +895,8 @@ static void from_zval_write_control(const zval *arr,
864895
zval *data_elem;
865896
/* arr must be an array at this point */
866897
if ((data_elem = zend_hash_str_find(Z_ARRVAL_P(arr), "data", sizeof("data") - 1)) == NULL) {
898+
ctx->err.has_error = 1;
899+
ctx->err.should_free = 1;
867900
zend_value_error("cmsghdr should have a 'data' element here");
868901
return;
869902
}
@@ -910,6 +943,8 @@ static void from_zval_write_control_array(const zval *arr, char *msghdr_c, ser_c
910943

911944
if (Z_TYPE_P(arr) != IS_ARRAY) {
912945
/* TODO Improve error message */
946+
ctx->err.has_error = 1;
947+
ctx->err.should_free = 1;
913948
zend_type_error("Expected array");
914949
return;
915950
}
@@ -952,6 +987,8 @@ static void to_zval_read_cmsg_data(const char *cmsghdr_c, zval *zv, res_context
952987

953988
entry = get_ancillary_reg_entry(cmsg->cmsg_level, cmsg->cmsg_type);
954989
if (entry == NULL) {
990+
ctx->err.has_error = 1;
991+
ctx->err.should_free = 1;
955992
zend_value_error("cmsghdr with level %d and type %d not supported", cmsg->cmsg_level, cmsg->cmsg_type);
956993
return;
957994
}
@@ -1048,6 +1085,8 @@ static void from_zval_write_msghdr_buffer_size(const zval *elem, char *msghdr_c,
10481085
}
10491086

10501087
if (lval < 0 || (zend_ulong)lval > MAX_USER_BUFF_SIZE) {
1088+
ctx->err.has_error = 1;
1089+
ctx->err.should_free = 1;
10511090
zend_value_error("The buffer size must be between 1 and " ZEND_LONG_FMT "; given " ZEND_LONG_FMT,
10521091
(zend_long) MAX_USER_BUFF_SIZE, lval);
10531092
return;
@@ -1077,6 +1116,8 @@ static void from_zval_write_iov_array(const zval *arr, char *msghdr_c, ser_conte
10771116
struct msghdr *msg = (struct msghdr*)msghdr_c;
10781117

10791118
if (Z_TYPE_P(arr) != IS_ARRAY) {
1119+
ctx->err.has_error = 1;
1120+
ctx->err.should_free = 1;
10801121
/* TODO Improve error */
10811122
zend_type_error("Expected array");
10821123
return;
@@ -1102,6 +1143,8 @@ static void from_zval_write_controllen(const zval *elem, char *msghdr_c, ser_con
11021143
*/
11031144
from_zval_write_uint32(elem, (char*)&len, ctx);
11041145
if (!ctx->err.has_error && len == 0) {
1146+
ctx->err.has_error = 1;
1147+
ctx->err.should_free = 1;
11051148
zend_value_error("controllen cannot be 0");
11061149
return;
11071150
}
@@ -1175,6 +1218,8 @@ static void to_zval_read_iov(const char *msghdr_c, zval *zv, res_context *ctx)
11751218
uint32_t i;
11761219

11771220
if (iovlen > UINT_MAX) {
1221+
ctx->err.has_error = 1;
1222+
ctx->err.should_free = 1;
11781223
zend_value_error("Unexpectedly large value for iov_len: %lu", (unsigned long) iovlen);
11791224
}
11801225
array_init_size(zv, (uint32_t)iovlen);
@@ -1221,6 +1266,8 @@ static void from_zval_write_ifindex(const zval *zv, char *uinteger, ser_context
12211266

12221267
if (Z_TYPE_P(zv) == IS_LONG) {
12231268
if (Z_LVAL_P(zv) < 0 || (zend_ulong)Z_LVAL_P(zv) > UINT_MAX) { /* allow 0 (unspecified interface) */
1269+
ctx->err.has_error = 1;
1270+
ctx->err.should_free = 1;
12241271
zend_value_error("The interface index cannot be negative or larger than %u; given " ZEND_LONG_FMT,
12251272
UINT_MAX, Z_LVAL_P(zv));
12261273
} else {
@@ -1317,12 +1364,16 @@ size_t calculate_scm_rights_space(const zval *arr, ser_context *ctx)
13171364

13181365
if (Z_TYPE_P(arr) != IS_ARRAY) {
13191366
/* TODO Improve error */
1367+
ctx->err.has_error = 1;
1368+
ctx->err.should_free = 1;
13201369
zend_type_error("Expected array");
13211370
return (size_t)-1;
13221371
}
13231372

13241373
num_elems = zend_hash_num_elements(Z_ARRVAL_P(arr));
13251374
if (num_elems == 0) {
1375+
ctx->err.has_error = 1;
1376+
ctx->err.should_free = 1;
13261377
zend_value_error("Expected at least one element in this array");
13271378
return (size_t)-1;
13281379
}
@@ -1345,6 +1396,8 @@ static void from_zval_write_fd_array_aux(zval *elem, unsigned i, void **args, se
13451396

13461397
stream = (php_stream *)zend_fetch_resource2_ex(elem, NULL, php_file_le_stream(), php_file_le_pstream());
13471398
if (stream == NULL) {
1399+
ctx->err.has_error = 1;
1400+
ctx->err.should_free = 1;
13481401
zend_type_error("Resource is not a stream nor a scoket");
13491402
return;
13501403
}
@@ -1356,13 +1409,17 @@ static void from_zval_write_fd_array_aux(zval *elem, unsigned i, void **args, se
13561409
return;
13571410
}
13581411
} else {
1412+
ctx->err.has_error = 1;
1413+
ctx->err.should_free = 1;
13591414
zend_type_error("Expected a resource");
13601415
}
13611416
}
13621417
void from_zval_write_fd_array(const zval *arr, char *int_arr, ser_context *ctx)
13631418
{
13641419
if (Z_TYPE_P(arr) != IS_ARRAY) {
13651420
/* TODO Improve error */
1421+
ctx->err.has_error = 1;
1422+
ctx->err.should_free = 1;
13661423
zend_type_error("Expected array");
13671424
return;
13681425
}
@@ -1386,6 +1443,8 @@ void to_zval_read_fd_array(const char *data, zval *zv, res_context *ctx)
13861443
}
13871444

13881445
if (*cmsg_len < data_offset) {
1446+
ctx->err.has_error = 1;
1447+
ctx->err.should_free = 1;
13891448
zend_value_error("Length of cmsg is smaller than its data member offset (" ZEND_LONG_FMT
13901449
" vs " ZEND_LONG_FMT ")", (zend_long)*cmsg_len, (zend_long)data_offset);
13911450
return;

0 commit comments

Comments
 (0)