Skip to content

Commit 2139bd0

Browse files
avargitster
authored andcommitted
http-backend.c: fix cmd_main() memory leak, refactor reg{exec,free}()
Fix a memory leak that's been with us ever since 2f4038a (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eef75d2 commit 2139bd0

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

http-backend.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,14 @@ int cmd_main(int argc, const char **argv)
759759
struct service_cmd *c = &services[i];
760760
regex_t re;
761761
regmatch_t out[1];
762+
int ret;
762763

763764
if (regcomp(&re, c->pattern, REG_EXTENDED))
764765
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) {
766770
size_t n;
767771

768772
if (strcmp(method, c->method))
@@ -774,7 +778,6 @@ int cmd_main(int argc, const char **argv)
774778
dir[out[0].rm_so] = 0;
775779
break;
776780
}
777-
regfree(&re);
778781
}
779782

780783
if (!cmd)

0 commit comments

Comments
 (0)