@@ -787,37 +787,46 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
787
787
/* make a connection attempt */
788
788
789
789
if (bindto ) {
790
- struct sockaddr local_address ;
790
+ struct sockaddr * local_address = NULL ;
791
+ int local_address_len = 0 ;
791
792
792
793
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 );
794
798
795
799
in4 -> sin_family = sa -> sa_family ;
796
800
in4 -> sin_port = htons (bindport );
797
801
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 ;
799
804
}
800
805
memset (& (in4 -> sin_zero ), 0 , sizeof (in4 -> sin_zero ));
801
806
}
802
807
#if HAVE_IPV6 && HAVE_INET_PTON
803
808
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 );
805
813
806
814
in6 -> sin6_family = sa -> sa_family ;
807
815
in6 -> sin6_port = htons (bindport );
808
816
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 ;
810
819
}
811
820
}
812
821
#endif
813
- if (bind (sock , & local_address , sizeof ( struct sockaddr ) )) {
822
+ if (! local_address || bind (sock , local_address , local_address_len )) {
814
823
php_error_docref (NULL TSRMLS_CC , E_WARNING , "failed to bind to '%s:%d', system said: %s" , bindto , bindport , strerror (errno ));
815
824
}
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
+ }
819
829
}
820
- bind_done :
821
830
/* free error string recieved during previous iteration (if any) */
822
831
if (error_string && * error_string ) {
823
832
efree (* error_string );
0 commit comments