]> git.ipfire.org Git - thirdparty/git.git/commitdiff
http-backend.c: fix cmd_main() memory leak, refactor reg{exec,free}()
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Mon, 6 Feb 2023 23:07:45 +0000 (00:07 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 6 Feb 2023 23:34:38 +0000 (15:34 -0800)
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>
http-backend.c

index 67819d931ce5d50950fb54bf64f29d86c1ab00de..8ab58e55f8524851fa77e047e9b9a5dfd31eb6bd 100644 (file)
@@ -759,10 +759,14 @@ int cmd_main(int argc, const char **argv)
                struct service_cmd *c = &services[i];
                regex_t re;
                regmatch_t out[1];
+               int ret;
 
                if (regcomp(&re, c->pattern, REG_EXTENDED))
                        die("Bogus regex in service table: %s", c->pattern);
-               if (!regexec(&re, dir, 1, out, 0)) {
+               ret = regexec(&re, dir, 1, out, 0);
+               regfree(&re);
+
+               if (!ret) {
                        size_t n;
 
                        if (strcmp(method, c->method))
@@ -774,7 +778,6 @@ int cmd_main(int argc, const char **argv)
                        dir[out[0].rm_so] = 0;
                        break;
                }
-               regfree(&re);
        }
 
        if (!cmd)