@@ -57,6 +57,10 @@ extern void __res_ndestroy(res_state statp);
5757#endif
5858#endif
5959
60+ #ifndef HAVE_INET_NTOP
61+ #error inet_ntop unsupported on this platform
62+ #endif
63+
6064#ifndef MAXHOSTNAMELEN
6165#define MAXHOSTNAMELEN 255
6266#endif
@@ -221,6 +225,7 @@ PHP_FUNCTION(gethostbyname)
221225{
222226 char * hostname ;
223227 size_t hostname_len ;
228+ zend_string * ipaddr ;
224229
225230 ZEND_PARSE_PARAMETERS_START (1 , 1 )
226231 Z_PARAM_PATH (hostname , hostname_len )
@@ -232,7 +237,12 @@ PHP_FUNCTION(gethostbyname)
232237 RETURN_STRINGL (hostname , hostname_len );
233238 }
234239
235- RETURN_STR (php_gethostbyname (hostname ));
240+ if (!(ipaddr = php_gethostbyname (hostname ))) {
241+ php_error_docref (NULL , E_WARNING , "Host name to ip failed %s" , hostname );
242+ RETURN_STRINGL (hostname , hostname_len );
243+ } else {
244+ RETURN_STR (ipaddr );
245+ }
236246}
237247/* }}} */
238248
@@ -244,9 +254,7 @@ PHP_FUNCTION(gethostbynamel)
244254 struct hostent * hp ;
245255 struct in_addr in ;
246256 int i ;
247- #ifdef HAVE_INET_NTOP
248257 char addr4 [INET_ADDRSTRLEN ];
249- #endif
250258
251259 ZEND_PARSE_PARAMETERS_START (1 , 1 )
252260 Z_PARAM_PATH (hostname , hostname_len )
@@ -267,18 +275,21 @@ PHP_FUNCTION(gethostbynamel)
267275
268276 for (i = 0 ;; i ++ ) {
269277 /* On macos h_addr_list entries may be misaligned. */
278+ const char * ipaddr ;
270279 struct in_addr * h_addr_entry ; /* Don't call this h_addr, it's a macro! */
271280 memcpy (& h_addr_entry , & hp -> h_addr_list [i ], sizeof (struct in_addr * ));
272281 if (!h_addr_entry ) {
273282 return ;
274283 }
275284
276285 in = * h_addr_entry ;
277- #ifdef HAVE_INET_NTOP
278- add_next_index_string (return_value , inet_ntop (AF_INET , & in , addr4 , INET_ADDRSTRLEN ));
279- #else
280- add_next_index_string (return_value , inet_ntoa (in ));
281- #endif
286+ if (!(ipaddr = inet_ntop (AF_INET , & in , addr4 , INET_ADDRSTRLEN ))) {
287+ /* unlikely regarding (too) long hostname and protocols but checking still */
288+ php_error_docref (NULL , E_WARNING , "Host name to ip failed %s" , hostname );
289+ continue ;
290+ } else {
291+ add_next_index_string (return_value , ipaddr );
292+ }
282293 }
283294}
284295/* }}} */
@@ -289,9 +300,7 @@ static zend_string *php_gethostbyname(char *name)
289300 struct hostent * hp ;
290301 struct in_addr * h_addr_0 ; /* Don't call this h_addr, it's a macro! */
291302 struct in_addr in ;
292- #ifdef HAVE_INET_NTOP
293303 char addr4 [INET_ADDRSTRLEN ];
294- #endif
295304 const char * address ;
296305
297306 hp = php_network_gethostbyname (name );
@@ -307,11 +316,10 @@ static zend_string *php_gethostbyname(char *name)
307316
308317 memcpy (& in .s_addr , h_addr_0 , sizeof (in .s_addr ));
309318
310- #ifdef HAVE_INET_NTOP
311- address = inet_ntop (AF_INET , & in , addr4 , INET_ADDRSTRLEN );
312- #else
313- address = inet_ntoa (in );
314- #endif
319+ if (!(address = inet_ntop (AF_INET , & in , addr4 , INET_ADDRSTRLEN ))) {
320+ return NULL ;
321+ }
322+
315323 return zend_string_init (address , strlen (address ), 0 );
316324}
317325/* }}} */
0 commit comments