Skip to content

Commit 268a9ca

Browse files
peffgitster
authored andcommitted
rev-parse: simplify dotdot parsing
The previous commit simplified the way that revision.c parses ".." and "..." range operators. But there's roughly similar code in rev-parse. This is less likely to trigger a segfault, as there is no library function which we'd pass a string literal to, but it still causes the compiler to complain about laundering away constness via strstr(). Let's give it the same treatment, copying the left-hand side of the range operator into its own string. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4d5fb93 commit 268a9ca

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

builtin/rev-parse.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,20 @@ static int show_file(const char *arg, int output_prefix)
267267

268268
static int try_difference(const char *arg)
269269
{
270-
char *dotdot;
270+
const char *dotdot;
271271
struct object_id start_oid;
272272
struct object_id end_oid;
273273
const char *end;
274274
const char *start;
275+
char *to_free;
275276
int symmetric;
276277
static const char head_by_default[] = "HEAD";
277278

278279
if (!(dotdot = strstr(arg, "..")))
279280
return 0;
281+
start = to_free = xmemdupz(arg, dotdot - arg);
280282
end = dotdot + 2;
281-
start = arg;
282283
symmetric = (*end == '.');
283-
284-
*dotdot = 0;
285284
end += symmetric;
286285

287286
if (!*end)
@@ -295,7 +294,7 @@ static int try_difference(const char *arg)
295294
* Just ".."? That is not a range but the
296295
* pathspec for the parent directory.
297296
*/
298-
*dotdot = '.';
297+
free(to_free);
299298
return 0;
300299
}
301300

@@ -308,7 +307,7 @@ static int try_difference(const char *arg)
308307
a = lookup_commit_reference(the_repository, &start_oid);
309308
b = lookup_commit_reference(the_repository, &end_oid);
310309
if (!a || !b) {
311-
*dotdot = '.';
310+
free(to_free);
312311
return 0;
313312
}
314313
if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0)
@@ -318,10 +317,10 @@ static int try_difference(const char *arg)
318317
show_rev(REVERSED, &commit->object.oid, NULL);
319318
}
320319
}
321-
*dotdot = '.';
320+
free(to_free);
322321
return 1;
323322
}
324-
*dotdot = '.';
323+
free(to_free);
325324
return 0;
326325
}
327326

0 commit comments

Comments
 (0)