Fixed queries without assignment operators failing to be properly canonized.

This commit is contained in:
Markus Makela 2015-10-26 20:12:04 +02:00
parent bed6666338
commit 9064212353
4 changed files with 74 additions and 1 deletions

View File

@ -1410,6 +1410,17 @@ char* skygw_get_canonical(
querystr = replace_literal(querystr, item->name, "?");
}
} /*< for */
/** Check for SET ... options with no Item classes */
if (thd->free_list == NULL)
{
char *replaced = replace_quoted(querystr);
if (replaced)
{
free(querystr);
querystr = replaced;
}
}
retblock:
return querystr;
}

View File

@ -1,2 +1,2 @@
add_library(utils skygw_utils.cc ../server/core/atomic.c)
target_link_libraries(utils stdc++)
target_link_libraries(utils stdc++ ${PCRE2_LIBRARIES})

View File

@ -16,6 +16,9 @@
* Copyright MariaDB Corporation Ab 2013-2014
*/
#ifndef PCRE2_CODE_UNIT_WIDTH
#define PCRE2_CODE_UNIT_WIDTH 8
#endif
#include <stdio.h>
#include <stdlib.h>
@ -30,6 +33,7 @@
#include <sys/time.h>
#include "skygw_utils.h"
#include <atomic.h>
#include <pcre2.h>
#if defined(MLIST)
@ -2117,6 +2121,63 @@ retblock:
return newstr;
}
/**
* Replace everything inside single or double quotes with question marks.
* @param str String to modify
* @return Pointer to new modified string or NULL if memory allocation failed
*/
char* replace_quoted(const char* str)
{
PCRE2_SIZE erroffset;
int errcore;
static const PCRE2_SPTR pattern = (PCRE2_SPTR) "(['\"])[^'\"]+(['\"])";
static const PCRE2_SPTR replace = (PCRE2_SPTR) "$1?$2";
pcre2_code* re;
pcre2_match_data* mdata;
int orig_len = strlen(str);
size_t len = strlen(str);
char* output;
if ((output = (char*) malloc(len * sizeof(char))))
{
/** TODO: Consider moving pattern compilation to some init function. */
if ((re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED,
0, &errcore, &erroffset, NULL)))
{
if ((mdata = pcre2_match_data_create_from_pattern(re, NULL)))
{
while (pcre2_substitute(re, (PCRE2_SPTR) str, orig_len, 0,
PCRE2_SUBSTITUTE_GLOBAL, mdata, NULL,
replace, PCRE2_ZERO_TERMINATED,
(PCRE2_UCHAR8*) output, &len) == PCRE2_ERROR_NOMEMORY)
{
char* tmp = (char*) realloc(output, len *= 2);
if (tmp == NULL)
{
free(output);
output = NULL;
break;
}
output = tmp;
}
pcre2_match_data_free(mdata);
}
else
{
free(output);
output = NULL;
}
pcre2_code_free(re);
}
else
{
free(output);
output = NULL;
}
}
return output;
}
/**
* Calculate the number of decimal numbers from a size_t value.
*

View File

@ -277,6 +277,7 @@ size_t get_decimal_len(size_t s);
char* replace_literal(char* haystack,
const char* needle,
const char* replacement);
char* replace_quoted(const char* str);
bool is_valid_posix_path(char* path);
bool strip_escape_chars(char*);
int simple_str_hash(char* key);