Skip to content

Commit c1103a9

Browse files
committed
Fix implicit/explicit port in mysqlnd
1 parent 6e3f93f commit c1103a9

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

NEWS

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.1.24
44

5-
5+
- MySQLnd:
6+
. Fixed bug GH-10270 (Invalid error message when connection via SSL fails:
7+
"trying to connect via (null)"). (Kamil Tekiela)
68

79
31 Aug 2023, PHP 8.1.23
810

ext/mysqli/tests/gh8978.phpt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug GH-8267 (Invalid error message when connection via SSL fails)
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+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
14+
$mysql = mysqli_init();
15+
// Ignore this warning as we are providing wrong information on purpose
16+
mysqli_ssl_set($mysql, 'x509.key', 'x509.pem', 'x509.ca', null, null);
17+
try {
18+
// There should be no warning here, only exception
19+
mysqli_real_connect($mysql, '127.0.0.1:3306', 'username', 'password', null, null, null, MYSQLI_CLIENT_SSL);
20+
} catch (mysqli_sql_exception $e) {
21+
echo $e->getMessage()."\n";
22+
}
23+
24+
echo 'done!';
25+
?>
26+
--EXPECTF--
27+
Warning: failed loading cafile stream: `x509.ca' in %s
28+
Cannot connect to MySQL using SSL
29+
done!

ext/mysqlnd/mysqlnd_commands.c

+2
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ MYSQLND_METHOD(mysqlnd_command, enable_ssl)(MYSQLND_CONN_DATA * const conn, cons
571571
conn->vio->data->m.set_client_option(conn->vio, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char *) &verify);
572572

573573
if (FAIL == conn->vio->data->m.enable_ssl(conn->vio)) {
574+
SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
575+
SET_CLIENT_ERROR(conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, "Cannot connect to MySQL using SSL");
574576
goto end;
575577
}
576578
}

ext/mysqlnd/mysqlnd_connection.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn)
289289
mysqlnd_set_persistent_string(&conn->unix_socket, NULL, 0, pers);
290290
DBG_INF_FMT("scheme=%s", conn->scheme.s);
291291
mysqlnd_set_persistent_string(&conn->scheme, NULL, 0, pers);
292-
292+
293293
if (conn->server_version) {
294294
mnd_pefree(conn->server_version, pers);
295295
conn->server_version = NULL;
@@ -726,19 +726,20 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
726726
DBG_RETURN(PASS);
727727
}
728728
err:
729-
if (transport.s) {
730-
mnd_sprintf_free(transport.s);
731-
transport.s = NULL;
732-
}
733-
734-
DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, conn->scheme.s);
729+
DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, transport.s ? transport.s : conn->scheme.s);
735730
if (!conn->error_info->error_no) {
731+
/* There was an unknown error if the connection failed but we have no error number */
736732
char * msg;
737-
mnd_sprintf(&msg, 0, "%s (trying to connect via %s)",conn->error_info->error, conn->scheme.s);
733+
mnd_sprintf(&msg, 0, "Unknown error while trying to connect via %s", transport.s ? transport.s : conn->scheme.s);
738734
SET_CLIENT_ERROR(conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, msg);
739735
mnd_sprintf_free(msg);
740736
}
741737

738+
if (transport.s) {
739+
mnd_sprintf_free(transport.s);
740+
transport.s = NULL;
741+
}
742+
742743
conn->m->free_contents(conn);
743744
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_FAILURE);
744745
DBG_RETURN(FAIL);

ext/mysqlnd/mysqlnd_vio.c

-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,6 @@ MYSQLND_METHOD(mysqlnd_vio, enable_ssl)(MYSQLND_VIO * const net)
569569
php_stream_xport_crypto_enable(net_stream, 1) < 0)
570570
{
571571
DBG_ERR("Cannot connect to MySQL by using SSL");
572-
php_error_docref(NULL, E_WARNING, "Cannot connect to MySQL by using SSL");
573572
DBG_RETURN(FAIL);
574573
}
575574
net->data->ssl = TRUE;

0 commit comments

Comments
 (0)