Skip to content

Commit 021ba32

Browse files
peffgitster
authored andcommitted
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>
1 parent 9fadedd commit 021ba32

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

remote.c

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -174,54 +174,43 @@ static void add_merge(struct branch *branch, const char *name)
174174
branch->merge_name[branch->merge_nr++] = name;
175175
}
176176

177-
static struct branch *make_branch(const char *name, int len)
177+
static struct branch *make_branch(const char *name, size_t len)
178178
{
179179
struct branch *ret;
180180
int i;
181181

182182
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])
186185
return branches[i];
187186
}
188187

189188
ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
190189
ret = xcalloc(1, sizeof(struct branch));
191190
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);
196192
ret->refname = xstrfmt("refs/heads/%s", ret->name);
197193

198194
return ret;
199195
}
200196

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)
202199
{
203200
struct rewrite *ret;
204201
int i;
205202

206203
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))
211206
return r->rewrite[i];
212207
}
213208

214209
ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
215210
ret = xcalloc(1, sizeof(struct rewrite));
216211
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;
225214
return ret;
226215
}
227216

@@ -470,7 +459,7 @@ static void read_config(void)
470459
const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
471460
if (head_ref && (flag & REF_ISSYMREF) &&
472461
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));
474463
}
475464
}
476465
git_config(handle_config, NULL);
@@ -1584,7 +1573,7 @@ struct branch *branch_get(const char *name)
15841573
if (!name || !*name || !strcmp(name, "HEAD"))
15851574
ret = current_branch;
15861575
else
1587-
ret = make_branch(name, 0);
1576+
ret = make_branch(name, strlen(name));
15881577
set_merge(ret);
15891578
return ret;
15901579
}

0 commit comments

Comments
 (0)