Skip to content

Commit dfb3a79

Browse files
committed
Fix #80067: Omitting the port in bindto setting errors
A recent commit[1] which fixed a memory leak introduced a regression regarding the formerly liberal handling of IP addresses to bind to. We fix this by reverting that commit, and fix the memory leak where it actually occurs. In other words, this fix is less intrusive than the former fix. [1] <http://git.php.net/?p=php-src.git;a=commit;h=0b8c83f5936581942715d14883cdebddc18bad30> Closes GH-6104.
1 parent 2d4aa1e commit dfb3a79

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ PHP NEWS
3030
. Fixed bug #80077 (getmxrr test bug). (Rainer Jung)
3131
. Fixed bug #72941 (Modifying bucket->data by-ref has no effect any longer).
3232
(cmb)
33+
. Fixed bug #80067 (Omitting the port in bindto setting errors). (cmb)
3334

3435
03 Sep 2020, PHP 7.3.22
3536

ext/standard/tests/network/bindto.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ $fp = stream_socket_client(
1313
);
1414
?>
1515
--EXPECTF--
16-
Warning: stream_socket_client(): unable to connect to tcp://%s:80 (Failed to parse address "invalid") in %s on line %d
16+
Warning: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: %s in %s on line %d
17+
18+
Warning: stream_socket_client(): unable to connect to tcp://www.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.com:80 (php_network_getaddresses: getaddrinfo failed: %s) in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Bug #80067 (Omitting the port in bindto setting errors)
3+
--SKIPIF--
4+
<?php
5+
if (getenv("SKIP_ONLINE_TESTS")) die('skip online test');
6+
?>
7+
--FILE--
8+
<?php
9+
$context = stream_context_create(['socket' => ['bindto' => '0']]);
10+
var_dump(file_get_contents('https://httpbin.org/get', false, $context) !== false);
11+
?>
12+
--EXPECT--
13+
bool(true)

main/network.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka
200200

201201
if ((n = getaddrinfo(host, NULL, &hints, &res))) {
202202
if (error_string) {
203+
/* free error string received during previous iteration (if any) */
204+
if (*error_string) {
205+
zend_string_release_ex(*error_string, 0);
206+
}
203207
*error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo failed: %s", PHP_GAI_STRERROR(n));
204208
php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(*error_string));
205209
} else {
@@ -208,6 +212,10 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka
208212
return 0;
209213
} else if (res == NULL) {
210214
if (error_string) {
215+
/* free error string received during previous iteration (if any) */
216+
if (*error_string) {
217+
zend_string_release_ex(*error_string, 0);
218+
}
211219
*error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo failed (null result pointer) errno=%d", errno);
212220
php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(*error_string));
213221
} else {
@@ -241,6 +249,10 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka
241249
}
242250
if (host_info == NULL) {
243251
if (error_string) {
252+
/* free error string received during previous iteration (if any) */
253+
if (*error_string) {
254+
zend_string_release_ex(*error_string, 0);
255+
}
244256
*error_string = strpprintf(0, "php_network_getaddresses: gethostbyname failed. errno=%d", errno);
245257
php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(*error_string));
246258
} else {

main/streams/xp_socket.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,10 +744,6 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
744744
return -1;
745745
}
746746
bindto = parse_ip_address_ex(Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval), &bindport, xparam->want_errortext, &xparam->outputs.error_text);
747-
if (bindto == NULL) {
748-
efree(host);
749-
return -1;
750-
}
751747
}
752748

753749
#ifdef SO_BROADCAST

0 commit comments

Comments
 (0)