@@ -13,7 +13,7 @@ extern zend_result php_string_to_if_index(const char *val, unsigned *out);
1313
1414#ifdef HAVE_IPV6
1515/* Sets addr by hostname, or by ip in string form (AF_INET6) */
16- int php_set_inet6_addr (struct sockaddr_in6 * sin6 , char * string , php_socket * php_sock ) /* {{{ */
16+ zend_result php_set_inet6_addr (struct sockaddr_in6 * sin6 , char * string , php_socket * php_sock ) /* {{{ */
1717{
1818 struct in6_addr tmp ;
1919#ifdef HAVE_GETADDRINFO
@@ -41,12 +41,12 @@ int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_
4141#else
4242 PHP_SOCKET_ERROR (php_sock , "Host lookup failed" , (-10000 - h_errno ));
4343#endif
44- return 0 ;
44+ return FAILURE ;
4545 }
4646 if (addrinfo -> ai_family != PF_INET6 || addrinfo -> ai_addrlen != sizeof (struct sockaddr_in6 )) {
4747 php_error_docref (NULL , E_WARNING , "Host lookup failed: Non AF_INET6 domain returned on AF_INET6 socket" );
4848 freeaddrinfo (addrinfo );
49- return 0 ;
49+ return FAILURE ;
5050 }
5151
5252 memcpy (& (sin6 -> sin6_addr .s6_addr ), ((struct sockaddr_in6 * )(addrinfo -> ai_addr ))-> sin6_addr .s6_addr , sizeof (struct in6_addr ));
@@ -55,36 +55,43 @@ int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_
5555#else
5656 /* No IPv6 specific hostname resolution is available on this system? */
5757 php_error_docref (NULL , E_WARNING , "Host lookup failed: getaddrinfo() not available on this system" );
58- return 0 ;
58+ return FAILURE ;
5959#endif
6060
6161 }
6262
6363 if (scope ) {
6464 zend_long lval = 0 ;
6565 double dval = 0 ;
66- unsigned scope_id = 0 ;
67-
66+ uint32_t scope_id = 0 ;
6867 scope ++ ;
6968
69+ if (* scope == '\0' ) {
70+ zend_value_error ("scope cannot be empty" );
71+ return FAILURE ;
72+ }
73+
74+
7075 if (IS_LONG == is_numeric_string (scope , strlen (scope ), & lval , & dval , 0 )) {
71- if (lval > 0 && (zend_ulong )lval <= UINT_MAX ) {
72- scope_id = lval ;
76+ if (lval <= 0 || (zend_ulong )lval > UINT_MAX ) {
77+ zend_value_error ("scope must be between 1 and %u" , UINT_MAX );
78+ return FAILURE ;
7379 }
80+ scope_id = lval ;
7481 } else {
7582 php_string_to_if_index (scope , & scope_id );
7683 }
7784
7885 sin6 -> sin6_scope_id = scope_id ;
7986 }
8087
81- return 1 ;
88+ return SUCCESS ;
8289}
8390/* }}} */
8491#endif
8592
8693/* Sets addr by hostname, or by ip in string form (AF_INET) */
87- int php_set_inet_addr (struct sockaddr_in * sin , char * string , php_socket * php_sock ) /* {{{ */
94+ zend_result php_set_inet_addr (struct sockaddr_in * sin , char * string , php_socket * php_sock ) /* {{{ */
8895{
8996 struct in_addr tmp ;
9097 struct hostent * host_entry ;
@@ -99,46 +106,46 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_soc
99106#else
100107 PHP_SOCKET_ERROR (php_sock , "Host lookup failed" , (-10000 - h_errno ));
101108#endif
102- return 0 ;
109+ return FAILURE ;
103110 }
104111 if (host_entry -> h_addrtype != AF_INET ) {
105112 php_error_docref (NULL , E_WARNING , "Host lookup failed: Non AF_INET domain returned on AF_INET socket" );
106- return 0 ;
113+ return FAILURE ;
107114 }
108115 memcpy (& (sin -> sin_addr .s_addr ), host_entry -> h_addr_list [0 ], host_entry -> h_length );
109116 }
110117
111- return 1 ;
118+ return SUCCESS ;
112119}
113120/* }}} */
114121
115122/* Sets addr by hostname or by ip in string form (AF_INET or AF_INET6,
116123 * depending on the socket) */
117- int php_set_inet46_addr (php_sockaddr_storage * ss , socklen_t * ss_len , char * string , php_socket * php_sock ) /* {{{ */
124+ zend_result php_set_inet46_addr (php_sockaddr_storage * ss , socklen_t * ss_len , char * string , php_socket * php_sock ) /* {{{ */
118125{
119126 if (php_sock -> type == AF_INET ) {
120127 struct sockaddr_in t = {0 };
121- if (php_set_inet_addr (& t , string , php_sock )) {
128+ if (php_set_inet_addr (& t , string , php_sock ) == SUCCESS ) {
122129 memcpy (ss , & t , sizeof t );
123130 ss -> ss_family = AF_INET ;
124131 * ss_len = sizeof (t );
125- return 1 ;
132+ return SUCCESS ;
126133 }
127134 }
128135#ifdef HAVE_IPV6
129136 else if (php_sock -> type == AF_INET6 ) {
130137 struct sockaddr_in6 t = {0 };
131- if (php_set_inet6_addr (& t , string , php_sock )) {
138+ if (php_set_inet6_addr (& t , string , php_sock ) == SUCCESS ) {
132139 memcpy (ss , & t , sizeof t );
133140 ss -> ss_family = AF_INET6 ;
134141 * ss_len = sizeof (t );
135- return 1 ;
142+ return SUCCESS ;
136143 }
137144 }
138145#endif
139146 else {
140147 php_error_docref (NULL , E_WARNING ,
141148 "IP address used in the context of an unexpected type of socket" );
142149 }
143- return 0 ;
150+ return FAILURE ;
144151}
0 commit comments