Skip to content

Commit 213b213

Browse files
peffgitster
authored andcommitted
rev-parse: avoid writing to const string for parent marks
The previous commit cleared up some const confusion in handling parent marks in revision.c, but we have roughly the same code duplicated in rev-parse. This one is much easier to fix, because the handling of the shortened string is all done in one place, after detecting any marks (but without shortening the string between marks). As a side note, I suspect this means that it behaves differently than the revision.c parser for weird stuff like "foo^!^@^-", but that is outside the scope of this patch. While we are here, let's also rename the variable "dotdot", which is totally misleading (and which we already fixed in revision.c long ago via f632ded (handle_revision_arg: stop using "dotdot" as a generic pointer, 2017-05-19)). Doing that here makes the diff a little messier, but it also lets the compiler help us make sure we did not miss any stray mentions of the variable while we are changing its semantics. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 22b985e commit 213b213

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

builtin/rev-parse.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,46 +326,47 @@ static int try_difference(const char *arg)
326326

327327
static int try_parent_shorthands(const char *arg)
328328
{
329-
char *dotdot;
329+
const char *mark;
330330
struct object_id oid;
331331
struct commit *commit;
332332
struct commit_list *parents;
333333
int parent_number;
334334
int include_rev = 0;
335335
int include_parents = 0;
336336
int exclude_parent = 0;
337+
char *to_free;
337338

338-
if ((dotdot = strstr(arg, "^!"))) {
339+
if ((mark = strstr(arg, "^!"))) {
339340
include_rev = 1;
340-
if (dotdot[2])
341+
if (mark[2])
341342
return 0;
342-
} else if ((dotdot = strstr(arg, "^@"))) {
343+
} else if ((mark = strstr(arg, "^@"))) {
343344
include_parents = 1;
344-
if (dotdot[2])
345+
if (mark[2])
345346
return 0;
346-
} else if ((dotdot = strstr(arg, "^-"))) {
347+
} else if ((mark = strstr(arg, "^-"))) {
347348
include_rev = 1;
348349
exclude_parent = 1;
349350

350-
if (dotdot[2]) {
351+
if (mark[2]) {
351352
char *end;
352-
exclude_parent = strtoul(dotdot + 2, &end, 10);
353+
exclude_parent = strtoul(mark + 2, &end, 10);
353354
if (*end != '\0' || !exclude_parent)
354355
return 0;
355356
}
356357
} else
357358
return 0;
358359

359-
*dotdot = 0;
360+
arg = to_free = xmemdupz(arg, mark - arg);
360361
if (repo_get_oid_committish(the_repository, arg, &oid) ||
361362
!(commit = lookup_commit_reference(the_repository, &oid))) {
362-
*dotdot = '^';
363+
free(to_free);
363364
return 0;
364365
}
365366

366367
if (exclude_parent &&
367368
exclude_parent > commit_list_count(commit->parents)) {
368-
*dotdot = '^';
369+
free(to_free);
369370
return 0;
370371
}
371372

@@ -386,7 +387,7 @@ static int try_parent_shorthands(const char *arg)
386387
free(name);
387388
}
388389

389-
*dotdot = '^';
390+
free(to_free);
390391
return 1;
391392
}
392393

0 commit comments

Comments
 (0)