]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rev-parse: simplify dotdot parsing
authorJeff King <peff@peff.net>
Thu, 26 Mar 2026 19:05:25 +0000 (15:05 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 26 Mar 2026 19:47:17 +0000 (12:47 -0700)
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>
builtin/rev-parse.c

index 01a62800e879380defdab9d0e16684adc5b387a1..5da95371134027c98890847fb29a5306058643eb 100644 (file)
@@ -267,21 +267,20 @@ static int show_file(const char *arg, int output_prefix)
 
 static int try_difference(const char *arg)
 {
-       char *dotdot;
+       const char *dotdot;
        struct object_id start_oid;
        struct object_id end_oid;
        const char *end;
        const char *start;
+       char *to_free;
        int symmetric;
        static const char head_by_default[] = "HEAD";
 
        if (!(dotdot = strstr(arg, "..")))
                return 0;
+       start = to_free = xmemdupz(arg, dotdot - arg);
        end = dotdot + 2;
-       start = arg;
        symmetric = (*end == '.');
-
-       *dotdot = 0;
        end += symmetric;
 
        if (!*end)
@@ -295,7 +294,7 @@ static int try_difference(const char *arg)
                 * Just ".."?  That is not a range but the
                 * pathspec for the parent directory.
                 */
-               *dotdot = '.';
+               free(to_free);
                return 0;
        }
 
@@ -308,7 +307,7 @@ static int try_difference(const char *arg)
                        a = lookup_commit_reference(the_repository, &start_oid);
                        b = lookup_commit_reference(the_repository, &end_oid);
                        if (!a || !b) {
-                               *dotdot = '.';
+                               free(to_free);
                                return 0;
                        }
                        if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0)
@@ -318,10 +317,10 @@ static int try_difference(const char *arg)
                                show_rev(REVERSED, &commit->object.oid, NULL);
                        }
                }
-               *dotdot = '.';
+               free(to_free);
                return 1;
        }
-       *dotdot = '.';
+       free(to_free);
        return 0;
 }