From 8baba2845031377d293e9649d103a28836cbab37 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Thu, 8 Sep 2016 09:57:38 +0300 Subject: [PATCH] Cleanup modutil_get_next_MySQL_packet - Single entry/single exit. - Variables declared as they are needed. - The GWBUF_EMPTY check removed as it only looks at the first buffer in a chain. That is, if there had been a non-empty chain where the first buffer is empty, the function would incorrectly have reported that the buffer contains no packet. - Documentation updated. --- server/core/modutil.c | 87 ++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 50 deletions(-) diff --git a/server/core/modutil.c b/server/core/modutil.c index 62e6af6d7..4026f3277 100644 --- a/server/core/modutil.c +++ b/server/core/modutil.c @@ -491,65 +491,52 @@ int modutil_send_mysql_err_packet(DCB *dcb, } /** - * Buffer contains at least one of the following: - * complete [complete] [partial] mysql packet + * Return the first packet from a buffer. * - * return pointer to gwbuf containing a complete packet or - * NULL if no complete packet was found. + * @param p_readbuf Pointer to pointer to GWBUF. If the GWBUF contains a + * complete packet, after the call it will have been updated + * to begin at the byte following the packet. + * + * @return Pointer to GWBUF if the buffer contained at least one complete packet, + * otherwise NULL. + * + * @attention The returned GWBUF is not necessarily contiguous. */ GWBUF* modutil_get_next_MySQL_packet(GWBUF** p_readbuf) { - GWBUF* packetbuf; - GWBUF* readbuf; - size_t packetlen; - size_t totalbuflen; - uint8_t* data; + GWBUF *packet = NULL; + GWBUF *readbuf = *p_readbuf; - readbuf = *p_readbuf; + if (readbuf) + { + CHK_GWBUF(readbuf); - if (readbuf == NULL) - { - packetbuf = NULL; - goto return_packetbuf; - } - CHK_GWBUF(readbuf); + size_t totalbuflen = gwbuf_length(readbuf); + if (totalbuflen >= MYSQL_HEADER_LEN) + { + size_t packetlen; - if (GWBUF_EMPTY(readbuf)) - { - packetbuf = NULL; - goto return_packetbuf; - } - totalbuflen = gwbuf_length(readbuf); - if (totalbuflen < MYSQL_HEADER_LEN) - { - packetbuf = NULL; - goto return_packetbuf; + if (GWBUF_LENGTH(readbuf) >= 3) // The length is in the 3 first bytes. + { + uint8_t *data = (uint8_t *)GWBUF_DATA((readbuf)); + packetlen = MYSQL_GET_PACKET_LEN(data) + 4; + } + else + { + // The header is split between two GWBUFs. + uint8_t data[3]; + gwbuf_copy_data(readbuf, 0, 3, data); + packetlen = MYSQL_GET_PACKET_LEN(data) + 4; + } + + if (packetlen <= totalbuflen) + { + packet = gwbuf_split(p_readbuf, packetlen); + } + } } - if (GWBUF_LENGTH(readbuf) >= 3) // The length is in the 3 first bytes. - { - data = (uint8_t *)GWBUF_DATA((readbuf)); - packetlen = MYSQL_GET_PACKET_LEN(data) + 4; - } - else - { - // The header is split between two GWBUFs. - uint8_t length[3]; - gwbuf_copy_data(readbuf, 0, 3, length); - packetlen = MYSQL_GET_PACKET_LEN(length) + 4; - } - - /** packet is incomplete */ - if (packetlen > totalbuflen) - { - packetbuf = NULL; - goto return_packetbuf; - } - - packetbuf = gwbuf_split(p_readbuf, packetlen); - -return_packetbuf: - return packetbuf; + return packet; } /**