Skip to content

Commit

Permalink
Merge pull request #1358 from Barenboim/master
Browse files Browse the repository at this point in the history
Update MySQL parser to fix OK package parsing bug.
  • Loading branch information
Barenboim authored Aug 17, 2023
2 parents 3d8b3a8 + 8fb40d6 commit b05368a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 41 deletions.
25 changes: 17 additions & 8 deletions src/protocol/MySQLMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ int MySQLMessage::append(const void *buf, size_t *size)
{
const void *stream_buf;
size_t stream_len;
size_t nleft = *size;
size_t n;
int ret;

cur_size_ += *size;
Expand All @@ -107,17 +109,24 @@ int MySQLMessage::append(const void *buf, size_t *size)
return -1;
}

ret = mysql_stream_write(buf, *size, stream_);
if (ret > 0)
while (nleft > 0)
{
seqid_ = mysql_stream_get_seq(stream_);
mysql_stream_get_buf(&stream_buf, &stream_len, stream_);
ret = decode_packet((const unsigned char *)stream_buf, stream_len);
if (ret == -2)
n = nleft;
ret = mysql_stream_write(buf, &n, stream_);
if (ret > 0)
{
errno = EBADMSG;
ret = -1;
seqid_ = mysql_stream_get_seq(stream_);
mysql_stream_get_buf(&stream_buf, &stream_len, stream_);
ret = decode_packet((const unsigned char *)stream_buf, stream_len);
if (ret == -2)
errno = EBADMSG;
}

if (ret < 0)
return -1;

nleft -= n;
buf = (const char *)buf + n;
}

return ret;
Expand Down
23 changes: 8 additions & 15 deletions src/protocol/mysql_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,18 @@ static int parse_ok_packet(const void *buf, size_t len, mysql_parser_t *parser)
warning_count = uint2korr(p);
p += 2;

if (server_status & MYSQL_SERVER_SESSION_STATE_CHANGED)
if (p != buf_end)
{
const unsigned char *tmp_str;
unsigned long long tmp_len;

if (decode_string(&str, &info_len, &p, buf_end) == 0)
return -2;

if (decode_string(&tmp_str, &tmp_len, &p, buf_end) == 0)
return -2;

} else if (p != buf_end &&
*p != MYSQL_PACKET_HEADER_OK &&
*p != MYSQL_PACKET_HEADER_NULL &&
*p != MYSQL_PACKET_HEADER_EOF &&
*p != MYSQL_PACKET_HEADER_ERROR)
{
if (decode_string(&str, &info_len, &p, buf_end) == 0)
return -2;
if (server_status & MYSQL_SERVER_SESSION_STATE_CHANGED)
{
const unsigned char *tmp_str;
unsigned long long tmp_len;
if (decode_string(&tmp_str, &tmp_len, &p, buf_end) == 0)
return -2;
}
} else {
str = p;
info_len = 0;
Expand Down
17 changes: 0 additions & 17 deletions src/protocol/mysql_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,6 @@ static int __mysql_stream_write_payload(const void *buf, size_t *n,
return stream->payload_length != (1 << 24) - 1;
}

int mysql_stream_write(const void *buf, size_t n, mysql_stream_t *stream)
{
size_t nleft = n;
int ret;

while (1)
{
ret = stream->write(buf, &n, stream);
if (nleft == n)
return ret;

nleft -= n;
buf = (const char *)buf + n;
n = nleft;
}
}

void mysql_stream_init(mysql_stream_t *stream)
{
stream->head_left = 4;
Expand Down
7 changes: 6 additions & 1 deletion src/protocol/mysql_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ extern "C"
#endif

void mysql_stream_init(mysql_stream_t *stream);
int mysql_stream_write(const void *buf, size_t n, mysql_stream_t *stream);

#ifdef __cplusplus
}
#endif

static inline int mysql_stream_write(const void *buf, size_t *n,
mysql_stream_t *stream)
{
return stream->write(buf, n, stream);
}

static inline int mysql_stream_get_seq(mysql_stream_t *stream)
{
return stream->sequence_id;
Expand Down

0 comments on commit b05368a

Please sign in to comment.