From 5c921b6c0b2df460c7d50f6563edf700d0420732 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 17 Oct 2025 09:51:50 +0200 Subject: [PATCH] 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. --- lib/open.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 (®exv[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 = ®exv[nregex]; rerr = regcomp (new_regex, str, REG_EXTENDED | REG_NOSUB); -- 2.45.2