From: Dmitry Stogov Date: Wed, 13 Aug 2014 10:51:48 +0000 (+0400) Subject: cleanup (use zend_string* instead of char*) X-Git-Tag: POST_PHPNG_MERGE~14^2~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5a06ac88da693623fb471494a3759f04582f966e;p=php cleanup (use zend_string* instead of char*) --- diff --git a/ext/mysqlnd/mysqlnd_net.c b/ext/mysqlnd/mysqlnd_net.c index 76e49a5d9d..84757f88c1 100644 --- a/ext/mysqlnd/mysqlnd_net.c +++ b/ext/mysqlnd/mysqlnd_net.c @@ -160,7 +160,7 @@ MYSQLND_METHOD(mysqlnd_net, open_tcp_or_unix)(MYSQLND_NET * const net, const cha unsigned int streams_flags = STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT; char * hashed_details = NULL; int hashed_details_len = 0; - char * errstr = NULL; + zend_string *errstr = NULL; int errcode = 0; struct timeval tv; dtor_func_t origin_dtor; @@ -190,10 +190,10 @@ MYSQLND_METHOD(mysqlnd_net, open_tcp_or_unix)(MYSQLND_NET * const net, const cha mnd_sprintf_free(hashed_details); } errcode = CR_CONNECTION_ERROR; - SET_CLIENT_ERROR(*error_info, errcode? errcode:CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, errstr); + SET_CLIENT_ERROR(*error_info, errcode? errcode:CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, errstr->val); if (errstr) { /* no mnd_ since we don't allocate it */ - efree(errstr); + STR_RELEASE(errstr); } DBG_RETURN(NULL); } diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c index 8fdbf2845f..b26e973507 100644 --- a/ext/openssl/xp_ssl.c +++ b/ext/openssl/xp_ssl.c @@ -1947,7 +1947,6 @@ static inline int php_openssl_tcp_sockop_accept(php_stream *stream, php_openssl_ clisock = php_network_accept_incoming(sock->s.socket, xparam->want_textaddr ? &xparam->outputs.textaddr : NULL, - xparam->want_textaddr ? &xparam->outputs.textaddrlen : NULL, xparam->want_addr ? &xparam->outputs.addr : NULL, xparam->want_addr ? &xparam->outputs.addrlen : NULL, xparam->inputs.timeout, diff --git a/ext/standard/fsock.c b/ext/standard/fsock.c index e11bb84056..300df8c1c0 100644 --- a/ext/standard/fsock.c +++ b/ext/standard/fsock.c @@ -43,7 +43,7 @@ static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent) int err; char *hostname = NULL; long hostname_len; - char *errstr = NULL; + zend_string *errstr = NULL; RETVAL_FALSE; @@ -83,7 +83,7 @@ static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent) efree(hostname); } if (stream == NULL) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:%ld (%s)", host, port, errstr == NULL ? "Unknown error" : errstr); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:%ld (%s)", host, port, errstr == NULL ? "Unknown error" : errstr->val); } if (hashkey) { @@ -98,18 +98,16 @@ static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent) if (zerrstr && errstr) { /* no need to dup; we need to efree buf anyway */ zval_dtor(zerrstr); - // TODO: avoid reallocation ??? - ZVAL_STRING(zerrstr, errstr); - efree(errstr); + ZVAL_STR(zerrstr, errstr); } else if (!zerrstr && errstr) { - efree(errstr); + STR_RELEASE(errstr); } RETURN_FALSE; } if (errstr) { - efree(errstr); + STR_RELEASE(errstr); } php_stream_to_zval(stream, return_value); diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 1007b16123..73952dda31 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -130,7 +130,8 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char tmp_line[128]; size_t chunk_size = 0, file_size = 0; int eol_detect = 0; - char *transport_string, *errstr = NULL; + char *transport_string; + zend_string *errstr = NULL; int transport_len, have_header = 0, request_fulluri = 0, ignore_errors = 0; char *protocol_version = NULL; int protocol_version_len = 3; /* Default: "1.0" */ @@ -216,8 +217,8 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, } if (errstr) { - php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "%s", errstr); - efree(errstr); + php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "%s", errstr->val); + STR_RELEASE(errstr); errstr = NULL; } diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 9ba10edb13..652512ce07 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -97,7 +97,7 @@ PHP_FUNCTION(stream_socket_client) php_stream *stream = NULL; int err; long flags = PHP_STREAM_CLIENT_CONNECT; - char *errstr = NULL; + zend_string *errstr = NULL; php_stream_context *context = NULL; RETVAL_FALSE; @@ -140,7 +140,7 @@ PHP_FUNCTION(stream_socket_client) /* host might contain binary characters */ zend_string *quoted_host = php_addslashes(host, host_len, 0 TSRMLS_CC); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", quoted_host->val, errstr == NULL ? "Unknown error" : errstr); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", quoted_host->val, errstr == NULL ? "Unknown error" : errstr->val); STR_RELEASE(quoted_host); } @@ -154,19 +154,16 @@ PHP_FUNCTION(stream_socket_client) ZVAL_LONG(zerrno, err); } if (zerrstr && errstr) { - /* no need to dup; we need to efree buf anyway */ zval_dtor(zerrstr); - // TODO: avoid reallocation ??? - ZVAL_STRING(zerrstr, errstr); - efree(errstr); + ZVAL_STR(zerrstr, errstr); } else if (errstr) { - efree(errstr); + STR_RELEASE(errstr); } RETURN_FALSE; } if (errstr) { - efree(errstr); + STR_RELEASE(errstr); } php_stream_to_zval(stream, return_value); @@ -184,7 +181,7 @@ PHP_FUNCTION(stream_socket_server) php_stream *stream = NULL; int err = 0; long flags = STREAM_XPORT_BIND | STREAM_XPORT_LISTEN; - char *errstr = NULL; + zend_string *errstr = NULL; php_stream_context *context = NULL; RETVAL_FALSE; @@ -213,7 +210,7 @@ PHP_FUNCTION(stream_socket_server) NULL, NULL, context, &errstr, &err); if (stream == NULL) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", host, errstr == NULL ? "Unknown error" : errstr); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", host, errstr == NULL ? "Unknown error" : errstr->val); } if (stream == NULL) { @@ -222,19 +219,16 @@ PHP_FUNCTION(stream_socket_server) ZVAL_LONG(zerrno, err); } if (zerrstr && errstr) { - /* no need to dup; we need to efree buf anyway */ zval_dtor(zerrstr); - // TODO: avoid reallocation ??? - ZVAL_STRING(zerrstr, errstr); - efree(errstr); + ZVAL_STR(zerrstr, errstr); } else if (errstr) { - efree(errstr); + STR_RELEASE(errstr); } RETURN_FALSE; } if (errstr) { - efree(errstr); + STR_RELEASE(errstr); } php_stream_to_zval(stream, return_value); @@ -247,14 +241,12 @@ PHP_FUNCTION(stream_socket_accept) { double timeout = FG(default_socket_timeout); zval *zpeername = NULL; - char *peername = NULL; - int peername_len; + zend_string *peername = NULL; php_timeout_ull conv; struct timeval tv; php_stream *stream = NULL, *clistream = NULL; zval *zstream; - - char *errstr = NULL; + zend_string *errstr = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|dz/", &zstream, &timeout, &zpeername) == FAILURE) { RETURN_FALSE; @@ -278,24 +270,21 @@ PHP_FUNCTION(stream_socket_accept) if (0 == php_stream_xport_accept(stream, &clistream, zpeername ? &peername : NULL, - zpeername ? &peername_len : NULL, NULL, NULL, &tv, &errstr TSRMLS_CC) && clistream) { if (peername) { - // TODO: avoid reallocation ??? - ZVAL_STRINGL(zpeername, peername, peername_len); - efree(peername); + ZVAL_STR(zpeername, peername); } php_stream_to_zval(clistream, return_value); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "accept failed: %s", errstr ? errstr : "Unknown error"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "accept failed: %s", errstr ? errstr->val : "Unknown error"); RETVAL_FALSE; } if (errstr) { - efree(errstr); + STR_RELEASE(errstr); } } /* }}} */ @@ -307,8 +296,7 @@ PHP_FUNCTION(stream_socket_get_name) php_stream *stream; zval *zstream; zend_bool want_peer; - char *name = NULL; - int name_len; + zend_string *name = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb", &zstream, &want_peer) == FAILURE) { RETURN_FALSE; @@ -318,15 +306,12 @@ PHP_FUNCTION(stream_socket_get_name) if (0 != php_stream_xport_get_name(stream, want_peer, &name, - &name_len, NULL, NULL TSRMLS_CC)) { RETURN_FALSE; } - // TODO: avoid reallocation ??? - RETVAL_STRINGL(name, name_len); - efree(name); + RETVAL_STR(name); } /* }}} */ @@ -365,8 +350,7 @@ PHP_FUNCTION(stream_socket_recvfrom) { php_stream *stream; zval *zstream, *zremote = NULL; - char *remote_addr = NULL; - int remote_addr_len; + zend_string *remote_addr = NULL; long to_read = 0; zend_string *read_buf; long flags = 0; @@ -391,15 +375,12 @@ PHP_FUNCTION(stream_socket_recvfrom) read_buf = STR_ALLOC(to_read, 0); recvd = php_stream_xport_recvfrom(stream, read_buf->val, to_read, flags, NULL, NULL, - zremote ? &remote_addr : NULL, - zremote ? &remote_addr_len : NULL + zremote ? &remote_addr : NULL TSRMLS_CC); if (recvd >= 0) { if (zremote) { - // TODO: avoid reallocation ??? - ZVAL_STRINGL(zremote, remote_addr, remote_addr_len); - efree(remote_addr); + ZVAL_STR(zremote, remote_addr); } read_buf->val[recvd] = '\0'; read_buf->len = recvd; diff --git a/ext/standard/string.c b/ext/standard/string.c index 5f4f360ca6..0b7cef3da8 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -146,41 +146,42 @@ static zend_string *php_bin2hex(const unsigned char *old, const size_t oldlen) /* {{{ php_hex2bin */ -static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t *newlen) +static zend_string *php_hex2bin(const unsigned char *old, const size_t oldlen) { size_t target_length = oldlen >> 1; - register unsigned char *str = (unsigned char *)safe_emalloc(target_length, sizeof(char), 1); + zend_string *str = STR_ALLOC(target_length, 0); + unsigned char *ret = (unsigned char *)str->val; size_t i, j; + for (i = j = 0; i < target_length; i++) { - char c = old[j++]; + unsigned char c = old[j++]; + unsigned char d; + if (c >= '0' && c <= '9') { - str[i] = (c - '0') << 4; + d = (c - '0') << 4; } else if (c >= 'a' && c <= 'f') { - str[i] = (c - 'a' + 10) << 4; + d = (c - 'a' + 10) << 4; } else if (c >= 'A' && c <= 'F') { - str[i] = (c - 'A' + 10) << 4; + d = (c - 'A' + 10) << 4; } else { - efree(str); + STR_FREE(str); return NULL; } c = old[j++]; if (c >= '0' && c <= '9') { - str[i] |= c - '0'; + d |= c - '0'; } else if (c >= 'a' && c <= 'f') { - str[i] |= c - 'a' + 10; + d |= c - 'a' + 10; } else if (c >= 'A' && c <= 'F') { - str[i] |= c - 'A' + 10; + d |= c - 'A' + 10; } else { - efree(str); + STR_FREE(str); return NULL; } + ret[i] = d; } - str[target_length] = '\0'; - - if (newlen) - *newlen = target_length; - return (char *)str; + return str; } /* }}} */ @@ -256,29 +257,25 @@ PHP_FUNCTION(bin2hex) Converts the hex representation of data to binary */ PHP_FUNCTION(hex2bin) { - char *result, *data; - size_t newlen; - int datalen; + zend_string *result, *data; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &datalen) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &data) == FAILURE) { return; } - if (datalen % 2 != 0) { + if (data->len % 2 != 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Hexadecimal input string must have an even length"); RETURN_FALSE; } - result = php_hex2bin((unsigned char *)data, datalen, &newlen); + result = php_hex2bin((unsigned char *)data->val, data->len); if (!result) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input string must be hexadecimal string"); RETURN_FALSE; } - // TODO: avoid reallocation ??? - RETVAL_STRINGL(result, newlen); - efree(result); + RETVAL_STR(result); } /* }}} */ @@ -2457,7 +2454,7 @@ PHP_FUNCTION(substr_replace) orig_str = tmp_str; } - /* + /*??? refcount = Z_REFCOUNT_P(orig_str); */ @@ -2528,7 +2525,7 @@ PHP_FUNCTION(substr_replace) } else { repl_str = tmp_repl; } - /* + /*??? if (Z_REFCOUNT_P(orig_str) != refcount) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument was modified while replacing"); if (Z_TYPE_P(tmp_repl) != IS_STRING) { diff --git a/main/network.c b/main/network.c index d2bc35b4fb..74855064d7 100644 --- a/main/network.c +++ b/main/network.c @@ -166,7 +166,7 @@ PHPAPI void php_network_freeaddresses(struct sockaddr **sal) /* {{{ php_network_getaddresses * Returns number of addresses, 0 for none/error */ -PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, char **error_string TSRMLS_DC) +PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string TSRMLS_DC) { struct sockaddr **sap; int n; @@ -212,16 +212,16 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka if ((n = getaddrinfo(host, NULL, &hints, &res))) { if (error_string) { - spprintf(error_string, 0, "php_network_getaddresses: getaddrinfo failed: %s", PHP_GAI_STRERROR(n)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", *error_string); + *error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo failed: %s", PHP_GAI_STRERROR(n)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", (*error_string)->val); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "php_network_getaddresses: getaddrinfo failed: %s", PHP_GAI_STRERROR(n)); } return 0; } else if (res == NULL) { if (error_string) { - spprintf(error_string, 0, "php_network_getaddresses: getaddrinfo failed (null result pointer) errno=%d", errno); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", *error_string); + *error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo failed (null result pointer) errno=%d", errno); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", (*error_string)->val); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "php_network_getaddresses: getaddrinfo failed (null result pointer)"); } @@ -249,8 +249,8 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka host_info = gethostbyname(host); if (host_info == NULL) { if (error_string) { - spprintf(error_string, 0, "php_network_getaddresses: gethostbyname failed. errno=%d", errno); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", *error_string); + error_string = strpprintf(0, "php_network_getaddresses: gethostbyname failed. errno=%d", errno); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", (*error_string)->val); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "php_network_getaddresses: gethostbyname failed"); } @@ -305,7 +305,7 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd, socklen_t addrlen, int asynchronous, struct timeval *timeout, - char **error_string, + zend_string **error_string, int *error_code) { #if HAVE_NON_BLOCKING_CONNECT @@ -326,7 +326,7 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd, if (error != EINPROGRESS) { if (error_string) { - *error_string = php_socket_strerror(error, NULL, 0); + *error_string = php_socket_error_str(error); } return -1; @@ -382,7 +382,7 @@ ok: if (error) { ret = -1; if (error_string) { - *error_string = php_socket_strerror(error, NULL, 0); + *error_string = php_socket_error_str(error); } } return ret; @@ -416,7 +416,7 @@ static inline void sub_times(struct timeval a, struct timeval b, struct timeval * */ /* {{{ php_network_bind_socket_to_local_addr */ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port, - int socktype, long sockopts, char **error_string, int *error_code + int socktype, long sockopts, zend_string **error_string, int *error_code TSRMLS_DC) { int num_addrs, n, err = 0; @@ -495,7 +495,7 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po *error_code = err; } if (error_string) { - *error_string = php_socket_strerror(err, NULL, 0); + *error_string = php_socket_error_str(err); } bound: @@ -516,7 +516,7 @@ PHPAPI int php_network_parse_network_address_with_port(const char *addr, long ad struct sockaddr_in *in4 = (struct sockaddr_in*)sa; struct sockaddr **psal; int n; - char *errstr = NULL; + zend_string *errstr = NULL; #if HAVE_IPV6 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)sa; #endif @@ -562,8 +562,8 @@ PHPAPI int php_network_parse_network_address_with_port(const char *addr, long ad if (n == 0) { if (errstr) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to resolve `%s': %s", tmp, errstr); - efree(errstr); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to resolve `%s': %s", tmp, errstr->val); + STR_RELEASE(errstr); } goto out; } @@ -598,7 +598,7 @@ PHPAPI void php_network_populate_name_from_sockaddr( /* input address */ struct sockaddr *sa, socklen_t sl, /* output readable address */ - char **textaddr, long *textaddrlen, + zend_string **textaddr, /* output address */ struct sockaddr **addr, socklen_t *addrlen @@ -621,7 +621,7 @@ PHPAPI void php_network_populate_name_from_sockaddr( /* generally not thread safe, but it *is* thread safe under win32 */ buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr); if (buf) { - *textaddrlen = spprintf(textaddr, 0, "%s:%d", + *textaddr = strpprintf(0, "%s:%d", buf, ntohs(((struct sockaddr_in*)sa)->sin_port)); } @@ -631,7 +631,7 @@ PHPAPI void php_network_populate_name_from_sockaddr( case AF_INET6: buf = (char*)inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, (char *)&abuf, sizeof(abuf)); if (buf) { - *textaddrlen = spprintf(textaddr, 0, "%s:%d", + *textaddr = strpprintf(0, "%s:%d", buf, ntohs(((struct sockaddr_in6*)sa)->sin6_port)); } @@ -645,13 +645,10 @@ PHPAPI void php_network_populate_name_from_sockaddr( if (ua->sun_path[0] == '\0') { /* abstract name */ int len = strlen(ua->sun_path + 1) + 1; - *textaddrlen = len; - *textaddr = emalloc(len + 1); - memcpy(*textaddr, ua->sun_path, len); - (*textaddr)[len] = '\0'; + *textaddr = STR_INIT((char*)ua->sun_path, len, 0); } else { - *textaddrlen = strlen(ua->sun_path); - *textaddr = estrndup(ua->sun_path, *textaddrlen); + int len = strlen(ua->sun_path); + *textaddr = STR_INIT((char*)ua->sun_path, len, 0); } } break; @@ -663,7 +660,7 @@ PHPAPI void php_network_populate_name_from_sockaddr( } PHPAPI int php_network_get_peer_name(php_socket_t sock, - char **textaddr, long *textaddrlen, + zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen TSRMLS_DC) @@ -674,7 +671,7 @@ PHPAPI int php_network_get_peer_name(php_socket_t sock, if (getpeername(sock, (struct sockaddr*)&sa, &sl) == 0) { php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl, - textaddr, textaddrlen, + textaddr, addr, addrlen TSRMLS_CC); return 0; @@ -683,7 +680,7 @@ PHPAPI int php_network_get_peer_name(php_socket_t sock, } PHPAPI int php_network_get_sock_name(php_socket_t sock, - char **textaddr, long *textaddrlen, + zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen TSRMLS_DC) @@ -694,7 +691,7 @@ PHPAPI int php_network_get_sock_name(php_socket_t sock, if (getsockname(sock, (struct sockaddr*)&sa, &sl) == 0) { php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl, - textaddr, textaddrlen, + textaddr, addr, addrlen TSRMLS_CC); return 0; @@ -708,17 +705,17 @@ PHPAPI int php_network_get_sock_name(php_socket_t sock, * using an optional timeout. * Returns the peer address in addr/addrlen (it will emalloc * these, so be sure to efree the result). - * If you specify textaddr/textaddrlen, a text-printable + * If you specify textaddr, a text-printable * version of the address will be emalloc'd and returned. * */ /* {{{ php_network_accept_incoming */ PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock, - char **textaddr, long *textaddrlen, + zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen, struct timeval *timeout, - char **error_string, + zend_string **error_string, int *error_code TSRMLS_DC) { @@ -740,7 +737,7 @@ PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock, if (clisock != SOCK_ERR) { php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl, - textaddr, textaddrlen, + textaddr, addr, addrlen TSRMLS_CC); } else { @@ -752,7 +749,7 @@ PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock, *error_code = error; } if (error_string) { - *error_string = php_socket_strerror(error, NULL, 0); + *error_string = php_socket_error_str(error); } return clisock; @@ -769,7 +766,7 @@ PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock, /* {{{ php_network_connect_socket_to_host */ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port, - int socktype, int asynchronous, struct timeval *timeout, char **error_string, + int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string, int *error_code, char *bindto, unsigned short bindport, long sockopts TSRMLS_DC) { @@ -883,7 +880,7 @@ skip_bind: } /* free error string received during previous iteration (if any) */ if (error_string && *error_string) { - efree(*error_string); + STR_RELEASE(*error_string); *error_string = NULL; } @@ -1044,6 +1041,44 @@ PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize) } /* }}} */ +/* {{{ php_socket_error_str */ +PHPAPI zend_string *php_socket_error_str(long err) +{ +#ifndef PHP_WIN32 + char *errstr; + + errstr = strerror(err); + return STR_INIT(errstr, strlen(errstr), 0); +#else + zend_string *ret; + char *sysbuf; + int free_it = 1; + + if (!FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&sysbuf, + 0, + NULL)) { + free_it = 0; + sysbuf = "Unknown Error"; + } + + ret = STR_INIT(sysbuf, strlen(sysbuf), 0); + + if (free_it) { + LocalFree(sysbuf); + } + + return ret; +#endif +} +/* }}} */ + /* deprecated */ PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC TSRMLS_DC) { diff --git a/main/php_network.h b/main/php_network.h index 95299e63da..3a0062c437 100644 --- a/main/php_network.h +++ b/main/php_network.h @@ -63,6 +63,7 @@ * Also works sensibly for win32 */ BEGIN_EXTERN_C() PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize); +PHPAPI zend_string *php_socket_error_str(long err); END_EXTERN_C() #ifdef HAVE_NETINET_IN_H @@ -229,11 +230,11 @@ typedef struct { #endif BEGIN_EXTERN_C() -PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, char **error_string TSRMLS_DC); +PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string TSRMLS_DC); PHPAPI void php_network_freeaddresses(struct sockaddr **sal); PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port, - int socktype, int asynchronous, struct timeval *timeout, char **error_string, + int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string, int *error_code, char *bindto, unsigned short bindport, long sockopts TSRMLS_DC); @@ -242,33 +243,33 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd, socklen_t addrlen, int asynchronous, struct timeval *timeout, - char **error_string, + zend_string **error_string, int *error_code); #define php_connect_nonb(sock, addr, addrlen, timeout) \ php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL) PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port, - int socktype, long sockopts, char **error_string, int *error_code + int socktype, long sockopts, zend_string **error_string, int *error_code TSRMLS_DC); PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock, - char **textaddr, long *textaddrlen, + zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen, struct timeval *timeout, - char **error_string, + zend_string **error_string, int *error_code TSRMLS_DC); PHPAPI int php_network_get_sock_name(php_socket_t sock, - char **textaddr, long *textaddrlen, + zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen TSRMLS_DC); PHPAPI int php_network_get_peer_name(php_socket_t sock, - char **textaddr, long *textaddrlen, + zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen TSRMLS_DC); @@ -298,7 +299,7 @@ PHPAPI void php_network_populate_name_from_sockaddr( /* input address */ struct sockaddr *sa, socklen_t sl, /* output readable address */ - char **textaddr, long *textaddrlen, + zend_string **textaddr, /* output address */ struct sockaddr **addr, socklen_t *addrlen diff --git a/main/streams/php_stream_transport.h b/main/streams/php_stream_transport.h index dc10eb4e92..a6797ed898 100644 --- a/main/streams/php_stream_transport.h +++ b/main/streams/php_stream_transport.h @@ -50,7 +50,7 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, in int flags, const char *persistent_id, struct timeval *timeout, php_stream_context *context, - char **error_string, + zend_string **error_string, int *error_code STREAMS_DC TSRMLS_DC); @@ -60,7 +60,7 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, in /* Bind the stream to a local address */ PHPAPI int php_stream_xport_bind(php_stream *stream, const char *name, size_t namelen, - char **error_text + zend_string **error_text TSRMLS_DC); /* Connect to a remote address */ @@ -68,28 +68,28 @@ PHPAPI int php_stream_xport_connect(php_stream *stream, const char *name, size_t namelen, int asynchronous, struct timeval *timeout, - char **error_text, + zend_string **error_text, int *error_code TSRMLS_DC); /* Prepare to listen */ PHPAPI int php_stream_xport_listen(php_stream *stream, int backlog, - char **error_text + zend_string **error_text TSRMLS_DC); /* Get the next client and their address as a string, or the underlying address * structure. You must efree either of these if you request them */ PHPAPI int php_stream_xport_accept(php_stream *stream, php_stream **client, - char **textaddr, int *textaddrlen, + zend_string **textaddr, void **addr, socklen_t *addrlen, struct timeval *timeout, - char **error_text + zend_string **error_text TSRMLS_DC); /* Get the name of either the socket or it's peer */ PHPAPI int php_stream_xport_get_name(php_stream *stream, int want_peer, - char **textaddr, int *textaddrlen, + zend_string **textaddr, void **addr, socklen_t *addrlen TSRMLS_DC); @@ -102,7 +102,7 @@ enum php_stream_xport_send_recv_flags { * peeking, optionally retrieving OOB data */ PHPAPI int php_stream_xport_recvfrom(php_stream *stream, char *buf, size_t buflen, long flags, void **addr, socklen_t *addrlen, - char **textaddr, int *textaddrlen TSRMLS_DC); + zend_string **textaddr TSRMLS_DC); /* Similar to send() system call; send data to the stream, optionally * sending it as OOB data */ @@ -155,10 +155,8 @@ typedef struct _php_stream_xport_param { int returncode; struct sockaddr *addr; socklen_t addrlen; - char *textaddr; - long textaddrlen; - - char *error_text; + zend_string *textaddr; + zend_string *error_text; int error_code; } outputs; } php_stream_xport_param; diff --git a/main/streams/transports.c b/main/streams/transports.c index 0db04cd495..7792e24fef 100644 --- a/main/streams/transports.c +++ b/main/streams/transports.c @@ -40,20 +40,20 @@ PHPAPI int php_stream_xport_unregister(const char *protocol TSRMLS_DC) } #define ERR_REPORT(out_err, fmt, arg) \ - if (out_err) { spprintf(out_err, 0, fmt, arg); } \ + if (out_err) { *out_err = strpprintf(0, fmt, arg); } \ else { php_error_docref(NULL TSRMLS_CC, E_WARNING, fmt, arg); } #define ERR_RETURN(out_err, local_err, fmt) \ if (out_err) { *out_err = local_err; } \ - else { php_error_docref(NULL TSRMLS_CC, E_WARNING, fmt, local_err ? local_err : "Unspecified error"); \ - if (local_err) { efree(local_err); local_err = NULL; } \ + else { php_error_docref(NULL TSRMLS_CC, E_WARNING, fmt, local_err ? local_err->val : "Unspecified error"); \ + if (local_err) { STR_RELEASE(local_err); local_err = NULL; } \ } PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, int options, int flags, const char *persistent_id, struct timeval *timeout, php_stream_context *context, - char **error_string, + zend_string **error_string, int *error_code STREAMS_DC TSRMLS_DC) { @@ -61,7 +61,7 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, in php_stream_transport_factory factory = NULL; const char *p, *protocol = NULL; int n = 0, failed = 0; - char *error_text = NULL; + zend_string *error_text = NULL; struct timeval default_timeout = { 0, 0 }; default_timeout.tv_sec = FG(default_socket_timeout); @@ -195,7 +195,7 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, in /* Bind the stream to a local address */ PHPAPI int php_stream_xport_bind(php_stream *stream, const char *name, size_t namelen, - char **error_text + zend_string **error_text TSRMLS_DC) { php_stream_xport_param param; @@ -225,7 +225,7 @@ PHPAPI int php_stream_xport_connect(php_stream *stream, const char *name, size_t namelen, int asynchronous, struct timeval *timeout, - char **error_text, + zend_string **error_text, int *error_code TSRMLS_DC) { @@ -257,7 +257,7 @@ PHPAPI int php_stream_xport_connect(php_stream *stream, } /* Prepare to listen */ -PHPAPI int php_stream_xport_listen(php_stream *stream, int backlog, char **error_text TSRMLS_DC) +PHPAPI int php_stream_xport_listen(php_stream *stream, int backlog, zend_string **error_text TSRMLS_DC) { php_stream_xport_param param; int ret; @@ -282,10 +282,10 @@ PHPAPI int php_stream_xport_listen(php_stream *stream, int backlog, char **error /* Get the next client and their address (as a string) */ PHPAPI int php_stream_xport_accept(php_stream *stream, php_stream **client, - char **textaddr, int *textaddrlen, + zend_string **textaddr, void **addr, socklen_t *addrlen, struct timeval *timeout, - char **error_text + zend_string **error_text TSRMLS_DC) { php_stream_xport_param param; @@ -309,7 +309,6 @@ PHPAPI int php_stream_xport_accept(php_stream *stream, php_stream **client, } if (textaddr) { *textaddr = param.outputs.textaddr; - *textaddrlen = param.outputs.textaddrlen; } if (error_text) { *error_text = param.outputs.error_text; @@ -321,7 +320,7 @@ PHPAPI int php_stream_xport_accept(php_stream *stream, php_stream **client, } PHPAPI int php_stream_xport_get_name(php_stream *stream, int want_peer, - char **textaddr, int *textaddrlen, + zend_string **textaddr, void **addr, socklen_t *addrlen TSRMLS_DC) { @@ -343,7 +342,6 @@ PHPAPI int php_stream_xport_get_name(php_stream *stream, int want_peer, } if (textaddr) { *textaddr = param.outputs.textaddr; - *textaddrlen = param.outputs.textaddrlen; } return param.outputs.returncode; @@ -395,7 +393,7 @@ PHPAPI int php_stream_xport_crypto_enable(php_stream *stream, int activate TSRML /* Similar to recv() system call; read data from the stream, optionally * peeking, optionally retrieving OOB data */ PHPAPI int php_stream_xport_recvfrom(php_stream *stream, char *buf, size_t buflen, - long flags, void **addr, socklen_t *addrlen, char **textaddr, int *textaddrlen + long flags, void **addr, socklen_t *addrlen, zend_string **textaddr TSRMLS_DC) { php_stream_xport_param param; @@ -455,7 +453,6 @@ PHPAPI int php_stream_xport_recvfrom(php_stream *stream, char *buf, size_t bufle } if (textaddr) { *textaddr = param.outputs.textaddr; - *textaddrlen = param.outputs.textaddrlen; } return recvd_len + param.outputs.returncode; } diff --git a/main/streams/xp_socket.c b/main/streams/xp_socket.c index eb0e1cd298..dad78beb13 100644 --- a/main/streams/xp_socket.c +++ b/main/streams/xp_socket.c @@ -247,7 +247,7 @@ static inline int sock_sendto(php_netstream_data_t *sock, const char *buf, size_ } static inline int sock_recvfrom(php_netstream_data_t *sock, char *buf, size_t buflen, int flags, - char **textaddr, long *textaddrlen, + zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen TSRMLS_DC) { @@ -260,7 +260,7 @@ static inline int sock_recvfrom(php_netstream_data_t *sock, char *buf, size_t bu ret = recvfrom(sock->socket, buf, buflen, flags, (struct sockaddr*)&sa, &sl); ret = (ret == SOCK_CONN_ERR) ? -1 : ret; php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl, - textaddr, textaddrlen, addr, addrlen TSRMLS_CC); + textaddr, addr, addrlen TSRMLS_CC); } else { ret = recv(sock->socket, buf, buflen, flags); ret = (ret == SOCK_CONN_ERR) ? -1 : ret; @@ -338,7 +338,6 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void case STREAM_XPORT_OP_GET_NAME: xparam->outputs.returncode = php_network_get_sock_name(sock->socket, xparam->want_textaddr ? &xparam->outputs.textaddr : NULL, - xparam->want_textaddr ? &xparam->outputs.textaddrlen : NULL, xparam->want_addr ? &xparam->outputs.addr : NULL, xparam->want_addr ? &xparam->outputs.addrlen : NULL TSRMLS_CC); @@ -347,7 +346,6 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void case STREAM_XPORT_OP_GET_PEER_NAME: xparam->outputs.returncode = php_network_get_peer_name(sock->socket, xparam->want_textaddr ? &xparam->outputs.textaddr : NULL, - xparam->want_textaddr ? &xparam->outputs.textaddrlen : NULL, xparam->want_addr ? &xparam->outputs.addr : NULL, xparam->want_addr ? &xparam->outputs.addrlen : NULL TSRMLS_CC); @@ -383,7 +381,6 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void xparam->inputs.buf, xparam->inputs.buflen, flags, xparam->want_textaddr ? &xparam->outputs.textaddr : NULL, - xparam->want_textaddr ? &xparam->outputs.textaddrlen : NULL, xparam->want_addr ? &xparam->outputs.addr : NULL, xparam->want_addr ? &xparam->outputs.addrlen : NULL TSRMLS_CC); @@ -533,7 +530,7 @@ static inline int parse_unix_address(php_stream_xport_param *xparam, struct sock } #endif -static inline char *parse_ip_address_ex(const char *str, size_t str_len, int *portno, int get_err, char **err TSRMLS_DC) +static inline char *parse_ip_address_ex(const char *str, size_t str_len, int *portno, int get_err, zend_string **err TSRMLS_DC) { char *colon; char *host = NULL; @@ -546,7 +543,7 @@ static inline char *parse_ip_address_ex(const char *str, size_t str_len, int *po p = memchr(str + 1, ']', str_len - 2); if (!p || *(p + 1) != ':') { if (get_err) { - spprintf(err, 0, "Failed to parse IPv6 address \"%s\"", str); + *err = strpprintf(0, "Failed to parse IPv6 address \"%s\"", str); } return NULL; } @@ -564,7 +561,7 @@ static inline char *parse_ip_address_ex(const char *str, size_t str_len, int *po host = estrndup(str, colon - str); } else { if (get_err) { - spprintf(err, 0, "Failed to parse address \"%s\"", str); + *err = strpprintf(0, "Failed to parse address \"%s\"", str); } return NULL; } @@ -593,7 +590,7 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t * if (sock->socket == SOCK_ERR) { if (xparam->want_errortext) { - spprintf(&xparam->outputs.error_text, 0, "Failed to create unix%s socket %s", + xparam->outputs.error_text = strpprintf(0, "Failed to create unix%s socket %s", stream->ops == &php_stream_unix_socket_ops ? "" : "datagram", strerror(errno)); } @@ -664,7 +661,7 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_ if (sock->socket == SOCK_ERR) { if (xparam->want_errortext) { - spprintf(&xparam->outputs.error_text, 0, "Failed to create unix socket"); + xparam->outputs.error_text = strpprintf(0, "Failed to create unix socket"); } return -1; } @@ -692,7 +689,7 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_ if (PHP_STREAM_CONTEXT(stream) && (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "bindto")) != NULL) { if (Z_TYPE_P(tmpzval) != IS_STRING) { if (xparam->want_errortext) { - spprintf(&xparam->outputs.error_text, 0, "local_addr context option is not a string."); + xparam->outputs.error_text = strpprintf(0, "local_addr context option is not a string."); } efree(host); return -1; @@ -756,7 +753,6 @@ static inline int php_tcp_sockop_accept(php_stream *stream, php_netstream_data_t clisock = php_network_accept_incoming(sock->socket, xparam->want_textaddr ? &xparam->outputs.textaddr : NULL, - xparam->want_textaddr ? &xparam->outputs.textaddrlen : NULL, xparam->want_addr ? &xparam->outputs.addr : NULL, xparam->want_addr ? &xparam->outputs.addrlen : NULL, xparam->inputs.timeout, diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 8fe195f6d7..11dcb9ed9c 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -1210,7 +1210,7 @@ static void php_cli_server_logf(const char *format TSRMLS_DC, ...) /* {{{ */ efree(buf); } /* }}} */ -static int php_network_listen_socket(const char *host, int *port, int socktype, int *af, socklen_t *socklen, char **errstr TSRMLS_DC) /* {{{ */ +static int php_network_listen_socket(const char *host, int *port, int socktype, int *af, socklen_t *socklen, zend_string **errstr TSRMLS_DC) /* {{{ */ { int retval = SOCK_ERR; int err = 0; @@ -1325,7 +1325,7 @@ out: closesocket(retval); } if (errstr) { - *errstr = php_socket_strerror(err, NULL, 0); + *errstr = php_socket_error_str(err); } return SOCK_ERR; } @@ -1795,12 +1795,12 @@ static int php_cli_server_client_ctor(php_cli_server_client *client, php_cli_ser client->addr = addr; client->addr_len = addr_len; { - char *addr_str = 0; - long addr_str_len = 0; - php_network_populate_name_from_sockaddr(addr, addr_len, &addr_str, &addr_str_len, NULL, 0 TSRMLS_CC); - client->addr_str = pestrndup(addr_str, addr_str_len, 1); - client->addr_str_len = addr_str_len; - efree(addr_str); + zend_string *addr_str = 0; + + php_network_populate_name_from_sockaddr(addr, addr_len, &addr_str, NULL, 0 TSRMLS_CC); + client->addr_str = pestrndup(addr_str->val, addr_str->len, 1); + client->addr_str_len = addr_str->len; + STR_RELEASE(addr_str); } php_http_parser_init(&client->parser, PHP_HTTP_REQUEST); client->request_read = 0; @@ -2187,7 +2187,7 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c { int retval = SUCCESS; char *host = NULL; - char *errstr = NULL; + zend_string *errstr = NULL; char *_document_root = NULL; char *_router = NULL; int err = 0; @@ -2234,8 +2234,10 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c server_sock = php_network_listen_socket(host, &port, SOCK_STREAM, &server->address_family, &server->socklen, &errstr TSRMLS_CC); if (server_sock == SOCK_ERR) { - php_cli_server_logf("Failed to listen on %s:%d (reason: %s)" TSRMLS_CC, host, port, errstr ? errstr: "?"); - efree(errstr); + php_cli_server_logf("Failed to listen on %s:%d (reason: %s)" TSRMLS_CC, host, port, errstr ? errstr->val : "?"); + if (errstr) { + STR_RELEASE(errstr); + } retval = FAILURE; goto out; }