Allocate match_data only when necessary

If the session doesn't match the required username or remote address, the
match data is not allocated. This also doubles as a replacement of the
active member variable.
This commit is contained in:
Markus Mäkelä
2019-01-11 16:32:02 +02:00
parent 38879e6d0f
commit f38ee85d5b

View File

@ -76,7 +76,6 @@ struct RegexSession
pthread_mutex_t lock; pthread_mutex_t lock;
int no_change; /* No. of unchanged requests */ int no_change; /* No. of unchanged requests */
int replacements; /* No. of changed requests */ int replacements; /* No. of changed requests */
int active; /* Is filter active */
pcre2_match_data* match_data; /*< Matching data used by the compiled regex */ pcre2_match_data* match_data; /*< Matching data used by the compiled regex */
}; };
@ -240,6 +239,22 @@ static MXS_FILTER* createInstance(const char* name, MXS_CONFIG_PARAMETER* params
return (MXS_FILTER*) my_instance; return (MXS_FILTER*) my_instance;
} }
bool matching_connection(RegexInstance* my_instance, MXS_SESSION* session)
{
bool rval = true;
if (my_instance->source && strcmp(session_get_remote(session), my_instance->source) != 0)
{
rval = false;
}
else if (my_instance->user && strcmp(session_get_user(session), my_instance->user) != 0)
{
rval = false;
}
return rval;
}
/** /**
* Associate a new session with this instance of the filter. * Associate a new session with this instance of the filter.
* *
@ -250,43 +265,20 @@ static MXS_FILTER* createInstance(const char* name, MXS_CONFIG_PARAMETER* params
static MXS_FILTER_SESSION* newSession(MXS_FILTER* instance, MXS_SESSION* session) static MXS_FILTER_SESSION* newSession(MXS_FILTER* instance, MXS_SESSION* session)
{ {
RegexInstance* my_instance = (RegexInstance*) instance; RegexInstance* my_instance = (RegexInstance*) instance;
RegexSession* my_session; RegexSession* my_session = static_cast<RegexSession*>(MXS_CALLOC(1, sizeof(RegexSession)));
const char* remote, * user;
pcre2_match_data* match_data = pcre2_match_data_create_from_pattern(my_instance->re, NULL);
if (!match_data) if (my_session)
{
MXS_OOM();
return NULL;
}
if ((my_session = static_cast<RegexSession*>(MXS_CALLOC(1, sizeof(RegexSession)))) != NULL)
{ {
my_session->no_change = 0; my_session->no_change = 0;
my_session->replacements = 0; my_session->replacements = 0;
my_session->active = 1; my_session->match_data = nullptr;
my_session->match_data = match_data;
if (my_instance->source && (remote = session_get_remote(session))) if (matching_connection(my_instance, session))
{ {
if (strcmp(remote, my_instance->source)) my_session->match_data = pcre2_match_data_create_from_pattern(my_instance->re, NULL);
{
my_session->active = 0;
} }
} }
if (my_instance->user && (user = session_get_user(session))
&& strcmp(user, my_instance->user))
{
my_session->active = 0;
}
}
else
{
pcre2_match_data_free(match_data);
}
return (MXS_FILTER_SESSION*)my_session; return (MXS_FILTER_SESSION*)my_session;
} }
@ -344,7 +336,7 @@ static int routeQuery(MXS_FILTER* instance, MXS_FILTER_SESSION* session, GWBUF*
RegexSession* my_session = (RegexSession*) session; RegexSession* my_session = (RegexSession*) session;
char* sql, * newsql; char* sql, * newsql;
if (my_session->active && modutil_is_SQL(queue)) if (my_session->match_data && modutil_is_SQL(queue))
{ {
if ((sql = modutil_get_SQL(queue)) != NULL) if ((sql = modutil_get_SQL(queue)) != NULL)
{ {