Skip to content

Commit 8398038

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: NEWS NEWS NEWS Fix GH-20528: Regression breaks mysql connexion using an IPv6 address enclosed in square brackets
2 parents ca7f556 + e221948 commit 8398038

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
mysqli_connect() with port in host
3+
--EXTENSIONS--
4+
mysqli
5+
--SKIPIF--
6+
<?php
7+
require_once 'skipifconnectfailure.inc';
8+
?>
9+
--FILE--
10+
<?php
11+
require_once 'connect.inc';
12+
13+
// using port / host arguments
14+
if (!$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
15+
printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
16+
$host, $user, $db, $port, $socket);
17+
}
18+
19+
mysqli_close($link);
20+
21+
// using port in host
22+
if (!$link = mysqli_connect("$host:$port", $user, $passwd, $db, "1$port", $socket)) {
23+
printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
24+
"$host:$port", $user, $db, "1$port", $socket);
25+
}
26+
27+
mysqli_close($link);
28+
?>
29+
Done
30+
--EXPECTF--
31+
Done

ext/mysqlnd/mysqlnd_connection.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,24 @@ MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)(MYSQLND_CONN_DATA * conn, MYSQLND_
548548
port = 3306;
549549
}
550550

551-
/* ipv6 addresses are in the format [address]:port */
552551
if (hostname.s[0] != '[' && mysqlnd_fast_is_ipv6_address(hostname.s)) {
552+
/* IPv6 without square brackets so without port */
553553
transport.l = mnd_sprintf(&transport.s, 0, "tcp://[%s]:%u", hostname.s, port);
554554
} else {
555-
/* Not ipv6, but could already contain a port number, in which case we should not add an extra port.
555+
char *p;
556+
557+
/* IPv6 addresses are in the format [address]:port */
558+
if (hostname.s[0] == '[') { /* IPv6 */
559+
p = strchr(hostname.s, ']');
560+
if (p && p[1] != ':') {
561+
p = NULL;
562+
}
563+
} else { /* IPv4 or name */
564+
p = strchr(hostname.s, ':');
565+
}
566+
/* Could already contain a port number, in which case we should not add an extra port.
556567
* See GH-8978. In a port doubling scenario, the first port would be used so we do the same to keep BC. */
557-
if (strchr(hostname.s, ':')) {
568+
if (p) {
558569
/* TODO: Ideally we should be able to get rid of this workaround in the future. */
559570
transport.l = mnd_sprintf(&transport.s, 0, "tcp://%s", hostname.s);
560571
} else {

0 commit comments

Comments
 (0)