Skip to content

Commit b481273

Browse files
committed
MFH: Bug #38687 - sockaddr local storage insufficient for all sock families
1 parent 6024885 commit b481273

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

main/network.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -787,37 +787,46 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
787787
/* make a connection attempt */
788788

789789
if (bindto) {
790-
struct sockaddr local_address;
790+
struct sockaddr *local_address = NULL;
791+
int local_address_len = 0;
791792

792793
if (sa->sa_family == AF_INET) {
793-
struct sockaddr_in *in4 = (struct sockaddr_in*)&local_address;
794+
struct sockaddr_in *in4 = emalloc(sizeof(struct sockaddr_in));
795+
796+
local_address = (struct sockaddr*)in4;
797+
local_address_len = sizeof(struct sockaddr_in);
794798

795799
in4->sin_family = sa->sa_family;
796800
in4->sin_port = htons(bindport);
797801
if (!inet_aton(bindto, &in4->sin_addr)) {
798-
goto bad_ip;
802+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
803+
goto skip_bind;
799804
}
800805
memset(&(in4->sin_zero), 0, sizeof(in4->sin_zero));
801806
}
802807
#if HAVE_IPV6 && HAVE_INET_PTON
803808
else { /* IPV6 */
804-
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&local_address;
809+
struct sockaddr_in6 *in6 = emalloc(sizeof(struct sockaddr_in6));
810+
811+
local_address = (struct sockaddr*)in6;
812+
local_address_len = sizeof(struct sockaddr_in6);
805813

806814
in6->sin6_family = sa->sa_family;
807815
in6->sin6_port = htons(bindport);
808816
if (inet_pton(AF_INET6, bindto, &in6->sin6_addr) < 1) {
809-
goto bad_ip;
817+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
818+
goto skip_bind;
810819
}
811820
}
812821
#endif
813-
if (bind(sock, &local_address, sizeof(struct sockaddr))) {
822+
if (!local_address || bind(sock, local_address, local_address_len)) {
814823
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to bind to '%s:%d', system said: %s", bindto, bindport, strerror(errno));
815824
}
816-
goto bind_done;
817-
bad_ip:
818-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
825+
skip_bind:
826+
if (local_address) {
827+
efree(local_address);
828+
}
819829
}
820-
bind_done:
821830
/* free error string recieved during previous iteration (if any) */
822831
if (error_string && *error_string) {
823832
efree(*error_string);

0 commit comments

Comments
 (0)