Don't leak compiled regex(es) when an ispell cache entry is dropped.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 19 Mar 2021 01:44:43 +0000 (21:44 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 19 Mar 2021 01:44:43 +0000 (21:44 -0400)
commiteba939551afe5709d15fe7779e7771ae0c8bf830
treed981b909a5025278aa1edfc20e8f70b84925cdc3
parentea3989f3496c9c6a0c392680842518997d317e38
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
src/backend/tsearch/spell.c
src/include/tsearch/dicts/spell.h