plug memory leaks in got-fetch-pack and got-send-pack ok naddy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
diff --git a/lib/gitproto.c b/lib/gitproto.c
index e84621d..dec2486 100644
--- a/lib/gitproto.c
+++ b/lib/gitproto.c
@@ -84,6 +84,10 @@ got_gitproto_parse_refline(char **id_str, char **refname,
const struct got_error *err = NULL;
char *tokens[3];
+ *id_str = NULL;
+ *refname = NULL;
+ /* don't reset *server_capabilities */
+
err = tokenize_refline(tokens, line, len, nitems(tokens));
if (err)
return err;
@@ -93,11 +97,14 @@ got_gitproto_parse_refline(char **id_str, char **refname,
if (tokens[1])
*refname = tokens[1];
if (tokens[2]) {
- char *p;
- *server_capabilities = tokens[2];
- p = strrchr(*server_capabilities, '\n');
- if (p)
- *p = '\0';
+ if (*server_capabilities == NULL) {
+ char *p;
+ *server_capabilities = tokens[2];
+ p = strrchr(*server_capabilities, '\n');
+ if (p)
+ *p = '\0';
+ } else
+ free(tokens[2]);
}
return NULL;
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index 171700f..b85f024 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -346,6 +346,8 @@ fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
err = fetch_error(&buf[4], n - 4);
goto done;
}
+ free(id_str);
+ free(refname);
err = got_gitproto_parse_refline(&id_str, &refname,
&server_capabilities, buf, n);
if (err)
diff --git a/libexec/got-send-pack/got-send-pack.c b/libexec/got-send-pack/got-send-pack.c
index eb8f383..bec42fb 100644
--- a/libexec/got-send-pack/got-send-pack.c
+++ b/libexec/got-send-pack/got-send-pack.c
@@ -353,6 +353,8 @@ send_pack(int fd, struct got_pathlist_head *refs,
err = send_error(&buf[4], n - 4);
goto done;
}
+ free(id_str);
+ free(refname);
err = got_gitproto_parse_refline(&id_str, &refname,
&server_capabilities, buf, n);
if (err)