Skip to content

Commit 53797c2

Browse files
cmb69nikic
andcommitted
Fix #78210: Invalid pointer address
This is actually about three distinct issues: * If an empty string is passed as $address to `stream_socket_sendto()`, the `sa` is not initialized, so we must not pass it as `addr` to `php_stream_xport_sendto()`. * On POSIX, `recvfrom()` truncates messages which are too long to fit into the specified buffer (unless `MSG_PEEK` is given), discards the excessive bytes, and returns the buffer length. On Windows, the same happens, but `recvfrom()` returns `SOCKET_ERROR` with the error code `WSAEMSGSIZE`. We have to catch this for best POSIX compatibility. * In `php_network_parse_network_address_with_port()`, we have to zero `in6` (not only its alias `sa`) to properly support IPv6. Co-Authored-By: Nikita Popov <[email protected]>
1 parent 2462f2d commit 53797c2

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PHP NEWS
44

55
- Core:
66
. Fixed bug #79364 (When copy empty array, next key is unspecified). (cmb)
7+
. Fixed bug #78210 (Invalid pointer address). (cmb, Nikita)
78

89
- Spl:
910
. Fixed bug #75673 (SplStack::unserialize() behavior). (cmb)

ext/standard/streamsfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ PHP_FUNCTION(stream_socket_sendto)
366366
}
367367
}
368368

369-
RETURN_LONG(php_stream_xport_sendto(stream, data, datalen, (int)flags, target_addr ? &sa : NULL, sl));
369+
RETURN_LONG(php_stream_xport_sendto(stream, data, datalen, (int)flags, target_addr_len ? &sa : NULL, sl));
370370
}
371371
/* }}} */
372372

main/network.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,11 @@ PHPAPI int php_network_parse_network_address_with_port(const char *addr, zend_lo
512512
zend_string *errstr = NULL;
513513
#if HAVE_IPV6
514514
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)sa;
515-
#endif
516515

517-
memset(sa, 0, sizeof(struct sockaddr));
516+
memset(in6, 0, sizeof(struct sockaddr_in6));
517+
#else
518+
memset(in4, 0, sizeof(struct sockaddr_in));
519+
#endif
518520

519521
if (*addr == '[') {
520522
colon = memchr(addr + 1, ']', addrlen-1);

main/streams/xp_socket.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ static inline int sock_recvfrom(php_netstream_data_t *sock, char *buf, size_t bu
272272
socklen_t sl = sizeof(sa);
273273
ret = recvfrom(sock->socket, buf, XP_SOCK_BUF_SIZE(buflen), flags, (struct sockaddr*)&sa, &sl);
274274
ret = (ret == SOCK_CONN_ERR) ? -1 : ret;
275+
#ifdef PHP_WIN32
276+
/* POSIX discards excess bytes without signalling failure; emulate this on Windows */
277+
if (ret == -1 && WSAGetLastError() == WSAEMSGSIZE) {
278+
ret = buflen;
279+
}
280+
#endif
275281
if (sl) {
276282
php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
277283
textaddr, addr, addrlen);

0 commit comments

Comments
 (0)