mirror of
https://github.com/postgres/postgres.git
synced 2025-07-26 01:22:12 +03:00
Don't leak compiled regex(es) when an ispell cache entry is dropped.
The text search cache mechanisms assume that we can clean up an invalidated dictionary cache entry simply by resetting the associated long-lived memory context. However, that does not work for ispell affixes that make use of regular expressions, because the regex library deals in plain old malloc. Hence, we leaked compiled regex(es) any time we dropped such a cache entry. That could quickly add up, since even a fairly trivial regex can use up tens of kB, and a large one can eat megabytes. Add a memory context callback to ensure that a regex gets freed when its owning cache entry is cleared. Found via valgrind testing. This problem is ancient, so back-patch to all supported branches. Discussion: https://postgr.es/m/3816764.1616104288@sss.pgh.pa.us
This commit is contained in:
@ -81,6 +81,17 @@ typedef struct spell_struct
|
||||
|
||||
#define SPELLHDRSZ (offsetof(SPELL, word))
|
||||
|
||||
/*
|
||||
* If an affix uses a regex, we have to store that separately in a struct
|
||||
* that won't move around when arrays of affixes are enlarged or sorted.
|
||||
* This is so that it can be found to be cleaned up at context destruction.
|
||||
*/
|
||||
typedef struct aff_regex_struct
|
||||
{
|
||||
regex_t regex;
|
||||
MemoryContextCallback mcallback;
|
||||
} aff_regex_struct;
|
||||
|
||||
/*
|
||||
* Represents an entry in an affix list.
|
||||
*/
|
||||
@ -97,7 +108,7 @@ typedef struct aff_struct
|
||||
char *repl;
|
||||
union
|
||||
{
|
||||
regex_t regex;
|
||||
aff_regex_struct *pregex;
|
||||
Regis regis;
|
||||
} reg;
|
||||
} AFFIX;
|
||||
|
Reference in New Issue
Block a user