Change modutil_get_SQL() to modutil_extract_SQL() + length limit

The modutil_get_SQL()-function allocates storage, while
modutil_extract_SQL() does not. The strings given by the latter
are not 0-terminated so require a length limit when matched using
regexec().

This commit changes the used function in those cases where the
sql-string is not modified nor is the pointer saved for later use.
This commit is contained in:
Esa Korhonen
2017-06-14 17:49:52 +03:00
parent 49556d07b1
commit 7e59254454
4 changed files with 20 additions and 21 deletions

View File

@ -284,6 +284,7 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
CCR_INSTANCE *my_instance = (CCR_INSTANCE *)instance; CCR_INSTANCE *my_instance = (CCR_INSTANCE *)instance;
CCR_SESSION *my_session = (CCR_SESSION *)session; CCR_SESSION *my_session = (CCR_SESSION *)session;
char *sql; char *sql;
regmatch_t limits[] = {{0, 0}};
time_t now = time(NULL); time_t now = time(NULL);
if (modutil_is_SQL(queue)) if (modutil_is_SQL(queue))
@ -294,13 +295,13 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
*/ */
if (qc_query_is_type(qc_get_type_mask(queue), QUERY_TYPE_WRITE)) if (qc_query_is_type(qc_get_type_mask(queue), QUERY_TYPE_WRITE))
{ {
if ((sql = modutil_get_SQL(queue)) != NULL) if (modutil_extract_SQL(queue, &sql, &limits[0].rm_eo))
{ {
if (my_instance->nomatch == NULL || if (my_instance->nomatch == NULL ||
(my_instance->nomatch && regexec(&my_instance->nore, sql, 0, NULL, 0) != 0)) (my_instance->nomatch && regexec(&my_instance->nore, sql, 0, limits, REG_STARTEND) != 0))
{ {
if (my_instance->match == NULL || if (my_instance->match == NULL ||
(my_instance->match && regexec(&my_instance->re, sql, 0, NULL, 0) == 0)) (my_instance->match && regexec(&my_instance->re, sql, 0, limits, REG_STARTEND) == 0))
{ {
if (my_instance->count) if (my_instance->count)
{ {
@ -317,8 +318,6 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
my_instance->stats.n_modified++; my_instance->stats.n_modified++;
} }
} }
MXS_FREE(sql);
} }
} }
else if (my_session->hints_left > 0) else if (my_session->hints_left > 0)

View File

@ -310,12 +310,13 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
REGEXHINT_INSTANCE *my_instance = (REGEXHINT_INSTANCE *) instance; REGEXHINT_INSTANCE *my_instance = (REGEXHINT_INSTANCE *) instance;
REGEXHINT_SESSION *my_session = (REGEXHINT_SESSION *) session; REGEXHINT_SESSION *my_session = (REGEXHINT_SESSION *) session;
char *sql; char *sql;
regmatch_t limits[] = {{0, 0}};
if (modutil_is_SQL(queue) && my_session->active) if (modutil_is_SQL(queue) && my_session->active)
{ {
if ((sql = modutil_get_SQL(queue)) != NULL) if (modutil_extract_SQL(queue, &sql, &limits[0].rm_eo))
{ {
if (regexec(&my_instance->re, sql, 0, NULL, 0) == 0) if (regexec(&my_instance->re, sql, 0, limits, REG_STARTEND) == 0)
{ {
queue->hint = hint_create_route(queue->hint, queue->hint = hint_create_route(queue->hint,
HINT_ROUTE_TO_NAMED_SERVER, HINT_ROUTE_TO_NAMED_SERVER,
@ -326,7 +327,6 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
{ {
my_session->n_undiverted++; my_session->n_undiverted++;
} }
MXS_FREE(sql);
} }
} }
return my_session->down.routeQuery(my_session->down.instance, return my_session->down.routeQuery(my_session->down.instance,

View File

@ -507,19 +507,19 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
{ {
QLA_INSTANCE *my_instance = (QLA_INSTANCE *) instance; QLA_INSTANCE *my_instance = (QLA_INSTANCE *) instance;
QLA_SESSION *my_session = (QLA_SESSION *) session; QLA_SESSION *my_session = (QLA_SESSION *) session;
char *ptr = NULL; char *sql;
int length = 0;
struct tm t; struct tm t;
struct timeval tv; struct timeval tv;
regmatch_t limits[] = {{0, 0}};
if (my_session->active) if (my_session->active)
{ {
if (modutil_extract_SQL(queue, &ptr, &length)) if (modutil_extract_SQL(queue, &sql, &limits[0].rm_eo))
{ {
if ((my_instance->match == NULL || if ((my_instance->match == NULL ||
regexec(&my_instance->re, ptr, 0, NULL, 0) == 0) && regexec(&my_instance->re, sql, 0, limits, REG_STARTEND) == 0) &&
(my_instance->nomatch == NULL || (my_instance->nomatch == NULL ||
regexec(&my_instance->nore, ptr, 0, NULL, 0) != 0)) regexec(&my_instance->nore, sql, 0, limits, REG_STARTEND) != 0))
{ {
char buffer[QLA_DATE_BUFFER_SIZE]; char buffer[QLA_DATE_BUFFER_SIZE];
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
@ -530,8 +530,7 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
* Loop over all the possible log file modes and write to * Loop over all the possible log file modes and write to
* the enabled files. * the enabled files.
*/ */
int length = limits[0].rm_eo;
char *sql_string = ptr;
bool write_error = false; bool write_error = false;
if (my_instance->log_mode_flags & CONFIG_FILE_SESSION) if (my_instance->log_mode_flags & CONFIG_FILE_SESSION)
{ {
@ -541,7 +540,7 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
~LOG_DATA_SESSION); ~LOG_DATA_SESSION);
if (write_log_entry(data_flags, my_session->fp, if (write_log_entry(data_flags, my_session->fp,
my_instance, my_session, buffer, sql_string, length) < 0) my_instance, my_session, buffer, sql, length) < 0)
{ {
write_error = true; write_error = true;
} }
@ -550,7 +549,7 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue)
{ {
uint32_t data_flags = my_instance->log_file_data_flags; uint32_t data_flags = my_instance->log_file_data_flags;
if (write_log_entry(data_flags, my_instance->unified_fp, if (write_log_entry(data_flags, my_instance->unified_fp,
my_instance, my_session, buffer, sql_string, length) < 0) my_instance, my_session, buffer, sql, length) < 0)
{ {
write_error = true; write_error = true;
} }

View File

@ -846,16 +846,17 @@ GWBUF* clone_query(TEE_INSTANCE* my_instance, TEE_SESSION* my_session, GWBUF* bu
} }
else else
{ {
char *ptr = modutil_get_SQL(buffer); char *ptr = NULL;
regmatch_t limits[] = {{0, 0}};
modutil_extract_SQL(buffer, &ptr, &limits[0].rm_eo);
if (ptr) if (ptr)
{ {
if ((my_instance->match && regexec(&my_instance->re, ptr, 0, NULL, 0) == 0) || if ((my_instance->match && regexec(&my_instance->re, ptr, 0, limits, REG_STARTEND) == 0) ||
(my_instance->nomatch && regexec(&my_instance->nore, ptr, 0, NULL, 0) != 0)) (my_instance->nomatch && regexec(&my_instance->nore, ptr, 0, limits, REG_STARTEND) != 0))
{ {
clone = gwbuf_clone(buffer); clone = gwbuf_clone(buffer);
} }
MXS_FREE(ptr);
} }
} }