update MySQL protocol

This commit is contained in:
Xie Han
2023-08-15 21:02:36 +08:00
parent 260ba129e8
commit f9c3dc514e
9 changed files with 23 additions and 43 deletions

View File

@@ -440,7 +440,6 @@ int MySQLAuthResponse::decode_packet(const unsigned char *buf, size_t buflen)
const unsigned char *pos;
const unsigned char *str;
unsigned long long len;
int ret;
if (end == buf)
return -2;
@@ -472,11 +471,7 @@ int MySQLAuthResponse::decode_packet(const unsigned char *buf, size_t buflen)
default:
pos = buf;
ret = decode_string(&str, &len, &pos, end);
if (ret <= 0)
return ret;
if (len == 1)
if (decode_string(&str, &len, &pos, end) > 0 && len == 1)
{
if (*str == 0x03)
{

View File

@@ -24,7 +24,6 @@
#include "ProtocolMessage.h"
#include "mysql_stream.h"
#include "mysql_parser.h"
#include "mysql_byteorder.h"
/**
* @file MySQLMessage.h

View File

@@ -16,8 +16,10 @@
Authors: Li Yingxin (liyingxin@sogou-inc.com)
*/
#include <vector>
#include "mysql_types.h"
#include "mysql_byteorder.h"
#include "MySQLMessage.h"
#include "MySQLResult.h"
namespace protocol
@@ -196,7 +198,7 @@ bool MySQLResultCursor::fetch_row(std::vector<MySQLCell>& row_arr)
len = 0;
p++;
data_type = MYSQL_TYPE_NULL;
} else if (decode_string(&data, &len, &p, end) == false) {
} else if (decode_string(&data, &len, &p, end) == 0) {
this->status = MYSQL_STATUS_ERROR;
return false;
}
@@ -241,7 +243,7 @@ bool MySQLResultCursor::fetch_row_nocopy(const void **data, size_t *len, int *da
cell_len = 0;
p++;
}
else if (decode_string(&cell_data, &cell_len, &p, end) == false)
else if (decode_string(&cell_data, &cell_len, &p, end) == 0)
{
this->status = MYSQL_STATUS_ERROR;
return false;
@@ -287,7 +289,7 @@ bool MySQLResultCursor::fetch_all(std::vector<std::vector<MySQLCell>>& rows)
p++;
data_type = MYSQL_TYPE_NULL;
}
else if (decode_string(&data, &len, &p, end) == false)
else if (decode_string(&data, &len, &p, end) == 0)
{
this->status = MYSQL_STATUS_ERROR;
return false;

View File

@@ -23,8 +23,9 @@
#include <vector>
#include <string>
#include <unordered_map>
#include "MySQLMessage.h"
#include "mysql_types.h"
#include "mysql_parser.h"
#include "MySQLMessage.h"
/**
* @file MySQLResult.h

View File

@@ -23,6 +23,9 @@ int decode_length_safe(unsigned long long *res, const unsigned char **pos,
{
const unsigned char *p = *pos;
if (p >= end)
return 0;
switch (*p)
{
default:
@@ -74,12 +77,8 @@ int decode_string(const unsigned char **str, unsigned long long *len,
if (decode_length_safe(&length, pos, end) <= 0)
return 0;
if (length == 0 || length == (~0ULL))
{
*len = 0;
*str = NULL;
return 1;
}
if (length == (~0ULL))
length = 0;
if (*pos + length > end)
return 0;
@@ -89,3 +88,4 @@ int decode_string(const unsigned char **str, unsigned long long *len,
*pos = *pos + length;
return 1;
}

View File

@@ -19,7 +19,7 @@
#ifndef _MYSQL_BYTEORDER_H_
#define _MYSQL_BYTEORDER_H_
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <stdint.h>

View File

@@ -182,10 +182,10 @@ static int parse_ok_packet(const void *buf, size_t len, mysql_parser_t *parser)
const unsigned char *buf_end = (const unsigned char *)buf + len;
unsigned long long affected_rows, insert_id, info_len;
const unsigned char *str;
struct __mysql_result_set *result_set;
unsigned int warning_count;
int server_status;
int ret;
p += 1;// 0x00
if (decode_length_safe(&affected_rows, &p, buf_end) <= 0)
@@ -202,31 +202,20 @@ static int parse_ok_packet(const void *buf, size_t len, mysql_parser_t *parser)
warning_count = uint2korr(p);
p += 2;
if (p < buf_end)
if (server_status & MYSQL_SERVER_SESSION_STATE_CHANGED)
{
ret = decode_length_safe(&info_len, &p, buf_end);
if (ret > 0)
{
if (info_len == ~0ULL)
info_len = 0;
if (p + info_len > buf_end)
return -2;
}
else if (ret < 0)
info_len = 0;
else
if (decode_string(&str, &info_len, &p, buf_end) == 0)
return -2;
} else {
ret = 1;
info_len = 0;
str = p;
}
result_set = (struct __mysql_result_set *)malloc(sizeof(struct __mysql_result_set));
if (result_set == NULL)
return -1;
result_set->info_offset = p - (const unsigned char *)buf;
result_set->info_offset = str - (const unsigned char *)buf;
result_set->info_len = info_len;
result_set->affected_rows = (affected_rows == ~0ULL) ? 0 : affected_rows;
result_set->insert_id = (insert_id == ~0ULL) ? 0 : insert_id;
@@ -241,13 +230,7 @@ static int parse_ok_packet(const void *buf, size_t len, mysql_parser_t *parser)
parser->packet_type = MYSQL_PACKET_OK;
parser->buf = buf;
parser->offset = result_set->info_offset + result_set->info_len;
if (ret < 0)
{
parser->parse = parse_error_packet;
return 0;
}
parser->offset = p - (const unsigned char *)buf;
if (server_status & MYSQL_SERVER_MORE_RESULTS_EXIST)
{

View File

@@ -19,7 +19,7 @@
#ifndef _MYSQL_PARSER_H_
#define _MYSQL_PARSER_H_
#include <stdlib.h>
#include <stddef.h>
#include "list.h"
// the first byte in response message

View File

@@ -23,8 +23,8 @@
#define MYSQL_STATE_LENGTH 5
#define MYSQL_STATE_DEFAULT "HY000"
// may be set by server in EOF/OK packet
#define MYSQL_SERVER_MORE_RESULTS_EXIST 0x0008
#define MYSQL_SERVER_SESSION_STATE_CHANGED 0x4000
enum
{