Commit 5859417
committed
Use right PCRE functions to alloc/free pcre_extra
Allocate pcre_extra structure with pcre_study or pcre_malloc
and free it with pcre_free_study(). Pass flag
PCRE_STUDY_EXTRA_NEEDED to pcre_study telling it to always
allocate pcre_extra because this structure is needed anyway to
configure match limits.
Until this change, pcre_extra was allocated with eigther
pcre_malloc or regular malloc (depending on whether VERSION_NGINX
is defined); function msc_pcre_cleanup(), which is responsible
for freeing compiled regex data, used either regular free() or
pcre_free() (depending VERSION_NGINX too) to free pcre_extra
structure (pointer to which is stored in regex->pe).
Freeing it like 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, 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().
There also seems to be no reason to allocate pcre_extra with
regular malloc (and de-allocate it with free()) - there is a
function pcre_malloc(), which is a function pcre_study() itself
would use to allocate that memory, and, in default case,
pcre_malloc and pcre_free will be set to regular malloc and free.
Usage of malloc() seems to be a remaining of old code where
manual allocation of pcre_extra was always done with malloc().
So, remove "#if defined(VERSION_NGINX)" branches and always use
pcre_malloc() for pcre_extra allocation in case pcre_study did not
allocate it yet and always free is with pcre_free_study() (btw.
'pcreapi' man page recommends to replace pcre_free() usages to
deallocate pcre_extra with pcre_free_study()).
Fixes #6101 parent f732fc6 commit 5859417
1 file changed
+3
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 24 | + | |
29 | 25 | | |
30 | 26 | | |
31 | 27 | | |
| |||
67 | 63 | | |
68 | 64 | | |
69 | 65 | | |
70 | | - | |
| 66 | + | |
71 | 67 | | |
72 | | - | |
| 68 | + | |
73 | 69 | | |
74 | 70 | | |
75 | 71 | | |
76 | 72 | | |
77 | 73 | | |
78 | | - | |
79 | 74 | | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | 75 | | |
84 | 76 | | |
85 | 77 | | |
| |||
0 commit comments