@matheusbsbs / matheus-bagliano / commits / 021ba32a7b
Fork 0

remote: drop auto-strlen behavior of make_branch() and make_rewrite()

The make_branch() and make_rewrite() functions can take a NUL-terminated string or a ptr/len pair. They use a sentinel value of "0" for the len to tell the difference between the two. However, when parsing config like: [branch ""] merge = whatever whose key flattens to: branch..merge we might actually have a zero-length branch name. This is obviously nonsense, but the current code would consider it as a NUL-terminated string and use the branch name ".merge". We could use a better sentinel value here (like "-1"), but that gets in the way of moving to size_t, which is a more appropriate type for a ptr/len combo. Let's instead just drop this feature and have the callers (of which there are only two total) use strlen() themselves. This simplifies the code, and lets us move to using size_t. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 10, 2020 at 15:43 UTC 021ba32a7bca954235e31338c4f27b221a1807de
1 file changed +12 -23
remote.c
+12 -23
@@ -174,54 +174,43 @@ static void add_merge(struct branch *branch, const char *name)
174 branch->merge_name[branch->merge_nr++] = name;
175 }
176
177 -static struct branch *make_branch(const char *name, int len)
177 +static struct branch *make_branch(const char *name, size_t len)
178 {
179 struct branch *ret;
180 int i;
181
182 for (i = 0; i < branches_nr; i++) {
183 - if (len ? (!strncmp(name, branches[i]->name, len) &&
184 - !branches[i]->name[len]) :
185 - !strcmp(name, branches[i]->name))
183 + if (!strncmp(name, branches[i]->name, len) &&
184 + !branches[i]->name[len])
185 return branches[i];
186 }
187
188 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
189 ret = xcalloc(1, sizeof(struct branch));
190 branches[branches_nr++] = ret;
192 - if (len)
193 - ret->name = xstrndup(name, len);
194 - else
195 - ret->name = xstrdup(name);
191 + ret->name = xstrndup(name, len);
192 ret->refname = xstrfmt("refs/heads/%s", ret->name);
193
194 return ret;
195 }
196
201 -static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
197 +static struct rewrite *make_rewrite(struct rewrites *r,
198 + const char *base, size_t len)
199 {
200 struct rewrite *ret;
201 int i;
202
203 for (i = 0; i < r->rewrite_nr; i++) {
207 - if (len
208 - ? (len == r->rewrite[i]->baselen &&
209 - !strncmp(base, r->rewrite[i]->base, len))
210 - : !strcmp(base, r->rewrite[i]->base))
204 + if (len == r->rewrite[i]->baselen &&
205 + !strncmp(base, r->rewrite[i]->base, len))
206 return r->rewrite[i];
207 }
208
209 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
210 ret = xcalloc(1, sizeof(struct rewrite));
211 r->rewrite[r->rewrite_nr++] = ret;
217 - if (len) {
218 - ret->base = xstrndup(base, len);
219 - ret->baselen = len;
220 - }
221 - else {
222 - ret->base = xstrdup(base);
223 - ret->baselen = strlen(base);
224 - }
212 + ret->base = xstrndup(base, len);
213 + ret->baselen = len;
214 return ret;
215 }
216
@@ -470,7 +459,7 @@ static void read_config(void)
459 const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
460 if (head_ref && (flag & REF_ISSYMREF) &&
461 skip_prefix(head_ref, "refs/heads/", &head_ref)) {
473 - current_branch = make_branch(head_ref, 0);
462 + current_branch = make_branch(head_ref, strlen(head_ref));
463 }
464 }
465 git_config(handle_config, NULL);
@@ -1584,7 +1573,7 @@ struct branch *branch_get(const char *name)
1573 if (!name || !*name || !strcmp(name, "HEAD"))
1574 ret = current_branch;
1575 else
1587 - ret = make_branch(name, 0);
1576 + ret = make_branch(name, strlen(name));
1577 set_merge(ret);
1578 return ret;
1579 }