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>

Jeff King committed Mar 26, 2026 at 15:05 UTC 268a9caaf29f0269147dacbea2c8d439c505c5ee
1 file changed +7 -8
builtin/rev-parse.c
+7 -8
@@ -267,21 +267,20 @@ static int show_file(const char *arg, int output_prefix)
267
268 static int try_difference(const char *arg)
269 {
270 - char *dotdot;
270 + const char *dotdot;
271 struct object_id start_oid;
272 struct object_id end_oid;
273 const char *end;
274 const char *start;
275 + char *to_free;
276 int symmetric;
277 static const char head_by_default[] = "HEAD";
278
279 if (!(dotdot = strstr(arg, "..")))
280 return 0;
281 + start = to_free = xmemdupz(arg, dotdot - arg);
282 end = dotdot + 2;
281 - start = arg;
283 symmetric = (*end == '.');
283 -
284 - *dotdot = 0;
284 end += symmetric;
285
286 if (!*end)
@@ -295,7 +294,7 @@ static int try_difference(const char *arg)
294 * Just ".."? That is not a range but the
295 * pathspec for the parent directory.
296 */
298 - *dotdot = '.';
297 + free(to_free);
298 return 0;
299 }
300
@@ -308,7 +307,7 @@ static int try_difference(const char *arg)
307 a = lookup_commit_reference(the_repository, &start_oid);
308 b = lookup_commit_reference(the_repository, &end_oid);
309 if (!a || !b) {
311 - *dotdot = '.';
310 + free(to_free);
311 return 0;
312 }
313 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0)
@@ -318,10 +317,10 @@ static int try_difference(const char *arg)
317 show_rev(REVERSED, &commit->object.oid, NULL);
318 }
319 }
321 - *dotdot = '.';
320 + free(to_free);
321 return 1;
322 }
324 - *dotdot = '.';
323 + free(to_free);
324 return 0;
325 }
326