use more readable names for items parsed from refline, and plug related leaks
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index 9450b19..30b7714 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -214,12 +214,11 @@ match_branch(char *br, char *pat)
}
static const struct got_error *
-tokenize_refline(char **tokens, char *line, int len)
+tokenize_refline(char **tokens, char *line, int len, int maxtokens)
{
const struct got_error *err = NULL;
char *p;
size_t i, n = 0;
- const int maxtokens = 3;
for (i = 0; i < maxtokens; i++)
tokens[i] = NULL;
@@ -258,6 +257,27 @@ done:
return err;
}
+static const struct got_error *
+parse_refline(char **id_str, char **refname, char **server_capabilities,
+ char *line, int len)
+{
+ const struct got_error *err = NULL;
+ char *tokens[3];
+
+ err = tokenize_refline(tokens, line, len, nitems(tokens));
+ if (err)
+ return err;
+
+ if (tokens[0])
+ *id_str = tokens[0];
+ if (tokens[1])
+ *refname = tokens[1];
+ if (tokens[2])
+ *server_capabilities = tokens[2];
+
+ return NULL;
+}
+
struct got_capability {
const char *key;
const char *value;
@@ -370,13 +390,14 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
struct got_pathlist_head *have_refs, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
- char buf[GOT_PKTMAX], *sp[3];
+ char buf[GOT_PKTMAX];
char hashstr[SHA1_DIGEST_STRING_LENGTH];
struct got_object_id *have, *want;
int is_firstpkt = 1, nref = 0, refsz = 16;
int i, n, req;
off_t packsz;
- char *my_capabilities = NULL;
+ char *id_str = NULL, *refname = NULL;
+ char *server_capabilities = NULL, *my_capabilities = NULL;
struct got_pathlist_head symrefs;
struct got_pathlist_entry *pe;
@@ -413,14 +434,16 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
err = got_error_msg(GOT_ERR_FETCH_FAILED, msg);
goto done;
}
- err = tokenize_refline(sp, buf, n);
+ err = parse_refline(&id_str, &refname, &server_capabilities,
+ buf, n);
if (err)
goto done;
- if (chattygit && sp[2][0] != '\0')
- fprintf(stderr, "server capabilities: %s\n", sp[2]);
+ if (chattygit && server_capabilities[0] != '\0')
+ fprintf(stderr, "server capabilities: %s\n",
+ server_capabilities);
if (is_firstpkt) {
err = match_capabilities(&my_capabilities, &symrefs,
- sp[2]);
+ server_capabilities);
if (err)
goto done;
if (chattygit && my_capabilities)
@@ -431,9 +454,9 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
goto done;
}
is_firstpkt = 0;
- if (strstr(sp[1], "^{}"))
+ if (strstr(refname, "^{}"))
continue;
- if (fetchbranch && !match_branch(sp[1], fetchbranch))
+ if (fetchbranch && !match_branch(refname, fetchbranch))
continue;
if (refsz == nref + 1) {
refsz *= 2;
@@ -448,20 +471,21 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
goto done;
}
}
- if (!got_parse_sha1_digest(want[nref].sha1, sp[0])) {
+ if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
goto done;
}
- err = match_remote_ref(have_refs, &have[nref], sp[0], sp[1]);
+ err = match_remote_ref(have_refs, &have[nref], id_str, refname);
if (err)
goto done;
- err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
+ err = got_privsep_send_fetch_progress(ibuf, &want[nref],
+ refname);
if (err)
goto done;
if (chattygit)
- fprintf(stderr, "remote %s\n", sp[1]);
+ fprintf(stderr, "remote %s\n", refname);
nref++;
}
@@ -553,6 +577,9 @@ done:
got_pathlist_free(&symrefs);
free(have);
free(want);
+ free(id_str);
+ free(refname);
+ free(server_capabilities);
return err;
}