Skip to content

Commit c74a295

Browse files
committed
Use pcre_free_study() to free PCRE JIT data
Function msc_pcre_cleanup(), which is responsible for freeing compiled regex data, used either regular free() or pcre_free() (depending on compilation settings) to free pcre_extra structure (pointer to which is stored in regex->pe) created by pcre_study(). This was incorrect, structure returned by pcre_study() should be freed by function pcre_free_study(). In case PCRE JIT is used, pcre_study() makes some additional allocations itself (at least for JITed executable code), which function pcre_free_study() frees. If pcre_free_study() is not used a memory leak occurs because, while pcre_extra structure itself might be freed by regular free(), some additional data referenced by it is not. Fix that by calling pcre_free_study() (instead of free()/pcre_free()) on pointer returned by pcre_study(). Note that code creating msc_regex_t may allocate regex->pe with malloc() or pcre_malloc() instead of pcre_study(). This case is checked by testing if PCRE_EXTRA_EXECUTABLE_JIT flag on regex->pe->flags is set. msc_pregcomp_ex() does not set that flag itself (and it memsets the whole structure with zeros after allocation) and pcre_free_study() actually does the same (it tests for PCRE_EXTRA_EXECUTABLE_JIT flag, and, if that is zero, calls pcre_free() on passed pointer). Fixes #610
1 parent f732fc6 commit c74a295

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

apache2/msc_pcre.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
static apr_status_t msc_pcre_cleanup(msc_regex_t *regex) {
2222
if (regex != NULL) {
2323
if (regex->pe != NULL) {
24+
if (((pcre_extra *)regex->pe)->flags & PCRE_EXTRA_EXECUTABLE_JIT) {
25+
pcre_free_study(regex->pe);
26+
} else {
2427
#if defined(VERSION_NGINX)
25-
pcre_free(regex->pe);
28+
pcre_free(regex->pe);
2629
#else
27-
free(regex->pe);
30+
free(regex->pe);
2831
#endif
32+
}
2933
regex->pe = NULL;
3034
}
3135
if (regex->re != NULL) {

0 commit comments

Comments
 (0)