PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/sockets - conversions.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 439 640 68.6 %
Date: 2015-01-26 Functions: 56 67 83.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "sockaddr_conv.h"
       2             : #include "conversions.h"
       3             : #include "sendrecvmsg.h" /* for ancillary registry */
       4             : #ifdef PHP_WIN32
       5             : # include "windows_common.h"
       6             : #endif
       7             : 
       8             : #include <Zend/zend_llist.h>
       9             : #include <zend_smart_str.h>
      10             : 
      11             : #ifndef PHP_WIN32
      12             : # include <sys/types.h>
      13             : # include <sys/socket.h>
      14             : # include <arpa/inet.h>
      15             : # include <netinet/in.h>
      16             : # include <sys/un.h>
      17             : # include <sys/ioctl.h>
      18             : # include <net/if.h>
      19             : #else
      20             : # include <win32/php_stdint.h>
      21             : #endif
      22             : 
      23             : #include <limits.h>
      24             : #include <stdarg.h>
      25             : #include <stddef.h>
      26             : 
      27             : #ifdef PHP_WIN32
      28             : typedef unsigned short sa_family_t;
      29             : # define msghdr                 _WSAMSG
      30             : /*
      31             : struct _WSAMSG {
      32             :     LPSOCKADDR       name;                              //void *msg_name
      33             :     INT              namelen;                   //socklen_t msg_namelen
      34             :     LPWSABUF         lpBuffers;                 //struct iovec *msg_iov
      35             :     ULONG            dwBufferCount;             //size_t msg_iovlen
      36             :     WSABUF           Control;                   //void *msg_control, size_t msg_controllen
      37             :     DWORD            dwFlags;                   //int msg_flags
      38             : }
      39             : struct __WSABUF {
      40             :   u_long                        len;                            //size_t iov_len (2nd member)
      41             :   char FAR                      *buf;                           //void *iov_base (1st member)
      42             : }
      43             : struct _WSACMSGHDR {
      44             :   UINT        cmsg_len;                                 //socklen_t cmsg_len
      45             :   INT         cmsg_level;                               //int       cmsg_level
      46             :   INT         cmsg_type;                                //int       cmsg_type;
      47             :   followed by UCHAR cmsg_data[]
      48             : }
      49             : */
      50             : # define msg_name               name
      51             : # define msg_namelen    namelen
      52             : # define msg_iov                lpBuffers
      53             : # define msg_iovlen             dwBufferCount
      54             : # define msg_control    Control.buf
      55             : # define msg_controllen Control.len
      56             : # define msg_flags              dwFlags
      57             : # define iov_base               buf
      58             : # define iov_len                len
      59             : 
      60             : # define cmsghdr                _WSACMSGHDR
      61             : # ifdef CMSG_DATA
      62             : #  undef CMSG_DATA
      63             : # endif
      64             : # define CMSG_DATA              WSA_CMSG_DATA
      65             : #endif
      66             : 
      67             : #define MAX_USER_BUFF_SIZE ((size_t)(100*1024*1024))
      68             : #define DEFAULT_BUFF_SIZE 8192
      69             : 
      70             : struct _ser_context {
      71             :         HashTable               params; /* stores pointers; has to be first */
      72             :         struct err_s    err;
      73             :         zend_llist              keys,
      74             :         /* common part to res_context ends here */
      75             :                                         allocations;
      76             :         php_socket              *sock;
      77             : };
      78             : struct _res_context {
      79             :         HashTable               params; /* stores pointers; has to be first */
      80             :         struct err_s    err;
      81             :         zend_llist              keys;
      82             : };
      83             : 
      84             : typedef struct {
      85             :         /* zval info */
      86             :         const char *name;
      87             :         unsigned name_size;
      88             :         int required;
      89             : 
      90             :         /* structure info */
      91             :         size_t field_offset; /* 0 to pass full structure, e.g. when more than
      92             :                                                         one field is to be changed; in that case the
      93             :                                                         callbacks need to know the name of the fields */
      94             : 
      95             :         /* callbacks */
      96             :         from_zval_write_field *from_zval;
      97             :         to_zval_read_field *to_zval;
      98             : } field_descriptor;
      99             : 
     100             : #define KEY_FILL_SOCKADDR "fill_sockaddr"
     101             : #define KEY_RECVMSG_RET   "recvmsg_ret"
     102             : #define KEY_CMSG_LEN      "cmsg_len"
     103             : 
     104             : const struct key_value empty_key_value_list[] = {{0}};
     105             : 
     106             : /* PARAMETERS */
     107           7 : static int param_get_bool(void *ctx, const char *key, int def)
     108             : {
     109             :         int *elem;
     110          14 :         if ((elem = zend_hash_str_find_ptr(ctx, key, strlen(key))) != NULL) {
     111           4 :                 return *elem;
     112             :         } else {
     113           3 :                 return def;
     114             :         }
     115             : }
     116             : 
     117             : /* MEMORY */
     118          24 : static inline void *accounted_emalloc(size_t alloc_size, ser_context *ctx)
     119             : {
     120          24 :         void *ret = emalloc(alloc_size);
     121          24 :         zend_llist_add_element(&ctx->allocations, &ret);
     122          24 :         return ret;
     123             : }
     124           7 : static inline void *accounted_ecalloc(size_t nmemb, size_t alloc_size, ser_context *ctx)
     125             : {
     126           7 :         void *ret = ecalloc(nmemb, alloc_size);
     127           7 :         zend_llist_add_element(&ctx->allocations, &ret);
     128           7 :         return ret;
     129             : }
     130           6 : static inline void *accounted_safe_ecalloc(size_t nmemb, size_t alloc_size, size_t offset, ser_context *ctx)
     131             : {
     132           6 :         void *ret = safe_emalloc(nmemb, alloc_size, offset);
     133           6 :         memset(ret, '\0', nmemb * alloc_size + offset);
     134           6 :         zend_llist_add_element(&ctx->allocations, &ret);
     135           6 :         return ret;
     136             : }
     137             : 
     138             : /* ERRORS */
     139           1 : static void do_from_to_zval_err(struct err_s *err,
     140             :                                                                 zend_llist *keys,
     141             :                                                                 const char *what_conv,
     142             :                                                                 const char *fmt,
     143             :                                                                 va_list ap)
     144             : {
     145           1 :         smart_str                       path = {0};
     146             :         const char                      **node;
     147             :         char                            *user_msg;
     148             :         int                                     user_msg_size;
     149             :         zend_llist_position     pos;
     150             : 
     151           1 :         if (err->has_error) {
     152           0 :                 return;
     153             :         }
     154             : 
     155           3 :         for (node = zend_llist_get_first_ex(keys, &pos);
     156             :                         node != NULL;
     157           1 :                         node = zend_llist_get_next_ex(keys, &pos)) {
     158           1 :                 smart_str_appends(&path, *node);
     159             :                 smart_str_appends(&path, " > ");
     160             :         }
     161             : 
     162           1 :         if (path.s && path.s->len > 3) {
     163           1 :                 path.s->len -= 3;
     164             :         }
     165             :         smart_str_0(&path);
     166             : 
     167           1 :         user_msg_size = vspprintf(&user_msg, 0, fmt, ap);
     168             : 
     169           1 :         err->has_error = 1;
     170           1 :         err->level = E_WARNING;
     171           4 :         spprintf(&err->msg, 0, "error converting %s data (path: %s): %.*s",
     172             :                         what_conv,
     173           3 :                         path.s && *path.s->val != '\0' ? path.s->val : "unavailable",
     174             :                         user_msg_size, user_msg);
     175           1 :         err->should_free = 1;
     176             : 
     177           1 :         efree(user_msg);
     178             :         smart_str_free(&path);
     179             : }
     180             : ZEND_ATTRIBUTE_FORMAT(printf, 2 ,3)
     181           1 : static void do_from_zval_err(ser_context *ctx, const char *fmt, ...)
     182             : {
     183             :         va_list ap;
     184             : 
     185           1 :         va_start(ap, fmt);
     186           1 :         do_from_to_zval_err(&ctx->err, &ctx->keys, "user", fmt, ap);
     187           1 :         va_end(ap);
     188           1 : }
     189             : ZEND_ATTRIBUTE_FORMAT(printf, 2 ,3)
     190           0 : static void do_to_zval_err(res_context *ctx, const char *fmt, ...)
     191             : {
     192             :         va_list ap;
     193             : 
     194           0 :         va_start(ap, fmt);
     195           0 :         do_from_to_zval_err(&ctx->err, &ctx->keys, "native", fmt, ap);
     196           0 :         va_end(ap);
     197           0 : }
     198             : 
     199           1 : void err_msg_dispose(struct err_s *err)
     200             : {
     201           1 :         if (err->msg != NULL) {
     202           1 :                 php_error_docref0(NULL, err->level, "%s", err->msg);
     203           1 :                 if (err->should_free) {
     204           1 :                         efree(err->msg);
     205             :                 }
     206             :         }
     207           1 : }
     208           5 : void allocations_dispose(zend_llist **allocations)
     209             : {
     210           5 :         zend_llist_destroy(*allocations);
     211           5 :         efree(*allocations);
     212           5 :         *allocations = NULL;
     213           5 : }
     214             : 
     215           5 : static unsigned from_array_iterate(const zval *arr,
     216             :                                                                    void (*func)(zval *elem, unsigned i, void **args, ser_context *ctx),
     217             :                                                                    void **args,
     218             :                                                                    ser_context *ctx)
     219             : {
     220             :         HashPosition    pos;
     221             :         unsigned                i;
     222             :         zval                    *elem;
     223             :         char                    buf[sizeof("element #4294967295")];
     224           5 :         char                    *bufp = buf;
     225             : 
     226             :         /* Note i starts at 1, not 0! */
     227          47 :     for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos), i = 1;
     228          21 :                         !ctx->err.has_error
     229          21 :                         && (elem = zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), &pos)) != NULL;
     230          16 :                         zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos), i++) {
     231          16 :                 if (snprintf(buf, sizeof(buf), "element #%u", i) >= sizeof(buf)) {
     232           0 :                         memcpy(buf, "element", sizeof("element"));
     233             :                 }
     234          16 :                 zend_llist_add_element(&ctx->keys, &bufp);
     235             : 
     236          16 :                 func(elem, i, args, ctx);
     237             : 
     238          16 :                 zend_llist_remove_tail(&ctx->keys);
     239             :     }
     240             : 
     241           5 :     return i -1;
     242             : }
     243             : 
     244             : /* Generic Aggregated conversions */
     245          19 : static void from_zval_write_aggregation(const zval *container,
     246             :                                                                                 char *structure,
     247             :                                                                                 const field_descriptor *descriptors,
     248             :                                                                                 ser_context *ctx)
     249             : {
     250             :         const field_descriptor  *descr;
     251             :         zval                                    *elem;
     252             : 
     253          19 :         if (Z_TYPE_P(container) != IS_ARRAY) {
     254           0 :                 do_from_zval_err(ctx, "%s", "expected an array here");
     255             :         }
     256             : 
     257          60 :         for (descr = descriptors; descr->name != NULL && !ctx->err.has_error; descr++) {
     258          42 :                 if ((elem = zend_hash_str_find(Z_ARRVAL_P(container),
     259          42 :                                 descr->name, descr->name_size - 1)) != NULL) {
     260             : 
     261          33 :                         if (descr->from_zval == NULL) {
     262           0 :                                 do_from_zval_err(ctx, "No information on how to convert value "
     263             :                                                 "of key '%s'", descr->name);
     264           0 :                                 break;
     265             :                         }
     266             : 
     267          33 :                         zend_llist_add_element(&ctx->keys, (void*)&descr->name);
     268          33 :                         descr->from_zval(elem, ((char*)structure) + descr->field_offset, ctx);
     269          33 :                         zend_llist_remove_tail(&ctx->keys);
     270             : 
     271           9 :                 } else if (descr->required) {
     272           1 :                         do_from_zval_err(ctx, "The key '%s' is required", descr->name);
     273           1 :                         break;
     274             :                 }
     275             :         }
     276          19 : }
     277          14 : static void to_zval_read_aggregation(const char *structure,
     278             :                                                                          zval *zarr, /* initialized array */
     279             :                                                                          const field_descriptor *descriptors,
     280             :                                                                          res_context *ctx)
     281             : {
     282             :         const field_descriptor  *descr;
     283             : 
     284             :         assert(Z_TYPE_P(zarr) == IS_ARRAY);
     285             :         assert(Z_ARRVAL_P(zarr) != NULL);
     286             : 
     287          62 :         for (descr = descriptors; descr->name != NULL && !ctx->err.has_error; descr++) {
     288             :                 zval *new_zv, tmp;
     289             : 
     290          48 :                 if (descr->to_zval == NULL) {
     291           0 :                         do_to_zval_err(ctx, "No information on how to convert native "
     292             :                                         "field into value for key '%s'", descr->name);
     293           0 :                         break;
     294             :                 }
     295             : 
     296          48 :                 ZVAL_NULL(&tmp);
     297          96 :                 new_zv = zend_symtable_str_update(Z_ARRVAL_P(zarr), descr->name, descr->name_size - 1, &tmp);
     298             : 
     299          48 :                 zend_llist_add_element(&ctx->keys, (void*)&descr->name);
     300          48 :                 descr->to_zval(structure + descr->field_offset, new_zv, ctx);
     301          48 :                 zend_llist_remove_tail(&ctx->keys);
     302             :         }
     303          14 : }
     304             : 
     305             : /* CONVERSIONS for integers */
     306          16 : static zend_long from_zval_integer_common(const zval *arr_value, ser_context *ctx)
     307             : {
     308          16 :         zend_long ret = 0;
     309             :         zval lzval;
     310             : 
     311          16 :         ZVAL_NULL(&lzval);
     312          16 :         if (Z_TYPE_P(arr_value) != IS_LONG) {
     313           0 :                 ZVAL_COPY(&lzval, (zval *)arr_value);
     314           0 :                 arr_value = &lzval;
     315             :         }
     316             : 
     317          16 :         switch (Z_TYPE_P(arr_value)) {
     318             :         case IS_LONG:
     319             : long_case:
     320          16 :                 ret = Z_LVAL_P(arr_value);
     321          16 :                 break;
     322             : 
     323             :         /* if not long we're operating on lzval */
     324             :         case IS_DOUBLE:
     325             : double_case:
     326           0 :                 convert_to_long(&lzval);
     327           0 :                 goto long_case;
     328             : 
     329             :         case IS_OBJECT:
     330             :         case IS_STRING: {
     331             :                 zend_long lval;
     332             :                 double dval;
     333             : 
     334           0 :                 convert_to_string(&lzval);
     335             : 
     336           0 :                 switch (is_numeric_string(Z_STRVAL(lzval), Z_STRLEN(lzval), &lval, &dval, 0)) {
     337             :                 case IS_DOUBLE:
     338             :                         zval_dtor(&lzval);
     339           0 :                         ZVAL_DOUBLE(&lzval, dval);
     340           0 :                         goto double_case;
     341             : 
     342             :                 case IS_LONG:
     343             :                         zval_dtor(&lzval);
     344           0 :                         ZVAL_LONG(&lzval, lval);
     345           0 :                         goto long_case;
     346             :                 }
     347             : 
     348             :                 /* if we get here, we don't have a numeric string */
     349           0 :                 do_from_zval_err(ctx, "expected an integer, but got a non numeric "
     350           0 :                                 "string (possibly from a converted object): '%s'", Z_STRVAL_P(arr_value));
     351           0 :                 break;
     352             :         }
     353             : 
     354             :         default:
     355           0 :                 do_from_zval_err(ctx, "%s", "expected an integer, either of a PHP "
     356             :                                 "integer type or of a convertible type");
     357             :                 break;
     358             :         }
     359             : 
     360             :         zval_dtor(&lzval);
     361             : 
     362          16 :         return ret;
     363             : }
     364           7 : void from_zval_write_int(const zval *arr_value, char *field, ser_context *ctx)
     365             : {
     366             :         zend_long lval;
     367             :         int ival;
     368             : 
     369           7 :         lval = from_zval_integer_common(arr_value, ctx);
     370           7 :         if (ctx->err.has_error) {
     371           0 :                 return;
     372             :         }
     373             : 
     374           7 :         if (lval > INT_MAX || lval < INT_MIN) {
     375           0 :                 do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
     376             :                                 "for a native int");
     377           0 :                 return;
     378             :         }
     379             : 
     380           7 :         ival = (int)lval;
     381           7 :         memcpy(field, &ival, sizeof(ival));
     382             : }
     383           4 : static void from_zval_write_uint32(const zval *arr_value, char *field, ser_context *ctx)
     384             : {
     385             :         zend_long lval;
     386             :         uint32_t ival;
     387             : 
     388           4 :         lval = from_zval_integer_common(arr_value, ctx);
     389           4 :         if (ctx->err.has_error) {
     390           0 :                 return;
     391             :         }
     392             : 
     393           4 :         if (sizeof(zend_long) > sizeof(uint32_t) && (lval < 0 || lval > 0xFFFFFFFF)) {
     394           0 :                 do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
     395             :                                 "for an unsigned 32-bit integer");
     396           0 :                 return;
     397             :         }
     398             : 
     399           4 :         ival = (uint32_t)lval;
     400           4 :         memcpy(field, &ival, sizeof(ival));
     401             : }
     402           1 : static void from_zval_write_net_uint16(const zval *arr_value, char *field, ser_context *ctx)
     403             : {
     404             :         zend_long lval;
     405             :         uint16_t ival;
     406             : 
     407           1 :         lval = from_zval_integer_common(arr_value, ctx);
     408           1 :         if (ctx->err.has_error) {
     409           0 :                 return;
     410             :         }
     411             : 
     412           1 :         if (lval < 0 || lval > 0xFFFF) {
     413           0 :                 do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
     414             :                                 "for an unsigned 16-bit integer");
     415           0 :                 return;
     416             :         }
     417             : 
     418           1 :         ival = htons((uint16_t)lval);
     419           1 :         memcpy(field, &ival, sizeof(ival));
     420             : }
     421           0 : static void from_zval_write_sa_family(const zval *arr_value, char *field, ser_context *ctx)
     422             : {
     423             :         zend_long lval;
     424             :         sa_family_t ival;
     425             : 
     426           0 :         lval = from_zval_integer_common(arr_value, ctx);
     427           0 :         if (ctx->err.has_error) {
     428           0 :                 return;
     429             :         }
     430             : 
     431           0 :         if (lval < 0 || lval > (sa_family_t)-1) { /* sa_family_t is unsigned */
     432           0 :                 do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
     433             :                                 "for a sa_family_t value");
     434           0 :                 return;
     435             :         }
     436             : 
     437           0 :         ival = (sa_family_t)lval;
     438           0 :         memcpy(field, &ival, sizeof(ival));
     439             : }
     440           0 : static void from_zval_write_pid_t(const zval *arr_value, char *field, ser_context *ctx)
     441             : {
     442             :         zend_long lval;
     443             :         pid_t ival;
     444             : 
     445           0 :         lval = from_zval_integer_common(arr_value, ctx);
     446           0 :         if (ctx->err.has_error) {
     447           0 :                 return;
     448             :         }
     449             : 
     450           0 :         if (lval < 0 || (pid_t)lval != lval) { /* pid_t is signed */
     451           0 :                 do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
     452             :                                 "for a pid_t value");
     453           0 :                 return;
     454             :         }
     455             : 
     456           0 :         ival = (pid_t)lval;
     457           0 :         memcpy(field, &ival, sizeof(ival));
     458             : }
     459           0 : static void from_zval_write_uid_t(const zval *arr_value, char *field, ser_context *ctx)
     460             : {
     461             :         zend_long lval;
     462             :         uid_t ival;
     463             : 
     464           0 :         lval = from_zval_integer_common(arr_value, ctx);
     465           0 :         if (ctx->err.has_error) {
     466           0 :                 return;
     467             :         }
     468             : 
     469             :         /* uid_t can be signed or unsigned (generally unsigned) */
     470             :         if ((uid_t)-1 > (uid_t)0) {
     471           0 :                 if (sizeof(zend_long) > sizeof(uid_t) && (lval < 0 || (uid_t)lval != lval)) {
     472           0 :                         do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
     473             :                                         "for a uid_t value");
     474           0 :                         return;
     475             :                 }
     476             :         } else {
     477             :                 if (sizeof(zend_long) > sizeof(uid_t) && (uid_t)lval != lval) {
     478             :                         do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
     479             :                                         "for a uid_t value");
     480             :                         return;
     481             :                 }
     482             :         }
     483             : 
     484           0 :         ival = (uid_t)lval;
     485           0 :         memcpy(field, &ival, sizeof(ival));
     486             : }
     487             : 
     488          15 : void to_zval_read_int(const char *data, zval *zv, res_context *ctx)
     489             : {
     490             :         int ival;
     491          15 :         memcpy(&ival, data, sizeof(ival));
     492             : 
     493          15 :         ZVAL_LONG(zv, (zend_long)ival);
     494          15 : }
     495           2 : static void to_zval_read_unsigned(const char *data, zval *zv, res_context *ctx)
     496             : {
     497             :         unsigned ival;
     498           2 :         memcpy(&ival, data, sizeof(ival));
     499             : 
     500           2 :         ZVAL_LONG(zv, (zend_long)ival);
     501           2 : }
     502           2 : static void to_zval_read_net_uint16(const char *data, zval *zv, res_context *ctx)
     503             : {
     504             :         uint16_t ival;
     505           2 :         memcpy(&ival, data, sizeof(ival));
     506             : 
     507           2 :         ZVAL_LONG(zv, (zend_long)ntohs(ival));
     508           2 : }
     509           4 : static void to_zval_read_uint32(const char *data, zval *zv, res_context *ctx)
     510             : {
     511             :         uint32_t ival;
     512           4 :         memcpy(&ival, data, sizeof(ival));
     513             : 
     514           4 :         ZVAL_LONG(zv, (zend_long)ival);
     515           4 : }
     516           2 : static void to_zval_read_sa_family(const char *data, zval *zv, res_context *ctx)
     517             : {
     518             :         sa_family_t ival;
     519           2 :         memcpy(&ival, data, sizeof(ival));
     520             : 
     521           2 :         ZVAL_LONG(zv, (zend_long)ival);
     522           2 : }
     523           1 : static void to_zval_read_pid_t(const char *data, zval *zv, res_context *ctx)
     524             : {
     525             :         pid_t ival;
     526           1 :         memcpy(&ival, data, sizeof(ival));
     527             : 
     528           1 :         ZVAL_LONG(zv, (zend_long)ival);
     529           1 : }
     530           2 : static void to_zval_read_uid_t(const char *data, zval *zv, res_context *ctx)
     531             : {
     532             :         uid_t ival;
     533           2 :         memcpy(&ival, data, sizeof(ival));
     534             : 
     535           2 :         ZVAL_LONG(zv, (zend_long)ival);
     536           2 : }
     537             : 
     538             : /* CONVERSIONS for sockaddr */
     539           0 : static void from_zval_write_sin_addr(const zval *zaddr_str, char *inaddr, ser_context *ctx)
     540             : {
     541             :         int                                     res;
     542           0 :         struct sockaddr_in      saddr = {0};
     543             :         zend_string                     *addr_str;
     544             : 
     545           0 :         addr_str = zval_get_string((zval *) zaddr_str);
     546           0 :         res = php_set_inet_addr(&saddr, addr_str->val, ctx->sock);
     547           0 :         if (res) {
     548           0 :                 memcpy(inaddr, &saddr.sin_addr, sizeof saddr.sin_addr);
     549             :         } else {
     550             :                 /* error already emitted, but let's emit another more relevant */
     551           0 :                 do_from_zval_err(ctx, "could not resolve address '%s' to get an AF_INET "
     552             :                                 "address", addr_str->val);
     553             :         }
     554             : 
     555             :         zend_string_release(addr_str);
     556           0 : }
     557           0 : static void to_zval_read_sin_addr(const char *data, zval *zv, res_context *ctx)
     558             : {
     559           0 :         const struct in_addr *addr = (const struct in_addr *)data;
     560           0 :         socklen_t size = INET_ADDRSTRLEN;
     561           0 :         zend_string *str = zend_string_alloc(size - 1, 0);
     562           0 :         memset(str->val, '\0', size);
     563             : 
     564           0 :         ZVAL_NEW_STR(zv, str);
     565             : 
     566           0 :         if (inet_ntop(AF_INET, addr, Z_STRVAL_P(zv), size) == NULL) {
     567           0 :                 do_to_zval_err(ctx, "could not convert IPv4 address to string "
     568             :                                 "(errno %d)", errno);
     569           0 :                 return;
     570             :         }
     571             : 
     572           0 :         Z_STRLEN_P(zv) = strlen(Z_STRVAL_P(zv));
     573             : }
     574             : static const field_descriptor descriptors_sockaddr_in[] = {
     575             :                 {"family", sizeof("family"), 0, offsetof(struct sockaddr_in, sin_family), from_zval_write_sa_family, to_zval_read_sa_family},
     576             :                 {"addr", sizeof("addr"), 0, offsetof(struct sockaddr_in, sin_addr), from_zval_write_sin_addr, to_zval_read_sin_addr},
     577             :                 {"port", sizeof("port"), 0, offsetof(struct sockaddr_in, sin_port), from_zval_write_net_uint16, to_zval_read_net_uint16},
     578             :                 {0}
     579             : };
     580           0 : static void from_zval_write_sockaddr_in(const zval *container, char *sockaddr, ser_context *ctx)
     581             : {
     582           0 :         from_zval_write_aggregation(container, sockaddr, descriptors_sockaddr_in, ctx);
     583           0 : }
     584           0 : static void to_zval_read_sockaddr_in(const char *data, zval *zv, res_context *ctx)
     585             : {
     586           0 :         to_zval_read_aggregation(data, zv, descriptors_sockaddr_in, ctx);
     587           0 : }
     588             : #if HAVE_IPV6
     589           2 : static void from_zval_write_sin6_addr(const zval *zaddr_str, char *addr6, ser_context *ctx)
     590             : {
     591             :         int                                     res;
     592           2 :         struct sockaddr_in6     saddr6 = {0};
     593             :         zend_string                     *addr_str;
     594             : 
     595           2 :         addr_str = zval_get_string((zval *) zaddr_str);
     596           2 :         res = php_set_inet6_addr(&saddr6, addr_str->val, ctx->sock);
     597           2 :         if (res) {
     598           2 :                 memcpy(addr6, &saddr6.sin6_addr, sizeof saddr6.sin6_addr);
     599             :         } else {
     600             :                 /* error already emitted, but let's emit another more relevant */
     601           0 :                 do_from_zval_err(ctx, "could not resolve address '%s' to get an AF_INET6 "
     602           0 :                                 "address", Z_STRVAL_P(zaddr_str));
     603             :         }
     604             : 
     605             :         zend_string_release(addr_str);
     606           2 : }
     607           4 : static void to_zval_read_sin6_addr(const char *data, zval *zv, res_context *ctx)
     608             : {
     609           4 :         const struct in6_addr *addr = (const struct in6_addr *)data;
     610           4 :         socklen_t size = INET6_ADDRSTRLEN;
     611           8 :         zend_string *str = zend_string_alloc(size - 1, 0);
     612             : 
     613           4 :         memset(str->val, '\0', size);
     614             : 
     615           4 :         ZVAL_NEW_STR(zv, str);
     616             : 
     617           4 :         if (inet_ntop(AF_INET6, addr, Z_STRVAL_P(zv), size) == NULL) {
     618           0 :                 do_to_zval_err(ctx, "could not convert IPv6 address to string "
     619             :                                 "(errno %d)", errno);
     620           0 :                 return;
     621             :         }
     622             : 
     623           4 :         Z_STRLEN_P(zv) = strlen(Z_STRVAL_P(zv));
     624             : }
     625             : static const field_descriptor descriptors_sockaddr_in6[] = {
     626             :                 {"family", sizeof("family"), 0, offsetof(struct sockaddr_in6, sin6_family), from_zval_write_sa_family, to_zval_read_sa_family},
     627             :                 {"addr", sizeof("addr"), 0, offsetof(struct sockaddr_in6, sin6_addr), from_zval_write_sin6_addr, to_zval_read_sin6_addr},
     628             :                 {"port", sizeof("port"), 0, offsetof(struct sockaddr_in6, sin6_port), from_zval_write_net_uint16, to_zval_read_net_uint16},
     629             :                 {"flowinfo", sizeof("flowinfo"), 0, offsetof(struct sockaddr_in6, sin6_flowinfo), from_zval_write_uint32, to_zval_read_uint32},
     630             :                 {"scope_id", sizeof("scope_id"), 0, offsetof(struct sockaddr_in6, sin6_scope_id), from_zval_write_uint32, to_zval_read_uint32},
     631             :                 {0}
     632             : };
     633           1 : static void from_zval_write_sockaddr_in6(const zval *container, char *sockaddr6, ser_context *ctx)
     634             : {
     635           1 :         from_zval_write_aggregation(container, sockaddr6, descriptors_sockaddr_in6, ctx);
     636           1 : }
     637           2 : static void to_zval_read_sockaddr_in6(const char *data, zval *zv, res_context *ctx)
     638             : {
     639           2 :         to_zval_read_aggregation(data, zv, descriptors_sockaddr_in6, ctx);
     640           2 : }
     641             : #endif /* HAVE_IPV6 */
     642           2 : static void from_zval_write_sun_path(const zval *path, char *sockaddr_un_c, ser_context *ctx)
     643             : {
     644             :         zend_string                     *path_str;
     645           2 :         struct sockaddr_un      *saddr = (struct sockaddr_un*)sockaddr_un_c;
     646             : 
     647           2 :         path_str = zval_get_string((zval *) path);
     648             : 
     649             :         /* code in this file relies on the path being nul terminated, even though
     650             :          * this is not required, at least on linux for abstract paths. It also
     651             :          * assumes that the path is not empty */
     652           2 :         if (path_str->len == 0) {
     653           0 :                 do_from_zval_err(ctx, "%s", "the path is cannot be empty");
     654           0 :                 return;
     655             :         }
     656           2 :         if (path_str->len >= sizeof(saddr->sun_path)) {
     657           0 :                 do_from_zval_err(ctx, "the path is too long, the maximum permitted "
     658             :                                 "length is %ld", sizeof(saddr->sun_path) - 1);
     659           0 :                 return;
     660             :         }
     661             : 
     662           2 :         memcpy(&saddr->sun_path, path_str->val, path_str->len);
     663           2 :         saddr->sun_path[path_str->len] = '\0';
     664             : 
     665             :         zend_string_release(path_str);
     666             : }
     667           0 : static void to_zval_read_sun_path(const char *data, zval *zv, res_context *ctx) {
     668           0 :         struct sockaddr_un      *saddr = (struct sockaddr_un*)data;
     669             :         char *nul_pos;
     670             : 
     671           0 :         nul_pos = memchr(&saddr->sun_path, '\0', sizeof(saddr->sun_path));
     672           0 :         if (nul_pos == NULL) {
     673           0 :                 do_to_zval_err(ctx, "could not find a NUL in the path");
     674           0 :                 return;
     675             :         }
     676             : 
     677           0 :         ZVAL_STRINGL(zv, saddr->sun_path, nul_pos - (char*)&saddr->sun_path);
     678             : }
     679             : static const field_descriptor descriptors_sockaddr_un[] = {
     680             :                 {"family", sizeof("family"), 0, offsetof(struct sockaddr_un, sun_family), from_zval_write_sa_family, to_zval_read_sa_family},
     681             :                 {"path", sizeof("path"), 0, 0, from_zval_write_sun_path, to_zval_read_sun_path},
     682             :                 {0}
     683             : };
     684           2 : static void from_zval_write_sockaddr_un(const zval *container, char *sockaddr, ser_context *ctx)
     685             : {
     686           2 :         from_zval_write_aggregation(container, sockaddr, descriptors_sockaddr_un, ctx);
     687           2 : }
     688           0 : static void to_zval_read_sockaddr_un(const char *data, zval *zv, res_context *ctx)
     689             : {
     690           0 :         to_zval_read_aggregation(data, zv, descriptors_sockaddr_un, ctx);
     691           0 : }
     692           7 : static void from_zval_write_sockaddr_aux(const zval *container,
     693             :                                                                                  struct sockaddr **sockaddr_ptr,
     694             :                                                                                  socklen_t *sockaddr_len,
     695             :                                                                                  ser_context *ctx)
     696             : {
     697             :         int             family;
     698             :         zval    *elem;
     699             :         int             fill_sockaddr;
     700             : 
     701           7 :         if (Z_TYPE_P(container) != IS_ARRAY) {
     702           0 :                 do_from_zval_err(ctx, "%s", "expected an array here");
     703           0 :                 return;
     704             :         }
     705             : 
     706           7 :         fill_sockaddr = param_get_bool(ctx, KEY_FILL_SOCKADDR, 1);
     707             : 
     708          11 :         if ((elem = zend_hash_str_find(Z_ARRVAL_P(container), "family", sizeof("family") - 1)) != NULL
     709             :                         && Z_TYPE_P(elem) != IS_NULL) {
     710           2 :                 const char *node = "family";
     711           2 :                 zend_llist_add_element(&ctx->keys, &node);
     712           2 :                 from_zval_write_int(elem, (char*)&family, ctx);
     713           2 :                 zend_llist_remove_tail(&ctx->keys);
     714             :         } else {
     715           5 :                 family = ctx->sock->type;
     716             :         }
     717             : 
     718           7 :         switch (family) {
     719             :         case AF_INET:
     720             :                 /* though not all OSes support sockaddr_in used in IPv6 sockets */
     721           0 :                 if (ctx->sock->type != AF_INET && ctx->sock->type != AF_INET6) {
     722           0 :                         do_from_zval_err(ctx, "the specified family (number %d) is not "
     723             :                                         "supported on this socket", family);
     724           0 :                         return;
     725             :                 }
     726           0 :                 *sockaddr_ptr = accounted_ecalloc(1, sizeof(struct sockaddr_in), ctx);
     727           0 :                 *sockaddr_len = sizeof(struct sockaddr_in);
     728           0 :                 if (fill_sockaddr) {
     729           0 :                         from_zval_write_sockaddr_in(container, (char*)*sockaddr_ptr, ctx);
     730           0 :                         (*sockaddr_ptr)->sa_family = AF_INET;
     731             :                 }
     732           0 :                 break;
     733             : 
     734             : #if HAVE_IPV6
     735             :         case AF_INET6:
     736           3 :                 if (ctx->sock->type != AF_INET6) {
     737           0 :                         do_from_zval_err(ctx, "the specified family (AF_INET6) is not "
     738             :                                         "supported on this socket");
     739           0 :                         return;
     740             :                 }
     741           3 :                 *sockaddr_ptr = accounted_ecalloc(1, sizeof(struct sockaddr_in6), ctx);
     742           3 :                 *sockaddr_len = sizeof(struct sockaddr_in6);
     743           3 :                 if (fill_sockaddr) {
     744           1 :                         from_zval_write_sockaddr_in6(container, (char*)*sockaddr_ptr, ctx);
     745           1 :                         (*sockaddr_ptr)->sa_family = AF_INET6;
     746             :                 }
     747           3 :                 break;
     748             : #endif /* HAVE_IPV6 */
     749             : 
     750             :         case AF_UNIX:
     751           4 :                 if (ctx->sock->type != AF_UNIX) {
     752           0 :                         do_from_zval_err(ctx, "the specified family (AF_UNIX) is not "
     753             :                                         "supported on this socket");
     754           0 :                         return;
     755             :                 }
     756           4 :                 *sockaddr_ptr = accounted_ecalloc(1, sizeof(struct sockaddr_un), ctx);
     757           4 :                 if (fill_sockaddr) {
     758           2 :                         struct sockaddr_un *sock_un = (struct sockaddr_un*)*sockaddr_ptr;
     759             : 
     760           2 :                         from_zval_write_sockaddr_un(container, (char*)*sockaddr_ptr, ctx);
     761           2 :                         (*sockaddr_ptr)->sa_family = AF_UNIX;
     762             : 
     763             :                         /* calculating length is more complicated here. Giving the size of
     764             :                          * struct sockaddr_un here and relying on the nul termination of
     765             :                          * sun_path does not work for paths in the abstract namespace. Note
     766             :                          * that we always assume the path is not empty and nul terminated */
     767           3 :                         *sockaddr_len = offsetof(struct sockaddr_un, sun_path) +
     768           2 :                                         (sock_un->sun_path[0] == '\0'
     769             :                                         ? (1 + strlen(&sock_un->sun_path[1]))
     770           1 :                                         : strlen(sock_un->sun_path));
     771             :                 } else {
     772           2 :                         *sockaddr_len = sizeof(struct sockaddr_un);
     773             :                 }
     774           4 :                 break;
     775             : 
     776             :         default:
     777           0 :                 do_from_zval_err(ctx, "%s", "the only families currently supported are "
     778             :                                 "AF_INET, AF_INET6 and AF_UNIX");
     779             :                 break;
     780             :         }
     781             : }
     782           4 : static void to_zval_read_sockaddr_aux(const char *sockaddr_c, zval *zv, res_context *ctx)
     783             : {
     784           4 :         const struct sockaddr *saddr = (struct sockaddr *)sockaddr_c;
     785             : 
     786           4 :         if (saddr->sa_family == 0) {
     787           2 :                 ZVAL_NULL(zv);
     788           2 :                 return;
     789             :         }
     790             : 
     791           2 :         array_init(zv);
     792             : 
     793           2 :         switch (saddr->sa_family) {
     794             :         case AF_INET:
     795           0 :                 to_zval_read_sockaddr_in(sockaddr_c, zv, ctx);
     796           0 :                 break;
     797             : 
     798             : #if HAVE_IPV6
     799             :         case AF_INET6:
     800           2 :                 to_zval_read_sockaddr_in6(sockaddr_c, zv, ctx);
     801           2 :                 break;
     802             : #endif /* HAVE_IPV6 */
     803             : 
     804             :         case AF_UNIX:
     805           0 :                 to_zval_read_sockaddr_un(sockaddr_c, zv, ctx);
     806           0 :                 break;
     807             : 
     808             :         default:
     809           0 :                 do_to_zval_err(ctx, "cannot read struct sockaddr with family %d; "
     810             :                                 "not supported",
     811             :                                 (int)saddr->sa_family);
     812             :                 break;
     813             :         }
     814             : }
     815             : 
     816             : /* CONVERSIONS for cmsghdr */
     817             : /*
     818             :  * [ level => , type => , data => [],]
     819             :  * struct cmsghdr {
     820             :  *  socklen_t cmsg_len;    // data byte count, including header
     821             :  *  int       cmsg_level;  // originating protocol
     822             :  *  int       cmsg_type;   // protocol-specific type
     823             :  *  // followed by unsigned char cmsg_data[];
     824             :  * };
     825             :  */
     826           2 : static void from_zval_write_control(const zval                  *arr,
     827             :                                                                         void                            **control_buf,
     828             :                                                                         zend_llist_element      *alloc,
     829             :                                                                         size_t                          *control_len,
     830             :                                                                         size_t                          *offset,
     831             :                                                                         ser_context                     *ctx)
     832             : {
     833             :         struct cmsghdr          *cmsghdr;
     834             :         int                                     level,
     835             :                                                 type;
     836             :         size_t                          data_len,
     837             :                                                 req_space,
     838             :                                                 space_left;
     839             :         ancillary_reg_entry     *entry;
     840             : 
     841             :         static const field_descriptor descriptor_level[] = {
     842             :                         {"level", sizeof("level"), 0, 0, from_zval_write_int, 0},
     843             :                         {0}
     844             :         };
     845             :         static const field_descriptor descriptor_type[] = {
     846             :                         {"type", sizeof("type"), 0, 0, from_zval_write_int, 0},
     847             :                         {0}
     848             :         };
     849             :         field_descriptor descriptor_data[] = {
     850             :                         {"data", sizeof("data"), 0, 0, 0, 0},
     851             :                         {0}
     852           2 :         };
     853             : 
     854           2 :         from_zval_write_aggregation(arr, (char *)&level, descriptor_level, ctx);
     855           2 :         if (ctx->err.has_error) {
     856           0 :                 return;
     857             :         }
     858           2 :         from_zval_write_aggregation(arr, (char *)&type, descriptor_type, ctx);
     859           2 :         if (ctx->err.has_error) {
     860           0 :                 return;
     861             :         }
     862             : 
     863           2 :         entry = get_ancillary_reg_entry(level, type);
     864           2 :         if (entry == NULL) {
     865           0 :                 do_from_zval_err(ctx, "cmsghdr with level %d and type %d not supported",
     866             :                                 level, type);
     867           0 :                 return;
     868             :         }
     869             : 
     870           2 :         if (entry->calc_space) {
     871             :                 zval *data_elem;
     872             :                 /* arr must be an array at this point */
     873           1 :                 if ((data_elem = zend_hash_str_find(Z_ARRVAL_P(arr), "data", sizeof("data") - 1)) == NULL) {
     874           0 :                         do_from_zval_err(ctx, "cmsghdr should have a 'data' element here");
     875           0 :                         return;
     876             :                 }
     877           1 :                 data_len = entry->calc_space(data_elem, ctx);
     878           1 :                 if (ctx->err.has_error) {
     879           0 :                         return;
     880             :                 }
     881             :         } else {
     882           1 :                 data_len = entry->size;
     883             :         }
     884           2 :         req_space = CMSG_SPACE(data_len);
     885           2 :         space_left = *control_len - *offset;
     886             :         assert(*control_len >= *offset);
     887             : 
     888           2 :         if (space_left < req_space) {
     889           0 :                 *control_buf = safe_erealloc(*control_buf, 2, req_space, *control_len);
     890           0 :                 *control_len += 2 * req_space;
     891           0 :                 memset(*control_buf, '\0', *control_len - *offset);
     892           0 :                 memcpy(&alloc->data, *control_buf, sizeof *control_buf);
     893             :         }
     894             : 
     895           2 :         cmsghdr = (struct cmsghdr*)(((char*)*control_buf) + *offset);
     896           2 :         cmsghdr->cmsg_level  = level;
     897           2 :         cmsghdr->cmsg_type   = type;
     898           2 :         cmsghdr->cmsg_len    = CMSG_LEN(data_len);
     899             : 
     900           2 :         descriptor_data[0].from_zval = entry->from_array;
     901           2 :         from_zval_write_aggregation(arr, (char*)CMSG_DATA(cmsghdr), descriptor_data, ctx);
     902             : 
     903           2 :         *offset += req_space;
     904             : }
     905           2 : static void from_zval_write_control_array(const zval *arr, char *msghdr_c, ser_context *ctx)
     906             : {
     907             :         char                            buf[sizeof("element #4294967295")];
     908           2 :         char                            *bufp = buf;
     909             :         zval                            *elem;
     910           2 :         uint32_t                        i = 0;
     911             :         int                                     num_elems;
     912             :         void                            *control_buf;
     913             :         zend_llist_element      *alloc;
     914             :         size_t                          control_len,
     915             :                                                 cur_offset;
     916           2 :         struct msghdr           *msg = (struct msghdr*)msghdr_c;
     917             : 
     918           2 :         if (Z_TYPE_P(arr) != IS_ARRAY) {
     919           0 :                 do_from_zval_err(ctx, "%s", "expected an array here");
     920           0 :                 return;
     921             :         }
     922             : 
     923           2 :         num_elems = zend_hash_num_elements(Z_ARRVAL_P(arr));
     924           2 :         if (num_elems == 0) {
     925           0 :                 return;
     926             :         }
     927             : 
     928             :         /* estimate each message at 20 bytes */
     929           2 :         control_buf     = accounted_safe_ecalloc(num_elems, CMSG_SPACE(20), 0, ctx);
     930           2 :         alloc           = ctx->allocations.tail;
     931           2 :         control_len = (size_t)num_elems * CMSG_SPACE(20);
     932           2 :         cur_offset      = 0;
     933             : 
     934           6 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arr), elem) {
     935           2 :                 if (ctx->err.has_error) {
     936           0 :                         break;
     937             :                 }
     938             : 
     939           2 :                 if (snprintf(buf, sizeof(buf), "element #%u", (unsigned)i++) >= sizeof(buf)) {
     940           0 :                         memcpy(buf, "element", sizeof("element"));
     941             :                 }
     942           2 :                 zend_llist_add_element(&ctx->keys, &bufp);
     943             : 
     944           2 :                 from_zval_write_control(elem, &control_buf, alloc, &control_len, &cur_offset, ctx);
     945             : 
     946           2 :                 zend_llist_remove_tail(&ctx->keys);
     947             :         } ZEND_HASH_FOREACH_END();
     948             : 
     949           2 :     msg->msg_control = control_buf;
     950           2 :     msg->msg_controllen = cur_offset; /* not control_len, which may be larger */
     951             : }
     952           5 : static void to_zval_read_cmsg_data(const char *cmsghdr_c, zval *zv, res_context *ctx)
     953             : {
     954           5 :         const struct cmsghdr    *cmsg = (const struct cmsghdr *)cmsghdr_c;
     955             :         ancillary_reg_entry             *entry;
     956             :         size_t                                  len,
     957           5 :                                                         *len_p = &len;
     958             : 
     959           5 :         entry = get_ancillary_reg_entry(cmsg->cmsg_level, cmsg->cmsg_type);
     960           5 :         if (entry == NULL) {
     961           0 :                 do_to_zval_err(ctx, "cmsghdr with level %d and type %d not supported",
     962             :                                 cmsg->cmsg_level, cmsg->cmsg_type);
     963           0 :                 return;
     964             :         }
     965           5 :         if (CMSG_LEN(entry->size) > cmsg->cmsg_len) {
     966           0 :                 do_to_zval_err(ctx, "the cmsghdr structure is unexpectedly small; "
     967             :                                 "expected a length of at least %pd, but got %pd",
     968           0 :                                 (zend_long)CMSG_LEN(entry->size), (zend_long)cmsg->cmsg_len);
     969           0 :                 return;
     970             :         }
     971             : 
     972           5 :         len = (size_t)cmsg->cmsg_len; /* use another var because type of cmsg_len varies */
     973             : 
     974          10 :         if (zend_hash_str_add_ptr(&ctx->params, KEY_CMSG_LEN, sizeof(KEY_CMSG_LEN) - 1, len_p) == NULL) {
     975           0 :                 do_to_zval_err(ctx, "%s", "could not set parameter " KEY_CMSG_LEN);
     976           0 :                 return;
     977             :         }
     978             : 
     979           5 :         entry->to_array((const char *)CMSG_DATA(cmsg), zv, ctx);
     980             : 
     981           5 :         zend_hash_str_del(&ctx->params, KEY_CMSG_LEN, sizeof(KEY_CMSG_LEN) - 1);
     982             : }
     983           5 : static void to_zval_read_control(const char *cmsghdr_c, zval *zv, res_context *ctx)
     984             : {
     985             :         /* takes a cmsghdr, not a msghdr like from_zval_write_control */
     986             :         static const field_descriptor descriptors[] = {
     987             :                         {"level", sizeof("level"), 0, offsetof(struct cmsghdr, cmsg_level), 0, to_zval_read_int},
     988             :                         {"type", sizeof("type"), 0, offsetof(struct cmsghdr, cmsg_type), 0, to_zval_read_int},
     989             :                         {"data", sizeof("data"), 0, 0 /* cmsghdr passed */, 0, to_zval_read_cmsg_data},
     990             :                         {0}
     991             :         };
     992             : 
     993           5 :         array_init_size(zv, 3);
     994           5 :         to_zval_read_aggregation(cmsghdr_c, zv, descriptors, ctx);
     995           5 : }
     996           4 : static void to_zval_read_control_array(const char *msghdr_c, zval *zv, res_context *ctx)
     997             : {
     998           4 :         struct msghdr   *msg = (struct msghdr *)msghdr_c;
     999             :         struct cmsghdr  *cmsg;
    1000             :         char                    buf[sizeof("element #4294967295")];
    1001           4 :         char                    *bufp = buf;
    1002           4 :         uint32_t                i = 1;
    1003             : 
    1004             :         /*if (msg->msg_flags & MSG_CTRUNC) {
    1005             :                 php_error_docref0(NULL, E_WARNING, "The MSG_CTRUNC flag is present; will not "
    1006             :                                 "attempt to read control messages");
    1007             :                 ZVAL_FALSE(zv);
    1008             :                 return;
    1009             :         }*/
    1010             : 
    1011           4 :         array_init(zv);
    1012             : 
    1013          18 :         for (cmsg = CMSG_FIRSTHDR(msg);
    1014           5 :                         cmsg != NULL && !ctx->err.has_error;
    1015           5 :                         cmsg = CMSG_NXTHDR(msg, cmsg)) {
    1016             :                 zval *elem, tmp;
    1017             : 
    1018           5 :                 ZVAL_NULL(&tmp);
    1019           5 :                 elem = zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
    1020             : 
    1021           5 :                 if (snprintf(buf, sizeof(buf), "element #%u", (unsigned)i++) >= sizeof(buf)) {
    1022           0 :                         memcpy(buf, "element", sizeof("element"));
    1023             :                 }
    1024           5 :                 zend_llist_add_element(&ctx->keys, &bufp);
    1025             : 
    1026           5 :                 to_zval_read_control((const char *)cmsg, elem, ctx);
    1027             : 
    1028           5 :                 zend_llist_remove_tail(&ctx->keys);
    1029             :         }
    1030           4 : }
    1031             : 
    1032             : /* CONVERSIONS for msghdr */
    1033           7 : static void from_zval_write_name(const zval *zname_arr, char *msghdr_c, ser_context *ctx)
    1034             : {
    1035             :         struct sockaddr *sockaddr;
    1036             :         socklen_t               sockaddr_len;
    1037           7 :         struct msghdr   *msghdr = (struct msghdr *)msghdr_c;
    1038             : 
    1039           7 :         from_zval_write_sockaddr_aux(zname_arr, &sockaddr, &sockaddr_len, ctx);
    1040             : 
    1041           7 :         msghdr->msg_name = sockaddr;
    1042           7 :         msghdr->msg_namelen = sockaddr_len;
    1043           7 : }
    1044           4 : static void to_zval_read_name(const char *sockaddr_p, zval *zv, res_context *ctx)
    1045             : {
    1046           4 :         void *name = (void*)*(void**)sockaddr_p;
    1047           4 :         if (name == NULL) {
    1048           0 :                 ZVAL_NULL(zv);
    1049             :         } else {
    1050           4 :                 to_zval_read_sockaddr_aux(name, zv, ctx);
    1051             :         }
    1052           4 : }
    1053           4 : static void from_zval_write_msghdr_buffer_size(const zval *elem, char *msghdr_c, ser_context *ctx)
    1054             : {
    1055             :         zend_long lval;
    1056           4 :         struct msghdr *msghdr = (struct msghdr *)msghdr_c;
    1057             : 
    1058           4 :         lval = from_zval_integer_common(elem, ctx);
    1059           4 :         if (ctx->err.has_error) {
    1060           0 :                 return;
    1061             :         }
    1062             : 
    1063           4 :         if (lval < 0 || lval > MAX_USER_BUFF_SIZE) {
    1064           0 :                 do_from_zval_err(ctx, "the buffer size must be between 1 and %pd; "
    1065             :                                 "given %pd", (zend_long)MAX_USER_BUFF_SIZE, lval);
    1066           0 :                 return;
    1067             :         }
    1068             : 
    1069           4 :         msghdr->msg_iovlen = 1;
    1070           4 :         msghdr->msg_iov = accounted_emalloc(sizeof(*msghdr->msg_iov) * 1, ctx);
    1071           4 :         msghdr->msg_iov[0].iov_base = accounted_emalloc((size_t)lval, ctx);
    1072           4 :         msghdr->msg_iov[0].iov_len = (size_t)lval;
    1073             : }
    1074          12 : static void from_zval_write_iov_array_aux(zval *elem, unsigned i, void **args, ser_context *ctx)
    1075             : {
    1076          12 :         struct msghdr   *msg = args[0];
    1077             :         size_t                  len;
    1078             : 
    1079          12 :         if (Z_REFCOUNTED_P(elem)) {
    1080             :                 Z_ADDREF_P(elem);
    1081             :         }
    1082          12 :         convert_to_string_ex(elem);
    1083             : 
    1084          12 :         len = Z_STRLEN_P(elem);
    1085          12 :         msg->msg_iov[i - 1].iov_base = accounted_emalloc(len, ctx);
    1086          12 :         msg->msg_iov[i - 1].iov_len = len;
    1087          12 :         memcpy(msg->msg_iov[i - 1].iov_base, Z_STRVAL_P(elem), len);
    1088             : 
    1089          12 :         zval_ptr_dtor(elem);
    1090          12 : }
    1091           4 : static void from_zval_write_iov_array(const zval *arr, char *msghdr_c, ser_context *ctx)
    1092             : {
    1093             :         int                             num_elem;
    1094           4 :         struct msghdr   *msg = (struct msghdr*)msghdr_c;
    1095             : 
    1096           4 :         if (Z_TYPE_P(arr) != IS_ARRAY) {
    1097           0 :                 do_from_zval_err(ctx, "%s", "expected an array here");
    1098           0 :                 return;
    1099             :         }
    1100             : 
    1101           4 :         num_elem = zend_hash_num_elements(Z_ARRVAL_P(arr));
    1102           4 :         if (num_elem == 0) {
    1103           0 :                 return;
    1104             :         }
    1105             : 
    1106           4 :         msg->msg_iov = accounted_safe_ecalloc(num_elem, sizeof *msg->msg_iov, 0, ctx);
    1107           4 :         msg->msg_iovlen = (size_t)num_elem;
    1108             : 
    1109           4 :     from_array_iterate(arr, from_zval_write_iov_array_aux, (void**)&msg, ctx);
    1110             : }
    1111           4 : static void from_zval_write_controllen(const zval *elem, char *msghdr_c, ser_context *ctx)
    1112             : {
    1113           4 :         struct msghdr *msghdr = (struct msghdr *)msghdr_c;
    1114             :         uint32_t len;
    1115             : 
    1116             :         /* controllen should be an unsigned with at least 32-bit. Let's assume
    1117             :          * this least common denominator
    1118             :          */
    1119           4 :         from_zval_write_uint32(elem, (char*)&len, ctx);
    1120           4 :         if (!ctx->err.has_error && len == 0) {
    1121           0 :                 do_from_zval_err(ctx, "controllen cannot be 0");
    1122           0 :                 return;
    1123             :         }
    1124           4 :         msghdr->msg_control = accounted_emalloc(len, ctx);
    1125           4 :         msghdr->msg_controllen = len;
    1126             : }
    1127           4 : void from_zval_write_msghdr_send(const zval *container, char *msghdr_c, ser_context *ctx)
    1128             : {
    1129             :         static const field_descriptor descriptors[] = {
    1130             :                         {"name", sizeof("name"), 0, 0, from_zval_write_name, 0},
    1131             :                         {"iov", sizeof("iov"), 0, 0, from_zval_write_iov_array, 0},
    1132             :                         {"control", sizeof("control"), 0, 0, from_zval_write_control_array, 0},
    1133             :                         {0}
    1134             :         };
    1135             : 
    1136           4 :         from_zval_write_aggregation(container, msghdr_c, descriptors, ctx);
    1137           4 : }
    1138           4 : void from_zval_write_msghdr_recv(const zval *container, char *msghdr_c, ser_context *ctx)
    1139             : {
    1140             :         /* zval to struct msghdr, version for recvmsg(). It differs from the version
    1141             :          * for sendmsg() in that it:
    1142             :          *      - has a buffer_size instead of an iov array;
    1143             :          *      - has no control element; has a controllen element instead
    1144             :          * struct msghdr {
    1145             :          *    void *msg_name;
    1146             :          *    socklen_t msg_namelen;
    1147             :          *    struct iovec *msg_iov;
    1148             :          *    size_t msg_iovlen;
    1149             :          *    void *msg_control;
    1150             :          *    size_t msg_controllen; //can also be socklen_t
    1151             :          *    int msg_flags;
    1152             :          * };
    1153             :          */
    1154             :         static const field_descriptor descriptors[] = {
    1155             :                         {"name", sizeof("name"), 0, 0, from_zval_write_name, 0},
    1156             :                         {"buffer_size", sizeof("buffer_size"), 0, 0, from_zval_write_msghdr_buffer_size, 0},
    1157             :                         {"controllen", sizeof("controllen"), 1, 0, from_zval_write_controllen, 0},
    1158             :                         {0}
    1159             :         };
    1160           4 :         struct msghdr   *msghdr = (struct msghdr *)msghdr_c;
    1161           4 :         const int               falsev = 0,
    1162           4 :                                         *falsevp = &falsev;
    1163             : 
    1164           8 :         if (zend_hash_str_add_ptr(&ctx->params, KEY_FILL_SOCKADDR, sizeof(KEY_FILL_SOCKADDR) - 1, (void *)falsevp) == NULL) {
    1165           0 :                 do_from_zval_err(ctx, "could not add fill_sockaddr; this is a bug");
    1166           0 :                 return;
    1167             :         }
    1168             : 
    1169           4 :         from_zval_write_aggregation(container, msghdr_c, descriptors, ctx);
    1170             : 
    1171           4 :         zend_hash_str_del(&ctx->params, KEY_FILL_SOCKADDR, sizeof(KEY_FILL_SOCKADDR) - 1);
    1172           4 :         if (ctx->err.has_error) {
    1173           0 :                 return;
    1174             :         }
    1175             : 
    1176           4 :         if (msghdr->msg_iovlen == 0) {
    1177           0 :                 msghdr->msg_iovlen = 1;
    1178           0 :                 msghdr->msg_iov = accounted_emalloc(sizeof(*msghdr->msg_iov) * 1, ctx);
    1179           0 :                 msghdr->msg_iov[0].iov_base = accounted_emalloc((size_t)DEFAULT_BUFF_SIZE, ctx);
    1180           0 :                 msghdr->msg_iov[0].iov_len = (size_t)DEFAULT_BUFF_SIZE;
    1181             :         }
    1182             : }
    1183             : 
    1184           4 : static void to_zval_read_iov(const char *msghdr_c, zval *zv, res_context *ctx)
    1185             : {
    1186           4 :         const struct msghdr     *msghdr = (const struct msghdr *)msghdr_c;
    1187           4 :         size_t                          iovlen = msghdr->msg_iovlen;
    1188             :         ssize_t                         *recvmsg_ret,
    1189             :                                                 bytes_left;
    1190             :         uint                            i;
    1191             : 
    1192           4 :         if (iovlen > UINT_MAX) {
    1193           0 :                 do_to_zval_err(ctx, "unexpectedly large value for iov_len: %lu",
    1194             :                                 (unsigned long)iovlen);
    1195             :         }
    1196           4 :         array_init_size(zv, (uint)iovlen);
    1197             : 
    1198           8 :         if ((recvmsg_ret = zend_hash_str_find_ptr(&ctx->params, KEY_RECVMSG_RET, sizeof(KEY_RECVMSG_RET) - 1)) == NULL) {
    1199           0 :                 do_to_zval_err(ctx, "recvmsg_ret not found in params. This is a bug");
    1200           0 :                 return;
    1201             :         }
    1202           4 :         bytes_left = *recvmsg_ret;
    1203             : 
    1204           8 :         for (i = 0; bytes_left > 0 && i < (uint)iovlen; i++) {
    1205             :                 zval elem;
    1206           4 :                 size_t len = MIN(msghdr->msg_iov[i].iov_len, (size_t)bytes_left);
    1207           4 :                 zend_string     *buf = zend_string_alloc(len, 0);
    1208             : 
    1209           4 :                 memcpy(buf->val, msghdr->msg_iov[i].iov_base, buf->len);
    1210           4 :                 buf->val[buf->len] = '\0';
    1211             : 
    1212           4 :                 ZVAL_NEW_STR(&elem, buf);
    1213           4 :                 add_next_index_zval(zv, &elem);
    1214           4 :                 bytes_left -= len;
    1215             :         }
    1216             : }
    1217           4 : void to_zval_read_msghdr(const char *msghdr_c, zval *zv, res_context *ctx)
    1218             : {
    1219             :         static const field_descriptor descriptors[] = {
    1220             :                         {"name", sizeof("name"), 0, offsetof(struct msghdr, msg_name), 0, to_zval_read_name},
    1221             :                         {"control", sizeof("control"), 0, 0, 0, to_zval_read_control_array},
    1222             :                         {"iov", sizeof("iov"), 0, 0, 0, to_zval_read_iov},
    1223             :                         {"flags", sizeof("flags"), 0, offsetof(struct msghdr, msg_flags), 0, to_zval_read_int},
    1224             :                         {0}
    1225             :         };
    1226             : 
    1227           4 :         array_init_size(zv, 4);
    1228             : 
    1229           4 :         to_zval_read_aggregation(msghdr_c, zv, descriptors, ctx);
    1230           4 : }
    1231             : 
    1232             : /* CONVERSIONS for if_index */
    1233           1 : static void from_zval_write_ifindex(const zval *zv, char *uinteger, ser_context *ctx)
    1234             : {
    1235           1 :         unsigned ret = 0;
    1236             : 
    1237           1 :         if (Z_TYPE_P(zv) == IS_LONG) {
    1238           1 :                 if (Z_LVAL_P(zv) < 0 || Z_LVAL_P(zv) > UINT_MAX) { /* allow 0 (unspecified interface) */
    1239           0 :                         do_from_zval_err(ctx, "the interface index cannot be negative or "
    1240             :                                         "larger than %u; given %pd", UINT_MAX, Z_LVAL_P(zv));
    1241             :                 } else {
    1242           1 :                         ret = (unsigned)Z_LVAL_P(zv);
    1243             :                 }
    1244             :         } else {
    1245             :                 zend_string *str;
    1246             : 
    1247           0 :                 str = zval_get_string((zval *) zv);
    1248             : 
    1249             : #if HAVE_IF_NAMETOINDEX
    1250           0 :                 ret = if_nametoindex(str->val);
    1251           0 :                 if (ret == 0) {
    1252           0 :                         do_from_zval_err(ctx, "no interface with name \"%s\" could be found", str->val);
    1253             :                 }
    1254             : #elif defined(SIOCGIFINDEX)
    1255             :                 {
    1256             :                         struct ifreq ifr;
    1257             :                         if (strlcpy(ifr.ifr_name, str->val, sizeof(ifr.ifr_name))
    1258             :                                         >= sizeof(ifr.ifr_name)) {
    1259             :                                 do_from_zval_err(ctx, "the interface name \"%s\" is too large ", str->val);
    1260             :                         } else if (ioctl(ctx->sock->bsd_socket, SIOCGIFINDEX, &ifr) < 0) {
    1261             :                                 if (errno == ENODEV) {
    1262             :                                         do_from_zval_err(ctx, "no interface with name \"%s\" could be "
    1263             :                                                         "found", str->val);
    1264             :                                 } else {
    1265             :                                         do_from_zval_err(ctx, "error fetching interface index for "
    1266             :                                                         "interface with name \"%s\" (errno %d)",
    1267             :                                                         str->val, errno);
    1268             :                                 }
    1269             :                         } else {
    1270             :                                 ret = (unsigned)ifr.ifr_ifindex;
    1271             :                         }
    1272             :                 }
    1273             : #else
    1274             :                 do_from_zval_err(ctx,
    1275             :                                 "this platform does not support looking up an interface by "
    1276             :                                 "name, an integer interface index must be supplied instead");
    1277             : #endif
    1278             : 
    1279             :                 zend_string_release(str);
    1280             :         }
    1281             : 
    1282           1 :         if (!ctx->err.has_error) {
    1283           1 :                 memcpy(uinteger, &ret, sizeof(ret));
    1284             :         }
    1285           1 : }
    1286             : 
    1287             : /* CONVERSIONS for struct in6_pktinfo */
    1288             : #if defined(IPV6_PKTINFO) && HAVE_IPV6
    1289             : static const field_descriptor descriptors_in6_pktinfo[] = {
    1290             :                 {"addr", sizeof("addr"), 1, offsetof(struct in6_pktinfo, ipi6_addr), from_zval_write_sin6_addr, to_zval_read_sin6_addr},
    1291             :                 {"ifindex", sizeof("ifindex"), 1, offsetof(struct in6_pktinfo, ipi6_ifindex), from_zval_write_ifindex, to_zval_read_unsigned},
    1292             :                 {0}
    1293             : };
    1294           2 : void from_zval_write_in6_pktinfo(const zval *container, char *in6_pktinfo_c, ser_context *ctx)
    1295             : {
    1296           2 :         from_zval_write_aggregation(container, in6_pktinfo_c, descriptors_in6_pktinfo, ctx);
    1297           2 : }
    1298           2 : void to_zval_read_in6_pktinfo(const char *data, zval *zv, res_context *ctx)
    1299             : {
    1300           2 :         array_init_size(zv, 2);
    1301             : 
    1302           2 :         to_zval_read_aggregation(data, zv, descriptors_in6_pktinfo, ctx);
    1303           2 : }
    1304             : #endif
    1305             : 
    1306             : /* CONVERSIONS for struct ucred */
    1307             : #ifdef SO_PASSCRED
    1308             : static const field_descriptor descriptors_ucred[] = {
    1309             :                 {"pid", sizeof("pid"), 1, offsetof(struct ucred, pid), from_zval_write_pid_t, to_zval_read_pid_t},
    1310             :                 {"uid", sizeof("uid"), 1, offsetof(struct ucred, uid), from_zval_write_uid_t, to_zval_read_uid_t},
    1311             :                 /* assume the type gid_t is the same as uid_t: */
    1312             :                 {"gid", sizeof("gid"), 1, offsetof(struct ucred, gid), from_zval_write_uid_t, to_zval_read_uid_t},
    1313             :                 {0}
    1314             : };
    1315           0 : void from_zval_write_ucred(const zval *container, char *ucred_c, ser_context *ctx)
    1316             : {
    1317           0 :         from_zval_write_aggregation(container, ucred_c, descriptors_ucred, ctx);
    1318           0 : }
    1319           1 : void to_zval_read_ucred(const char *data, zval *zv, res_context *ctx)
    1320             : {
    1321           1 :         array_init_size(zv, 3);
    1322             : 
    1323           1 :         to_zval_read_aggregation(data, zv, descriptors_ucred, ctx);
    1324           1 : }
    1325             : #endif
    1326             : 
    1327             : /* CONVERSIONS for SCM_RIGHTS */
    1328             : #ifdef SCM_RIGHTS
    1329           1 : size_t calculate_scm_rights_space(const zval *arr, ser_context *ctx)
    1330             : {
    1331             :         int num_elems;
    1332             : 
    1333           1 :         if (Z_TYPE_P(arr) != IS_ARRAY) {
    1334           0 :                 do_from_zval_err(ctx, "%s", "expected an array here");
    1335           0 :                 return (size_t)-1;
    1336             :         }
    1337             : 
    1338           1 :         num_elems = zend_hash_num_elements(Z_ARRVAL_P(arr));
    1339           1 :         if (num_elems == 0) {
    1340           0 :                 do_from_zval_err(ctx, "%s", "expected at least one element in this array");
    1341           0 :                 return (size_t)-1;
    1342             :         }
    1343             : 
    1344           1 :         return zend_hash_num_elements(Z_ARRVAL_P(arr)) * sizeof(int);
    1345             : }
    1346           4 : static void from_zval_write_fd_array_aux(zval *elem, unsigned i, void **args, ser_context *ctx)
    1347             : {
    1348           4 :         int *iarr = args[0];
    1349             : 
    1350           4 :         if (Z_TYPE_P(elem) == IS_RESOURCE) {
    1351             :                 php_stream *stream;
    1352             :                 php_socket *sock;
    1353             : 
    1354           4 :                 ZEND_FETCH_RESOURCE_NO_RETURN(sock, php_socket *, elem, -1,
    1355             :                                 NULL, php_sockets_le_socket());
    1356           4 :                 if (sock) {
    1357           1 :                         iarr[i] = sock->bsd_socket;
    1358           1 :                         return;
    1359             :                 }
    1360             : 
    1361           3 :                 ZEND_FETCH_RESOURCE2_NO_RETURN(stream, php_stream *, elem, -1,
    1362             :                                 NULL, php_file_le_stream(), php_file_le_pstream());
    1363           3 :                 if (stream == NULL) {
    1364           0 :                         do_from_zval_err(ctx, "resource is not a stream or a socket");
    1365           0 :                         return;
    1366             :                 }
    1367             : 
    1368           3 :                 if (php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&iarr[i - 1],
    1369             :                                 REPORT_ERRORS) == FAILURE) {
    1370           0 :                         do_from_zval_err(ctx, "cast stream to file descriptor failed");
    1371           0 :                         return;
    1372             :                 }
    1373             :         } else {
    1374           0 :                 do_from_zval_err(ctx, "expected a resource variable");
    1375             :         }
    1376             : }
    1377           1 : void from_zval_write_fd_array(const zval *arr, char *int_arr, ser_context *ctx)
    1378             : {
    1379           1 :         if (Z_TYPE_P(arr) != IS_ARRAY) {
    1380           0 :                 do_from_zval_err(ctx, "%s", "expected an array here");
    1381           0 :                 return;
    1382             :         }
    1383             : 
    1384           1 :    from_array_iterate(arr, &from_zval_write_fd_array_aux, (void**)&int_arr, ctx);
    1385             : }
    1386           1 : void to_zval_read_fd_array(const char *data, zval *zv, res_context *ctx)
    1387             : {
    1388             :         size_t                  *cmsg_len;
    1389             :         int                             num_elems,
    1390             :                                         i;
    1391           1 :         struct cmsghdr  *dummy_cmsg = 0;
    1392             :         size_t                  data_offset;
    1393             : 
    1394           1 :         data_offset = (unsigned char *)CMSG_DATA(dummy_cmsg)
    1395             :                         - (unsigned char *)dummy_cmsg;
    1396             : 
    1397           2 :         if ((cmsg_len = zend_hash_str_find_ptr(&ctx->params, KEY_CMSG_LEN, sizeof(KEY_CMSG_LEN) - 1)) == NULL) {
    1398           0 :                 do_to_zval_err(ctx, "could not get value of parameter " KEY_CMSG_LEN);
    1399           0 :                 return;
    1400             :         }
    1401             : 
    1402           1 :         if (*cmsg_len < data_offset) {
    1403           0 :                 do_to_zval_err(ctx, "length of cmsg is smaller than its data member "
    1404           0 :                                 "offset (%pd vs %pd)", (zend_long)*cmsg_len, (zend_long)data_offset);
    1405           0 :                 return;
    1406             :         }
    1407           1 :         num_elems = (*cmsg_len - data_offset) / sizeof(int);
    1408             : 
    1409           1 :         array_init_size(zv, num_elems);
    1410             : 
    1411           5 :         for (i = 0; i < num_elems; i++) {
    1412             :                 zval            elem;
    1413             :                 int                     fd;
    1414             :                 struct stat     statbuf;
    1415             : 
    1416           4 :                 fd = *((int *)data + i);
    1417             : 
    1418             :                 /* determine whether we have a socket */
    1419           4 :                 if (fstat(fd, &statbuf) == -1) {
    1420           0 :                         do_to_zval_err(ctx, "error creating resource for received file "
    1421             :                                         "descriptor %d: fstat() call failed with errno %d", fd, errno);
    1422           0 :                         return;
    1423             :                 }
    1424           4 :                 if (S_ISSOCK(statbuf.st_mode)) {
    1425           0 :                         php_socket *sock = socket_import_file_descriptor(fd);
    1426           0 :                         zend_register_resource(&elem, sock, php_sockets_le_socket());
    1427             :                 } else {
    1428           4 :                         php_stream *stream = php_stream_fopen_from_fd(fd, "rw", NULL);
    1429           4 :                         php_stream_to_zval(stream, &elem);
    1430             :                 }
    1431             : 
    1432           4 :                 add_next_index_zval(zv, &elem);
    1433             :         }
    1434             : }
    1435             : #endif
    1436             : 
    1437             : /* ENTRY POINT for conversions */
    1438          47 : static void free_from_zval_allocation(void *alloc_ptr_ptr)
    1439             : {
    1440          47 :         efree(*(void**)alloc_ptr_ptr);
    1441          47 : }
    1442          10 : void *from_zval_run_conversions(const zval                      *container,
    1443             :                                                                 php_socket                      *sock,
    1444             :                                                                 from_zval_write_field   *writer,
    1445             :                                                                 size_t                          struct_size,
    1446             :                                                                 const char                      *top_name,
    1447             :                                                                 zend_llist                      **allocations /* out */,
    1448             :                                                                 struct err_s                    *err /* in/out */)
    1449             : {
    1450          10 :         ser_context ctx = {{0}};
    1451          10 :         char *structure = NULL;
    1452             : 
    1453          10 :         *allocations = NULL;
    1454             : 
    1455          10 :         if (err->has_error) {
    1456           0 :                 return NULL;
    1457             :         }
    1458             : 
    1459          10 :         zend_hash_init(&ctx.params, 8, NULL, NULL, 0);
    1460          10 :         zend_llist_init(&ctx.keys, sizeof(const char *), NULL, 0);
    1461          10 :         zend_llist_init(&ctx.allocations, sizeof(void *), &free_from_zval_allocation, 0);
    1462          10 :         ctx.sock = sock;
    1463             : 
    1464          10 :         structure = ecalloc(1, struct_size);
    1465             : 
    1466          10 :         zend_llist_add_element(&ctx.keys, &top_name);
    1467          10 :         zend_llist_add_element(&ctx.allocations, &structure);
    1468             : 
    1469             :         /* main call */
    1470          10 :         writer(container, structure, &ctx);
    1471             : 
    1472          10 :         if (ctx.err.has_error) {
    1473           1 :                 zend_llist_destroy(&ctx.allocations); /* deallocates structure as well */
    1474           1 :                 structure = NULL;
    1475           1 :                 *err = ctx.err;
    1476             :         } else {
    1477           9 :                 *allocations = emalloc(sizeof **allocations);
    1478           9 :                 **allocations = ctx.allocations;
    1479             :         }
    1480             : 
    1481          10 :         zend_llist_destroy(&ctx.keys);
    1482          10 :         zend_hash_destroy(&ctx.params);
    1483             : 
    1484          10 :         return structure;
    1485             : }
    1486           4 : zval *to_zval_run_conversions(const char *structure,
    1487             :                                                           to_zval_read_field *reader,
    1488             :                                                           const char *top_name,
    1489             :                                                           const struct key_value *key_value_pairs,
    1490             :                                                           struct err_s *err, zval *zv)
    1491             : {
    1492           4 :         res_context                             ctx = {{0}, {0}};
    1493             :         const struct key_value  *kv;
    1494             : 
    1495           4 :         if (err->has_error) {
    1496           0 :                 return NULL;
    1497             :         }
    1498             : 
    1499           4 :         zend_llist_init(&ctx.keys, sizeof(const char *), NULL, 0);
    1500           4 :         zend_llist_add_element(&ctx.keys, &top_name);
    1501             : 
    1502           4 :         zend_hash_init(&ctx.params, 8, NULL, NULL, 0);
    1503           8 :         for (kv = key_value_pairs; kv->key != NULL; kv++) {
    1504           4 :                 zend_hash_str_update_ptr(&ctx.params, kv->key, kv->key_size - 1, kv->value);
    1505             :         }
    1506             : 
    1507           4 :         ZVAL_NULL(zv);
    1508             :         /* main call */
    1509           4 :         reader(structure, zv, &ctx);
    1510             : 
    1511           4 :         if (ctx.err.has_error) {
    1512           0 :                 zval_ptr_dtor(zv);
    1513           0 :                 ZVAL_UNDEF(zv);
    1514           0 :                 *err = ctx.err;
    1515             :         }
    1516             : 
    1517           4 :         zend_llist_destroy(&ctx.keys);
    1518           4 :         zend_hash_destroy(&ctx.params);
    1519             : 
    1520           4 :         return Z_ISUNDEF_P(zv)? NULL : zv;
    1521             : }

Generated by: LCOV version 1.10

Generated at Mon, 26 Jan 2015 14:46:53 +0000 (45 hours ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.