aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2025-10-17 09:51:50 +0200
committerDavid Bremner <david@tethera.net>2025-11-09 08:05:30 -0400
commit5c921b6c0b2df460c7d50f6563edf700d0420732 (patch)
tree6b6440a1b2113aa2314ceb948bca985da4d12043 /lib
parentc55953e9de408674d71db78ea4fff941972b2f0a (diff)
open: avoid leaking index_as_text regex objects
Regexes compiled with regcomp() need to be freed with regfree(). Do that in a talloc destructor attached to the compiled regex array. Amended by db: Use C style comment. Add blank line per uncrustify.
Diffstat (limited to 'lib')
-rw-r--r--lib/open.cc14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/open.cc b/lib/open.cc
index 463e38bf..b9e186ce 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -434,6 +434,16 @@ _load_database_state (notmuch_database_t *notmuch)
notmuch, notmuch->xapian_db->get_uuid ().c_str ());
}
+static int
+_index_as_text_free (regex_t *regexv)
+{
+ size_t num_regex = talloc_get_size (regexv) / sizeof (*regexv);
+
+ for (size_t i = 0; i < num_regex; i++)
+ regfree (&regexv[i]);
+ return 0;
+}
+
/* XXX This should really be done lazily, but the error reporting path in the indexing code
* would need to be redone to report any errors.
*/
@@ -460,6 +470,10 @@ _ensure_index_as_text (notmuch_database_t *notmuch, char **message)
assert (len > 0);
regexv = talloc_realloc (notmuch, regexv, regex_t, nregex + 1);
+ /* destructor applies to entire array, so only set it on first iteration */
+ if (nregex == 0)
+ talloc_set_destructor (regexv, _index_as_text_free);
+
new_regex = &regexv[nregex];
rerr = regcomp (new_regex, str, REG_EXTENDED | REG_NOSUB);