Skip to content

Commit 9d7c7f4

Browse files
committed
Address review
1 parent 36df2fa commit 9d7c7f4

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

ext/sockets/conversions.c

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,15 @@ static void from_zval_write_aggregation(const zval *container,
240240
const field_descriptor *descr;
241241
zval *elem;
242242

243-
if (Z_TYPE_P(container) != IS_ARRAY) {
244-
zend_type_error("Expected array");
245-
return;
246-
}
243+
ZEND_ASSERT(descriptors->from_zval);
247244

248245
for (descr = descriptors; descr->name != NULL && !ctx->err.has_error; descr++) {
249246
if ((elem = zend_hash_str_find(Z_ARRVAL_P(container),
250247
descr->name, descr->name_size - 1)) != NULL) {
251248

252249
if (descr->from_zval == NULL) {
250+
ctx->err.has_error = 1;
251+
ctx->err.should_free = 1;
253252
zend_value_error("Impossible to convert value of key '%s'", descr->name);
254253
return;
255254
}
@@ -259,6 +258,8 @@ static void from_zval_write_aggregation(const zval *container,
259258
zend_llist_remove_tail(&ctx->keys);
260259

261260
} else if (descr->required) {
261+
ctx->err.has_error = 1;
262+
ctx->err.should_free = 1;
262263
zend_value_error("The key '%s' is required", descr->name);
263264
return;
264265
}
@@ -339,11 +340,15 @@ static zend_long from_zval_integer_common(const zval *arr_value, ser_context *ct
339340
}
340341

341342
/* if we get here, we don't have a numeric string */
343+
ctx->err.has_error = 1;
344+
ctx->err.should_free = 1;
342345
zend_type_error("Expected an integer, received a non numeric string: '%s'", Z_STRVAL_P(arr_value));
343346
return ret;
344347
}
345348

346349
default:
350+
ctx->err.has_error = 1;
351+
ctx->err.should_free = 1;
347352
zend_type_error("Expected an integer");
348353
return ret;
349354
}
@@ -363,6 +368,8 @@ void from_zval_write_int(const zval *arr_value, char *field, ser_context *ctx)
363368
}
364369

365370
if (lval > INT_MAX || lval < INT_MIN) {
371+
ctx->err.has_error = 1;
372+
ctx->err.should_free = 1;
366373
zend_value_error("Provided PHP integer is out of bounds for a native int");
367374
return;
368375
}
@@ -381,6 +388,8 @@ static void from_zval_write_uint32(const zval *arr_value, char *field, ser_conte
381388
}
382389

383390
if (sizeof(zend_long) > sizeof(uint32_t) && (lval < 0 || lval > 0xFFFFFFFF)) {
391+
ctx->err.has_error = 1;
392+
ctx->err.should_free = 1;
384393
zend_value_error("Provided PHP integer is out of bounds for an unsigned 32-bit integer");
385394
return;
386395
}
@@ -399,6 +408,8 @@ static void from_zval_write_net_uint16(const zval *arr_value, char *field, ser_c
399408
}
400409

401410
if (lval < 0 || lval > 0xFFFF) {
411+
ctx->err.has_error = 1;
412+
ctx->err.should_free = 1;
402413
zend_value_error("Provided PHP integer is out of bounds for an unsigned 16-bit integer");
403414
return;
404415
}
@@ -417,6 +428,8 @@ static void from_zval_write_sa_family(const zval *arr_value, char *field, ser_co
417428
}
418429

419430
if (lval < 0 || lval > (sa_family_t)-1) { /* sa_family_t is unsigned */
431+
ctx->err.has_error = 1;
432+
ctx->err.should_free = 1;
420433
zend_value_error("Provided PHP integer is out of bounds for a sa_family_t value");
421434
return;
422435
}
@@ -437,6 +450,8 @@ static void from_zval_write_pid_t(const zval *arr_value, char *field, ser_contex
437450
}
438451

439452
if (lval < 0 || (pid_t)lval != lval) { /* pid_t is signed */
453+
ctx->err.has_error = 1;
454+
ctx->err.should_free = 1;
440455
zend_value_error("Provided PHP integer is out of bounds for a pid_t value");
441456
return;
442457
}
@@ -457,11 +472,15 @@ static void from_zval_write_uid_t(const zval *arr_value, char *field, ser_contex
457472
/* uid_t can be signed or unsigned (generally unsigned) */
458473
if ((uid_t)-1 > (uid_t)0) {
459474
if (sizeof(zend_long) > sizeof(uid_t) && (lval < 0 || (uid_t)lval != lval)) {
475+
ctx->err.has_error = 1;
476+
ctx->err.should_free = 1;
460477
zend_value_error("Provided PHP integer is out of bounds for a uid_t value");
461478
return;
462479
}
463480
} else {
464481
if (sizeof(zend_long) > sizeof(uid_t) && (uid_t)lval != lval) {
482+
ctx->err.has_error = 1;
483+
ctx->err.should_free = 1;
465484
zend_value_error("Provided PHP integer is out of bounds for a uid_t value");
466485
return;
467486
}
@@ -554,6 +573,8 @@ static void to_zval_read_sin_addr(const char *data, zval *zv, res_context *ctx)
554573
ZVAL_NEW_STR(zv, str);
555574

556575
if (inet_ntop(AF_INET, addr, Z_STRVAL_P(zv), size) == NULL) {
576+
ctx->err.has_error = 1;
577+
ctx->err.should_free = 1;
557578
zend_value_error("Could not convert IPv4 address to string (errno %d)", errno);
558579
return;
559580
}
@@ -605,6 +626,8 @@ static void to_zval_read_sin6_addr(const char *data, zval *zv, res_context *ctx)
605626
ZVAL_NEW_STR(zv, str);
606627

607628
if (inet_ntop(AF_INET6, addr, Z_STRVAL_P(zv), size) == NULL) {
629+
ctx->err.has_error = 1;
630+
ctx->err.should_free = 1;
608631
zend_value_error("Could not convert IPv6 address to string (errno %d)", errno);
609632
return;
610633
}
@@ -639,11 +662,15 @@ static void from_zval_write_sun_path(const zval *path, char *sockaddr_un_c, ser_
639662
* this is not required, at least on linux for abstract paths. It also
640663
* assumes that the path is not empty */
641664
if (ZSTR_LEN(path_str) == 0) {
665+
ctx->err.has_error = 1;
666+
ctx->err.should_free = 1;
642667
zend_value_error("The path cannot be empty");
643668
zend_tmp_string_release(tmp_path_str);
644669
return;
645670
}
646671
if (ZSTR_LEN(path_str) >= sizeof(saddr->sun_path)) {
672+
ctx->err.has_error = 1;
673+
ctx->err.should_free = 1;
647674
zend_value_error("The path is too long, maximum permitted length is %zd", sizeof(saddr->sun_path) - 1);
648675
zend_tmp_string_release(tmp_path_str);
649676
return;
@@ -660,6 +687,8 @@ static void to_zval_read_sun_path(const char *data, zval *zv, res_context *ctx)
660687

661688
nul_pos = memchr(&saddr->sun_path, '\0', sizeof(saddr->sun_path));
662689
if (nul_pos == NULL) {
690+
ctx->err.has_error = 1;
691+
ctx->err.should_free = 1;
663692
zend_value_error("Could not find a NUL in the path");
664693
return;
665694
}
@@ -693,6 +722,8 @@ static void from_zval_write_sockaddr_aux(const zval *container,
693722

694723
if (Z_TYPE_P(container) != IS_ARRAY) {
695724
/* TODO improve error message? */
725+
ctx->err.has_error = 1;
726+
ctx->err.should_free = 1;
696727
zend_type_error("Expected array");
697728
return;
698729
}
@@ -856,6 +887,8 @@ static void from_zval_write_control(const zval *arr,
856887

857888
entry = get_ancillary_reg_entry(level, type);
858889
if (entry == NULL) {
890+
ctx->err.has_error = 1;
891+
ctx->err.should_free = 1;
859892
zend_value_error("cmsghdr with level %d and type %d not supported", level, type);
860893
return;
861894
}
@@ -864,6 +897,8 @@ static void from_zval_write_control(const zval *arr,
864897
zval *data_elem;
865898
/* arr must be an array at this point */
866899
if ((data_elem = zend_hash_str_find(Z_ARRVAL_P(arr), "data", sizeof("data") - 1)) == NULL) {
900+
ctx->err.has_error = 1;
901+
ctx->err.should_free = 1;
867902
zend_value_error("cmsghdr should have a 'data' element here");
868903
return;
869904
}
@@ -910,6 +945,8 @@ static void from_zval_write_control_array(const zval *arr, char *msghdr_c, ser_c
910945

911946
if (Z_TYPE_P(arr) != IS_ARRAY) {
912947
/* TODO Improve error message */
948+
ctx->err.has_error = 1;
949+
ctx->err.should_free = 1;
913950
zend_type_error("Expected array");
914951
return;
915952
}
@@ -952,6 +989,8 @@ static void to_zval_read_cmsg_data(const char *cmsghdr_c, zval *zv, res_context
952989

953990
entry = get_ancillary_reg_entry(cmsg->cmsg_level, cmsg->cmsg_type);
954991
if (entry == NULL) {
992+
ctx->err.has_error = 1;
993+
ctx->err.should_free = 1;
955994
zend_value_error("cmsghdr with level %d and type %d not supported", cmsg->cmsg_level, cmsg->cmsg_type);
956995
return;
957996
}
@@ -1048,6 +1087,8 @@ static void from_zval_write_msghdr_buffer_size(const zval *elem, char *msghdr_c,
10481087
}
10491088

10501089
if (lval < 0 || (zend_ulong)lval > MAX_USER_BUFF_SIZE) {
1090+
ctx->err.has_error = 1;
1091+
ctx->err.should_free = 1;
10511092
zend_value_error("The buffer size must be between 1 and " ZEND_LONG_FMT "; given " ZEND_LONG_FMT,
10521093
(zend_long) MAX_USER_BUFF_SIZE, lval);
10531094
return;
@@ -1077,6 +1118,8 @@ static void from_zval_write_iov_array(const zval *arr, char *msghdr_c, ser_conte
10771118
struct msghdr *msg = (struct msghdr*)msghdr_c;
10781119

10791120
if (Z_TYPE_P(arr) != IS_ARRAY) {
1121+
ctx->err.has_error = 1;
1122+
ctx->err.should_free = 1;
10801123
/* TODO Improve error */
10811124
zend_type_error("Expected array");
10821125
return;
@@ -1102,6 +1145,8 @@ static void from_zval_write_controllen(const zval *elem, char *msghdr_c, ser_con
11021145
*/
11031146
from_zval_write_uint32(elem, (char*)&len, ctx);
11041147
if (!ctx->err.has_error && len == 0) {
1148+
ctx->err.has_error = 1;
1149+
ctx->err.should_free = 1;
11051150
zend_value_error("controllen cannot be 0");
11061151
return;
11071152
}
@@ -1175,6 +1220,8 @@ static void to_zval_read_iov(const char *msghdr_c, zval *zv, res_context *ctx)
11751220
uint32_t i;
11761221

11771222
if (iovlen > UINT_MAX) {
1223+
ctx->err.has_error = 1;
1224+
ctx->err.should_free = 1;
11781225
zend_value_error("Unexpectedly large value for iov_len: %lu", (unsigned long) iovlen);
11791226
}
11801227
array_init_size(zv, (uint32_t)iovlen);
@@ -1221,6 +1268,8 @@ static void from_zval_write_ifindex(const zval *zv, char *uinteger, ser_context
12211268

12221269
if (Z_TYPE_P(zv) == IS_LONG) {
12231270
if (Z_LVAL_P(zv) < 0 || (zend_ulong)Z_LVAL_P(zv) > UINT_MAX) { /* allow 0 (unspecified interface) */
1271+
ctx->err.has_error = 1;
1272+
ctx->err.should_free = 1;
12241273
zend_value_error("The interface index cannot be negative or larger than %u; given " ZEND_LONG_FMT,
12251274
UINT_MAX, Z_LVAL_P(zv));
12261275
} else {
@@ -1317,12 +1366,16 @@ size_t calculate_scm_rights_space(const zval *arr, ser_context *ctx)
13171366

13181367
if (Z_TYPE_P(arr) != IS_ARRAY) {
13191368
/* TODO Improve error */
1369+
ctx->err.has_error = 1;
1370+
ctx->err.should_free = 1;
13201371
zend_type_error("Expected array");
13211372
return (size_t)-1;
13221373
}
13231374

13241375
num_elems = zend_hash_num_elements(Z_ARRVAL_P(arr));
13251376
if (num_elems == 0) {
1377+
ctx->err.has_error = 1;
1378+
ctx->err.should_free = 1;
13261379
zend_value_error("Expected at least one element in this array");
13271380
return (size_t)-1;
13281381
}
@@ -1345,6 +1398,8 @@ static void from_zval_write_fd_array_aux(zval *elem, unsigned i, void **args, se
13451398

13461399
stream = (php_stream *)zend_fetch_resource2_ex(elem, NULL, php_file_le_stream(), php_file_le_pstream());
13471400
if (stream == NULL) {
1401+
ctx->err.has_error = 1;
1402+
ctx->err.should_free = 1;
13481403
zend_type_error("Resource is not a stream nor a scoket");
13491404
return;
13501405
}
@@ -1356,13 +1411,17 @@ static void from_zval_write_fd_array_aux(zval *elem, unsigned i, void **args, se
13561411
return;
13571412
}
13581413
} else {
1414+
ctx->err.has_error = 1;
1415+
ctx->err.should_free = 1;
13591416
zend_type_error("Expected a resource");
13601417
}
13611418
}
13621419
void from_zval_write_fd_array(const zval *arr, char *int_arr, ser_context *ctx)
13631420
{
13641421
if (Z_TYPE_P(arr) != IS_ARRAY) {
13651422
/* TODO Improve error */
1423+
ctx->err.has_error = 1;
1424+
ctx->err.should_free = 1;
13661425
zend_type_error("Expected array");
13671426
return;
13681427
}
@@ -1386,6 +1445,8 @@ void to_zval_read_fd_array(const char *data, zval *zv, res_context *ctx)
13861445
}
13871446

13881447
if (*cmsg_len < data_offset) {
1448+
ctx->err.has_error = 1;
1449+
ctx->err.should_free = 1;
13891450
zend_value_error("Length of cmsg is smaller than its data member offset (" ZEND_LONG_FMT
13901451
" vs " ZEND_LONG_FMT ")", (zend_long)*cmsg_len, (zend_long)data_offset);
13911452
return;

0 commit comments

Comments
 (0)