Move quote triming function to common place

Function for removing MySQL quote characters surrounding a
string moved from Cache's rules.cc to common place and renamed.

The function was now moved to mysql_utils.[h|c], which is in
maxscale-common, and not to the perhaps more appropriate place
protocol/mysql.h, as the latter would have implied that MySQLCommon
would have to be linked to, which actually might be the right thing
to do. However, that has to wait until protocol/mysql.h gets an
overhaul.
This commit is contained in:
Johan Wikman
2016-12-29 09:44:38 +02:00
parent b40872e600
commit c6e155cf2b
3 changed files with 85 additions and 82 deletions

View File

@ -31,4 +31,17 @@ char* mxs_lestr_consume(uint8_t** c, size_t *size);
MYSQL *mxs_mysql_real_connect(MYSQL *mysql, SERVER *server, const char *user, const char *passwd); MYSQL *mxs_mysql_real_connect(MYSQL *mysql, SERVER *server, const char *user, const char *passwd);
/**
* Trim MySQL quote characters surrounding a string.
*
* 'abcd' => abcd
* "abcd" => abcd
* `abcd` => abcd
*
* @param s The string to be trimmed.
*
* @note The string is modified in place.
*/
bool mxs_mysql_trim_quotes(char *s);
MXS_END_DECLS MXS_END_DECLS

View File

@ -168,3 +168,70 @@ MYSQL *mxs_mysql_real_connect(MYSQL *con, SERVER *server, const char *user, cons
return mysql_real_connect(con, server->name, user, passwd, NULL, server->port, NULL, 0); return mysql_real_connect(con, server->name, user, passwd, NULL, server->port, NULL, 0);
} }
bool mxs_mysql_trim_quotes(char *s)
{
bool dequoted = true;
char *i = s;
char *end = s + strlen(s);
// Remove space from the beginning
while (*i && isspace(*i))
{
++i;
}
if (*i)
{
// Remove space from the end
while (isspace(*(end - 1)))
{
*(end - 1) = 0;
--end;
}
ss_dassert(end > i);
char quote;
switch (*i)
{
case '\'':
case '"':
case '`':
quote = *i;
++i;
break;
default:
quote = 0;
}
if (quote)
{
--end;
if (*end == quote)
{
*end = 0;
memmove(s, i, end - i + 1);
}
else
{
dequoted = false;
}
}
else if (i != s)
{
memmove(s, i, end - i + 1);
}
}
else
{
*s = 0;
}
return dequoted;
}

View File

@ -18,6 +18,7 @@
#include <new> #include <new>
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <maxscale/modutil.h> #include <maxscale/modutil.h>
#include <maxscale/mysql_utils.h>
#include <maxscale/protocol/mysql.h> #include <maxscale/protocol/mysql.h>
#include <maxscale/query_classifier.h> #include <maxscale/query_classifier.h>
#include <maxscale/session.h> #include <maxscale/session.h>
@ -137,7 +138,6 @@ static bool cache_rules_parse_array(CACHE_RULES *self, json_t *store, const char
static bool cache_rules_parse_store_element(CACHE_RULES *self, json_t *object, size_t index); static bool cache_rules_parse_store_element(CACHE_RULES *self, json_t *object, size_t index);
static bool cache_rules_parse_use_element(CACHE_RULES *self, json_t *object, size_t index); static bool cache_rules_parse_use_element(CACHE_RULES *self, json_t *object, size_t index);
static bool dequote_mysql(char *s);
typedef enum pcre_quote_approach typedef enum pcre_quote_approach
{ {
@ -608,7 +608,7 @@ static CACHE_RULE *cache_rule_create_simple_user(cache_rule_attribute_t attribut
host = any; host = any;
} }
if (dequote_mysql(user)) if (mxs_mysql_trim_quotes(user))
{ {
char pcre_user[2 * len + 1]; // Surely enough char pcre_user[2 * len + 1]; // Surely enough
@ -621,7 +621,7 @@ static CACHE_RULE *cache_rule_create_simple_user(cache_rule_attribute_t attribut
mysql_to_pcre(pcre_user, user, PCRE_QUOTE_VERBATIM); mysql_to_pcre(pcre_user, user, PCRE_QUOTE_VERBATIM);
} }
if (dequote_mysql(host)) if (mxs_mysql_trim_quotes(host))
{ {
char pcre_host[2 * len + 1]; // Surely enough char pcre_host[2 * len + 1]; // Surely enough
@ -661,12 +661,12 @@ static CACHE_RULE *cache_rule_create_simple_user(cache_rule_attribute_t attribut
} }
else else
{ {
MXS_ERROR("Could not dequote host %s.", cvalue); MXS_ERROR("Could not trim quotes from host %s.", cvalue);
} }
} }
else else
{ {
MXS_ERROR("Could not dequote user %s.", cvalue); MXS_ERROR("Could not trim quotes from user %s.", cvalue);
} }
return rule; return rule;
@ -2045,83 +2045,6 @@ static bool cache_rules_parse_use_element(CACHE_RULES *self, json_t *object, siz
return rule != NULL; return rule != NULL;
} }
/**
* Remove quote characters surrounding a string.
* 'abcd' => abcd
* "abcd" => abcd
* `abcd` => abcd
*
* @param s The string to be dequoted.
*
* @note The string is modified in place.
*/
static bool dequote_mysql(char *s)
{
bool dequoted = true;
char *i = s;
char *end = s + strlen(s);
// Remove space from the beginning
while (*i && isspace(*i))
{
++i;
}
if (*i)
{
// Remove space from the end
while (isspace(*(end - 1)))
{
*(end - 1) = 0;
--end;
}
ss_dassert(end > i);
char quote;
switch (*i)
{
case '\'':
case '"':
case '`':
quote = *i;
++i;
break;
default:
quote = 0;
}
if (quote)
{
--end;
if (*end == quote)
{
*end = 0;
memmove(s, i, end - i + 1);
}
else
{
dequoted = false;
}
}
else if (i != s)
{
memmove(s, i, end - i + 1);
}
}
else
{
*s = 0;
}
return dequoted;
}
/** /**
* Convert MySQL/MariaDB account string to a pcre compatible one. * Convert MySQL/MariaDB account string to a pcre compatible one.
* *