http-backend.c: fix cmd_main() memory leak, refactor reg{exec,free}()

Fix a memory leak that's been with us ever since 2f4038ab337 (Git-aware CGI to provide dumb HTTP transport, 2009-10-30). In this case we're not calling regerror() after a failed regexec(), and don't otherwise use "re" afterwards. We can therefore simplify this code by calling regfree() right after the regexec(). An alternative fix would be to add a regfree() to both the "return" and "break" path in this for-loop. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 7, 2023 at 00:07 UTC 2139bd0200b2ee51674d8778cd26a9f38220a5b7
1 file changed +5 -2
http-backend.c
+5 -2
@@ -759,10 +759,14 @@ int cmd_main(int argc, const char **argv)
759 struct service_cmd *c = &services[i];
760 regex_t re;
761 regmatch_t out[1];
762 + int ret;
763
764 if (regcomp(&re, c->pattern, REG_EXTENDED))
765 die("Bogus regex in service table: %s", c->pattern);
765 - if (!regexec(&re, dir, 1, out, 0)) {
766 + ret = regexec(&re, dir, 1, out, 0);
767 + regfree(&re);
768 +
769 + if (!ret) {
770 size_t n;
771
772 if (strcmp(method, c->method))
@@ -774,7 +778,6 @@ int cmd_main(int argc, const char **argv)
778 dir[out[0].rm_so] = 0;
779 break;
780 }
777 - regfree(&re);
781 }
782
783 if (!cmd)