Skip to content

Commit c9de303

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: NEWS Fix GH-20528: Regression breaks mysql connexion using an IPv6 address enclosed in square brackets
2 parents 10ac41f + 769f319 commit c9de303

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
@@ -553,13 +553,24 @@ MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)(MYSQLND_CONN_DATA * conn, MYSQLND_
553553
port = 3306;
554554
}
555555

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

0 commit comments

Comments
 (0)