Skip to content

Commit a893a49

Browse files
Implement mysqlnd_set_persistent_string (#7371)
1 parent 6b05d95 commit a893a49

File tree

5 files changed

+35
-80
lines changed

5 files changed

+35
-80
lines changed

ext/mysqlnd/mysqlnd_alloc.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,16 @@ static inline void mysqlnd_set_string(MYSQLND_STRING *buf, const char *string, s
8383
}
8484
}
8585

86+
static inline void mysqlnd_set_persistent_string(MYSQLND_STRING *buf, const char *string, size_t len, bool persistent) {
87+
if (buf->s) {
88+
mnd_pefree(buf->s, persistent);
89+
buf->s = NULL;
90+
buf->l = 0;
91+
}
92+
if (string) {
93+
buf->s = mnd_pestrndup(string, len, persistent);
94+
buf->l = len;
95+
}
96+
}
97+
8698
#endif /* MYSQLND_ALLOC_H */

ext/mysqlnd/mysqlnd_auth.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ mysqlnd_run_authentication(
9999
switch_to_auth_protocol = NULL;
100100
switch_to_auth_protocol_len = 0;
101101

102-
if (conn->authentication_plugin_data.s) {
103-
mnd_pefree(conn->authentication_plugin_data.s, conn->persistent);
104-
conn->authentication_plugin_data.s = NULL;
105-
}
102+
mysqlnd_set_persistent_string(&conn->authentication_plugin_data, NULL, 0, conn->persistent);
106103
conn->authentication_plugin_data.l = plugin_data_len;
107104
conn->authentication_plugin_data.s = mnd_pemalloc(conn->authentication_plugin_data.l, conn->persistent);
108105
memcpy(conn->authentication_plugin_data.s, plugin_data, plugin_data_len);
@@ -490,24 +487,11 @@ mysqlnd_auth_change_user(MYSQLND_CONN_DATA * const conn,
490487
}
491488
}
492489
if (ret == PASS) {
493-
char * tmp = NULL;
494-
/* if we get conn->username as parameter and then we first free it, then estrndup it, we will crash */
495-
tmp = mnd_pestrndup(user, user_len, conn->persistent);
496-
if (conn->username.s) {
497-
mnd_pefree(conn->username.s, conn->persistent);
498-
}
499-
conn->username.s = tmp;
490+
ZEND_ASSERT(conn->username.s != user && conn->password.s != passwd);
491+
mysqlnd_set_persistent_string(&conn->username, user, user_len, conn->persistent);
492+
mysqlnd_set_persistent_string(&conn->password, passwd, passwd_len, conn->persistent);
500493

501-
tmp = mnd_pestrdup(passwd, conn->persistent);
502-
if (conn->password.s) {
503-
mnd_pefree(conn->password.s, conn->persistent);
504-
}
505-
conn->password.s = tmp;
506-
507-
if (conn->last_message.s) {
508-
mnd_efree(conn->last_message.s);
509-
conn->last_message.s = NULL;
510-
}
494+
mysqlnd_set_string(&conn->last_message, NULL, 0);
511495
UPSERT_STATUS_RESET(conn->upsert_status);
512496
/* set charset for old servers */
513497
if (conn->m->get_server_version(conn) < 50123) {

ext/mysqlnd/mysqlnd_commands.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ MYSQLND_METHOD(mysqlnd_command, init_db)(MYSQLND_CONN_DATA * const conn, const M
107107
*/
108108
UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
109109
if (ret == PASS) {
110-
if (conn->connect_or_select_db.s) {
111-
mnd_pefree(conn->connect_or_select_db.s, conn->persistent);
112-
}
113-
conn->connect_or_select_db.s = mnd_pestrndup(db.s, db.l, conn->persistent);
114-
conn->connect_or_select_db.l = db.l;
110+
mysqlnd_set_persistent_string(&conn->connect_or_select_db, db.s, db.l, conn->persistent);
115111
}
116112

117113
DBG_RETURN(ret);

ext/mysqlnd/mysqlnd_connection.c

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -282,31 +282,14 @@ MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn)
282282

283283
DBG_INF("Freeing memory of members");
284284

285-
if (conn->hostname.s) {
286-
mnd_pefree(conn->hostname.s, pers);
287-
conn->hostname.s = NULL;
288-
}
289-
if (conn->username.s) {
290-
mnd_pefree(conn->username.s, pers);
291-
conn->username.s = NULL;
292-
}
293-
if (conn->password.s) {
294-
mnd_pefree(conn->password.s, pers);
295-
conn->password.s = NULL;
296-
}
297-
if (conn->connect_or_select_db.s) {
298-
mnd_pefree(conn->connect_or_select_db.s, pers);
299-
conn->connect_or_select_db.s = NULL;
300-
}
301-
if (conn->unix_socket.s) {
302-
mnd_pefree(conn->unix_socket.s, pers);
303-
conn->unix_socket.s = NULL;
304-
}
285+
mysqlnd_set_persistent_string(&conn->hostname, NULL, 0, pers);
286+
mysqlnd_set_persistent_string(&conn->username, NULL, 0, pers);
287+
mysqlnd_set_persistent_string(&conn->password, NULL, 0, pers);
288+
mysqlnd_set_persistent_string(&conn->connect_or_select_db, NULL, 0, pers);
289+
mysqlnd_set_persistent_string(&conn->unix_socket, NULL, 0, pers);
305290
DBG_INF_FMT("scheme=%s", conn->scheme.s);
306-
if (conn->scheme.s) {
307-
mnd_pefree(conn->scheme.s, pers);
308-
conn->scheme.s = NULL;
309-
}
291+
mysqlnd_set_persistent_string(&conn->scheme, NULL, 0, pers);
292+
310293
if (conn->server_version) {
311294
mnd_pefree(conn->server_version, pers);
312295
conn->server_version = NULL;
@@ -315,14 +298,8 @@ MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn)
315298
mnd_pefree(conn->host_info, pers);
316299
conn->host_info = NULL;
317300
}
318-
if (conn->authentication_plugin_data.s) {
319-
mnd_pefree(conn->authentication_plugin_data.s, pers);
320-
conn->authentication_plugin_data.s = NULL;
321-
}
322-
if (conn->last_message.s) {
323-
mnd_efree(conn->last_message.s);
324-
conn->last_message.s = NULL;
325-
}
301+
mysqlnd_set_persistent_string(&conn->authentication_plugin_data, NULL, 0, pers);
302+
mysqlnd_set_string(&conn->last_message, NULL, 0);
326303

327304
conn->charset = NULL;
328305
conn->greet_charset = NULL;
@@ -406,10 +383,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, end_psession)(MYSQLND_CONN_DATA * conn)
406383
conn->current_result->m.free_result(conn->current_result, TRUE);
407384
conn->current_result = NULL;
408385
}
409-
if (conn->last_message.s) {
410-
mnd_efree(conn->last_message.s);
411-
conn->last_message.s = NULL;
412-
}
386+
mysqlnd_set_string(&conn->last_message, NULL, 0);
413387
conn->error_info = &conn->error_info_impl;
414388
DBG_RETURN(PASS);
415389
}
@@ -681,8 +655,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
681655
pfc->data->compressed = mysql_flags & CLIENT_COMPRESS? TRUE:FALSE;
682656

683657

684-
conn->scheme.s = mnd_pestrndup(transport.s, transport.l, conn->persistent);
685-
conn->scheme.l = transport.l;
658+
mysqlnd_set_persistent_string(&conn->scheme, transport.s, transport.l, conn->persistent);
686659
if (transport.s) {
687660
mnd_sprintf_free(transport.s);
688661
transport.s = NULL;
@@ -692,17 +665,13 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
692665
goto err; /* OOM */
693666
}
694667

695-
conn->username.l = username.l;
696-
conn->username.s = mnd_pestrndup(username.s, conn->username.l, conn->persistent);
697-
conn->password.l = password.l;
698-
conn->password.s = mnd_pestrndup(password.s, conn->password.l, conn->persistent);
668+
mysqlnd_set_persistent_string(&conn->username, username.s, username.l, conn->persistent);
669+
mysqlnd_set_persistent_string(&conn->password, username.s, password.l, conn->persistent);
699670
conn->port = port;
700-
conn->connect_or_select_db.l = database.l;
701-
conn->connect_or_select_db.s = mnd_pestrndup(database.s, conn->connect_or_select_db.l, conn->persistent);
671+
mysqlnd_set_persistent_string(&conn->connect_or_select_db, database.s, database.l, conn->persistent);
702672

703673
if (!unix_socket && !named_pipe) {
704-
conn->hostname.s = mnd_pestrndup(hostname.s, hostname.l, conn->persistent);
705-
conn->hostname.l = hostname.l;
674+
mysqlnd_set_persistent_string(&conn->hostname, hostname.s, hostname.l, conn->persistent);
706675
{
707676
char *p;
708677
mnd_sprintf(&p, 0, "%s via TCP/IP", conn->hostname.s);

ext/mysqlnd/mysqlnd_wireprotocol.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,10 +1127,7 @@ void php_mysqlnd_rset_header_free_mem(void * _packet)
11271127
{
11281128
MYSQLND_PACKET_RSET_HEADER *p= (MYSQLND_PACKET_RSET_HEADER *) _packet;
11291129
DBG_ENTER("php_mysqlnd_rset_header_free_mem");
1130-
if (p->info_or_local_file.s) {
1131-
mnd_efree(p->info_or_local_file.s);
1132-
p->info_or_local_file.s = NULL;
1133-
}
1130+
mysqlnd_set_string(&p->info_or_local_file, NULL, 0);
11341131
DBG_VOID_RETURN;
11351132
}
11361133
/* }}} */
@@ -1764,10 +1761,7 @@ static
17641761
void php_mysqlnd_stats_free_mem(void * _packet)
17651762
{
17661763
MYSQLND_PACKET_STATS *p= (MYSQLND_PACKET_STATS *) _packet;
1767-
if (p->message.s) {
1768-
mnd_efree(p->message.s);
1769-
p->message.s = NULL;
1770-
}
1764+
mysqlnd_set_string(&p->message, NULL, 0);
17711765
}
17721766
/* }}} */
17731767

0 commit comments

Comments
 (0)