Skip to content

Commit 3508b07

Browse files
Tidy up buffer preparation in mysqlnd (#9834)
1 parent dfc760b commit 3508b07

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

ext/mysqlnd/mysqlnd_driver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ MYSQLND_METHOD(mysqlnd_object_factory, get_prepared_statement)(MYSQLND_CONN_DATA
199199
mysqlnd_upsert_status_init(&stmt->upsert_status_impl);
200200
stmt->upsert_status = &(stmt->upsert_status_impl);
201201
stmt->state = MYSQLND_STMT_INITTED;
202-
stmt->execute_cmd_buffer.length = 4096;
202+
stmt->execute_cmd_buffer.length = MYSQLND_NET_CMD_BUFFER_MIN_SIZE;
203203
stmt->execute_cmd_buffer.buffer = mnd_emalloc(stmt->execute_cmd_buffer.length);
204204
stmt->prefetch_rows = MYSQLND_DEFAULT_PREFETCH_ROWS;
205205

ext/mysqlnd/mysqlnd_wireprotocol.c

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -323,35 +323,28 @@ mysqlnd_read_packet_header_and_body(MYSQLND_PACKET_HEADER * packet_header,
323323
static enum_func_status
324324
php_mysqlnd_greet_read(MYSQLND_CONN_DATA * conn, void * _packet)
325325
{
326-
zend_uchar buf[2048];
327-
const zend_uchar * p = buf;
328-
const zend_uchar * const begin = buf;
329326
const zend_uchar * pad_start = NULL;
330327
MYSQLND_PACKET_GREET *packet= (MYSQLND_PACKET_GREET *) _packet;
331328
MYSQLND_ERROR_INFO * error_info = conn->error_info;
332329
MYSQLND_PFC * pfc = conn->protocol_frame_codec;
333330
MYSQLND_VIO * vio = conn->vio;
334331
MYSQLND_STATS * stats = conn->stats;
335332
MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
333+
const size_t buf_len = pfc->cmd_buffer.length;
334+
zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer;
335+
const zend_uchar * p = buf;
336+
const zend_uchar * const begin = buf;
336337

337338
DBG_ENTER("php_mysqlnd_greet_read");
338339

339-
if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, sizeof(buf), "greeting", PROT_GREET_PACKET)) {
340+
if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "greeting", PROT_GREET_PACKET)) {
340341
DBG_RETURN(FAIL);
341342
}
342343
BAIL_IF_NO_MORE_DATA;
343344

344345
packet->authentication_plugin_data.s = packet->intern_auth_plugin_data;
345346
packet->authentication_plugin_data.l = sizeof(packet->intern_auth_plugin_data);
346347

347-
if (packet->header.size < sizeof(buf)) {
348-
/*
349-
Null-terminate the string, so strdup can work even if the packets have a string at the end,
350-
which is not ASCIIZ
351-
*/
352-
buf[packet->header.size] = '\0';
353-
}
354-
355348
packet->protocol_version = uint1korr(p);
356349
p++;
357350
BAIL_IF_NO_MORE_DATA;
@@ -642,8 +635,6 @@ size_t php_mysqlnd_auth_write(MYSQLND_CONN_DATA * conn, void * _packet)
642635
/* }}} */
643636

644637

645-
#define AUTH_RESP_BUFFER_SIZE 2048
646-
647638
/* {{{ php_mysqlnd_auth_response_read */
648639
static enum_func_status
649640
php_mysqlnd_auth_response_read(MYSQLND_CONN_DATA * conn, void * _packet)
@@ -654,27 +645,18 @@ php_mysqlnd_auth_response_read(MYSQLND_CONN_DATA * conn, void * _packet)
654645
MYSQLND_VIO * vio = conn->vio;
655646
MYSQLND_STATS * stats = conn->stats;
656647
MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
657-
zend_uchar local_buf[AUTH_RESP_BUFFER_SIZE];
658-
size_t buf_len = pfc->cmd_buffer.buffer? pfc->cmd_buffer.length: AUTH_RESP_BUFFER_SIZE;
659-
zend_uchar *buf = pfc->cmd_buffer.buffer? (zend_uchar *) pfc->cmd_buffer.buffer : local_buf;
648+
const size_t buf_len = pfc->cmd_buffer.length;
649+
zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer;
660650
const zend_uchar * p = buf;
661651
const zend_uchar * const begin = buf;
662652

663653
DBG_ENTER("php_mysqlnd_auth_response_read");
664654

665-
/* leave space for terminating safety \0 */
666-
buf_len--;
667655
if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "OK", PROT_OK_PACKET)) {
668656
DBG_RETURN(FAIL);
669657
}
670658
BAIL_IF_NO_MORE_DATA;
671659

672-
/*
673-
zero-terminate the buffer for safety. We are sure there is place for the \0
674-
because buf_len is -1 the size of the buffer pointed
675-
*/
676-
buf[packet->header.size] = '\0';
677-
678660
/* Should be always 0x0 or ERROR_MARKER for error */
679661
packet->response_code = uint1korr(p);
680662
p++;
@@ -806,8 +788,6 @@ php_mysqlnd_change_auth_response_write(MYSQLND_CONN_DATA * conn, void * _packet)
806788
/* }}} */
807789

808790

809-
#define OK_BUFFER_SIZE 2048
810-
811791
/* {{{ php_mysqlnd_ok_read */
812792
static enum_func_status
813793
php_mysqlnd_ok_read(MYSQLND_CONN_DATA * conn, void * _packet)
@@ -818,9 +798,8 @@ php_mysqlnd_ok_read(MYSQLND_CONN_DATA * conn, void * _packet)
818798
MYSQLND_VIO * vio = conn->vio;
819799
MYSQLND_STATS * stats = conn->stats;
820800
MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
821-
zend_uchar local_buf[OK_BUFFER_SIZE];
822-
const size_t buf_len = pfc->cmd_buffer.buffer? pfc->cmd_buffer.length : OK_BUFFER_SIZE;
823-
zend_uchar * const buf = pfc->cmd_buffer.buffer? (zend_uchar *) pfc->cmd_buffer.buffer : local_buf;
801+
const size_t buf_len = pfc->cmd_buffer.length;
802+
zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer;
824803
const zend_uchar * p = buf;
825804
const zend_uchar * const begin = buf;
826805
zend_ulong net_len;
@@ -1975,8 +1954,6 @@ size_t php_mysqlnd_sha256_pk_request_write(MYSQLND_CONN_DATA * conn, void * _pac
19751954
/* }}} */
19761955

19771956

1978-
#define SHA256_PK_REQUEST_RESP_BUFFER_SIZE 2048
1979-
19801957
/* {{{ php_mysqlnd_sha256_pk_request_response_read */
19811958
static enum_func_status
19821959
php_mysqlnd_sha256_pk_request_response_read(MYSQLND_CONN_DATA * conn, void * _packet)
@@ -1987,14 +1964,15 @@ php_mysqlnd_sha256_pk_request_response_read(MYSQLND_CONN_DATA * conn, void * _pa
19871964
MYSQLND_VIO * vio = conn->vio;
19881965
MYSQLND_STATS * stats = conn->stats;
19891966
MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
1990-
zend_uchar buf[SHA256_PK_REQUEST_RESP_BUFFER_SIZE];
1991-
zend_uchar *p = buf;
1967+
const size_t buf_len = pfc->cmd_buffer.length;
1968+
zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer;
1969+
const zend_uchar * p = buf;
19921970
const zend_uchar * const begin = buf;
19931971

19941972
DBG_ENTER("php_mysqlnd_sha256_pk_request_response_read");
19951973

19961974
/* leave space for terminating safety \0 */
1997-
if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, sizeof(buf), "SHA256_PK_REQUEST_RESPONSE", PROT_SHA256_PK_REQUEST_RESPONSE_PACKET)) {
1975+
if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "SHA256_PK_REQUEST_RESPONSE", PROT_SHA256_PK_REQUEST_RESPONSE_PACKET)) {
19981976
DBG_RETURN(FAIL);
19991977
}
20001978
BAIL_IF_NO_MORE_DATA;
@@ -2068,12 +2046,13 @@ php_mysqlnd_cached_sha2_result_read(MYSQLND_CONN_DATA * conn, void * _packet)
20682046
MYSQLND_VIO * vio = conn->vio;
20692047
MYSQLND_STATS * stats = conn->stats;
20702048
MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
2071-
zend_uchar buf[SHA256_PK_REQUEST_RESP_BUFFER_SIZE];
2072-
zend_uchar *p = buf;
2049+
const size_t buf_len = pfc->cmd_buffer.length;
2050+
zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer;
2051+
const zend_uchar * p = buf;
20732052
const zend_uchar * const begin = buf;
20742053

20752054
DBG_ENTER("php_mysqlnd_cached_sha2_result_read");
2076-
if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, sizeof(buf), "PROT_CACHED_SHA2_RESULT_PACKET", PROT_CACHED_SHA2_RESULT_PACKET)) {
2055+
if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "PROT_CACHED_SHA2_RESULT_PACKET", PROT_CACHED_SHA2_RESULT_PACKET)) {
20772056
DBG_RETURN(FAIL);
20782057
}
20792058
BAIL_IF_NO_MORE_DATA;

0 commit comments

Comments
 (0)