Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix freeing regex pointers to free the global objects #971

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions src/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ static regex_data *white_regex = NULL;
static regex_data *black_regex = NULL;
static regex_data *cli_regex = NULL;

regex_data *get_regex_from_type(const enum regex_type regexid);
inline regex_data *get_regex_from_type(const enum regex_type regexid)
static inline regex_data *get_regex_ptr(const enum regex_type regexid)
{
switch (regexid)
{
Expand All @@ -50,12 +49,39 @@ inline regex_data *get_regex_from_type(const enum regex_type regexid)
}
}

static inline void free_regex_ptr(const enum regex_type regexid)
{
regex_data **regex;
switch (regexid)
{
case REGEX_BLACKLIST:
regex = &black_regex;
break;
case REGEX_WHITELIST:
regex = &white_regex;
break;
case REGEX_CLI:
regex = &cli_regex;
break;
case REGEX_MAX: // Fall through
default: // This is not possible
return;
}

// Free pointer (if not already NULL)
if(*regex != NULL)
{
free(*regex);
*regex = NULL;
}
}

#define FTL_REGEX_SEP ";"
/* Compile regular expressions into data structures that can be used with
regexec() to match against a string */
static bool compile_regex(const char *regexin, const enum regex_type regexid)
{
regex_data *regex = get_regex_from_type(regexid);
regex_data *regex = get_regex_ptr(regexid);
int index = counters->num_regex[regexid]++;

// Extract possible Pi-hole extensions
Expand Down Expand Up @@ -163,7 +189,7 @@ int match_regex(const char *input, const DNSCacheData* dns_cache, const int clie
const enum regex_type regexid, const bool regextest)
{
int match_idx = -1;
regex_data *regex = get_regex_from_type(regexid);
regex_data *regex = get_regex_ptr(regexid);
#ifdef USE_TRE_REGEX
regmatch_t match = { 0 }; // This also disables any sub-matching
#endif
Expand Down Expand Up @@ -309,11 +335,20 @@ static void free_regex(void)

// Free regex datastructure
// Loop over regex types
for(unsigned char regexid = 0; regexid < REGEX_MAX; regexid++)
for(enum regex_type regexid = REGEX_BLACKLIST; regexid < REGEX_MAX; regexid++)
{
regex_data *regex = get_regex_from_type(regexid);
regex_data *regex = get_regex_ptr(regexid);

// Reset counter for number of regex
const unsigned int oldcount = counters->num_regex[regexid];
counters->num_regex[regexid] = 0;

// Exit early if the regex has already been freed (or has never been used)
if(regex == NULL)
continue;

// Loop over entries with this regex type
for(unsigned int index = 0; index < counters->num_regex[regexid]; index++)
for(unsigned int index = 0; index < oldcount; index++)
{
if(!regex[index].available)
continue;
Expand All @@ -329,14 +364,7 @@ static void free_regex(void)
}

// Free array with regex datastructure
if(regex != NULL)
{
free(regex);
regex = NULL;
}

// Reset counter for number of regex
counters->num_regex[regexid] = 0;
free_regex_ptr(regexid);
}
}

Expand Down