public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: Michael J Gruber <git@grubix.eu>,  git@vger.kernel.org
Subject: Re: [PATCH 1/4] revision: make handle_dotdot() interface less confusing
Date: Thu, 26 Mar 2026 12:28:15 -0700	[thread overview]
Message-ID: <xmqqikaipf00.fsf@gitster.g> (raw)
In-Reply-To: <20260326190444.GA415796@coredump.intra.peff.net> (Jeff King's message of "Thu, 26 Mar 2026 15:04:44 -0400")

Jeff King <peff@peff.net> writes:

> There are two very subtle bits to the way we parse ".." (and "...")
> range operators:
>
>  1. In handle_dotdot_1(), we assume that the incoming arguments "dotdot"
>     and "arg" are part of the same string, with the first digit of the

"digit" -> "dot".

>     range-operator blanked to a NUL. Then when we want the full name
>     (e.g., to report an error), we replace the NUL with a dot to restore
>     the original string.
>
>  2. In handle_dotdot(), we take in a const string, but then we modify it
>     by overwriting the range operator with a NUL. This has worked OK in
>     practice since we tend to pass in buffers that are actually
>     writeable (including argv), but segfaults with something like:
>
>       handle_revision_arg("..HEAD", &revs, 0, 0);
>
>     On top of that, building with recent versions of glibc causes the
>     compiler to complain, because it notices when we use strchr() or
>     strstr() to launder away constness (basically detecting the
>     possibility of the segfault above via the type system).
>
> Instead of munging the buffer, let's instead make a temporary copy of
> the left-hand side of the range operator. That avoids any const
> violations, and lets us pass around the parsed elements independently:
> the left-hand side, the right-hand side, the number of dots (via the
> "symmetric" flag), and the original full string for error messages.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---

OK.  I was hoping if we can do without a temporary allocation, but
the const-string "..HEAD" example does make it clear that it is not
something we can achieve easily.

And once we accept that it is inevitable to make a copy, everything
else falls into the right place.

> diff --git a/revision.c b/revision.c
> index 31808e3df0..f61262436f 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -2038,41 +2038,32 @@ static void prepare_show_merge(struct rev_info *revs)
>  	free(prune);
>  }
>  
> -static int dotdot_missing(const char *arg, char *dotdot,
> +static int dotdot_missing(const char *full_name,
>  			  struct rev_info *revs, int symmetric)
>  {
>  	if (revs->ignore_missing)
>  		return 0;
> -	/* de-munge so we report the full argument */
> -	*dotdot = '.';
>  	die(symmetric
>  	    ? "Invalid symmetric difference expression %s"
> -	    : "Invalid revision range %s", arg);
> +	    : "Invalid revision range %s", full_name);
>  }
>  
> -static int handle_dotdot_1(const char *arg, char *dotdot,
> +static int handle_dotdot_1(const char *a_name, const char *b_name,
> +			   const char *full_name, int symmetric,
>  			   struct rev_info *revs, int flags,
>  			   int cant_be_filename,
>  			   struct object_context *a_oc,
>  			   struct object_context *b_oc)
>  {
> -	const char *a_name, *b_name;
>  	struct object_id a_oid, b_oid;
>  	struct object *a_obj, *b_obj;
>  	unsigned int a_flags, b_flags;
> -	int symmetric = 0;
>  	unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
>  	unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
>  
> -	a_name = arg;
>  	if (!*a_name)
>  		a_name = "HEAD";
>  
> -	b_name = dotdot + 2;
> -	if (*b_name == '.') {
> -		symmetric = 1;
> -		b_name++;
> -	}
>  	if (!*b_name)
>  		b_name = "HEAD";
>  
> @@ -2081,15 +2072,13 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
>  		return -1;
>  
>  	if (!cant_be_filename) {
> -		*dotdot = '.';
> -		verify_non_filename(revs->prefix, arg);
> -		*dotdot = '\0';
> +		verify_non_filename(revs->prefix, full_name);
>  	}
>  
>  	a_obj = parse_object(revs->repo, &a_oid);
>  	b_obj = parse_object(revs->repo, &b_oid);
>  	if (!a_obj || !b_obj)
> -		return dotdot_missing(arg, dotdot, revs, symmetric);
> +		return dotdot_missing(full_name, revs, symmetric);
>  
>  	if (!symmetric) {
>  		/* just A..B */
> @@ -2103,7 +2092,7 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
>  		a = lookup_commit_reference(revs->repo, &a_obj->oid);
>  		b = lookup_commit_reference(revs->repo, &b_obj->oid);
>  		if (!a || !b)
> -			return dotdot_missing(arg, dotdot, revs, symmetric);
> +			return dotdot_missing(full_name, revs, symmetric);
>  
>  		if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
>  			commit_list_free(exclude);
> @@ -2132,16 +2121,23 @@ static int handle_dotdot(const char *arg,
>  			 int cant_be_filename)
>  {
>  	struct object_context a_oc = {0}, b_oc = {0};
> -	char *dotdot = strstr(arg, "..");
> +	const char *dotdot = strstr(arg, "..");
> +	char *tmp;
> +	int symmetric = 0;
>  	int ret;
>  
>  	if (!dotdot)
>  		return -1;
>  
> -	*dotdot = '\0';
> -	ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
> -			      &a_oc, &b_oc);
> -	*dotdot = '.';
> +	tmp = xmemdupz(arg, dotdot - arg);
> +	dotdot += 2;
> +	if (*dotdot == '.') {
> +		symmetric = 1;
> +		dotdot++;
> +	}
> +	ret = handle_dotdot_1(tmp, dotdot, arg, symmetric, revs, flags,
> +			      cant_be_filename, &a_oc, &b_oc);
> +	free(tmp);
>  
>  	object_context_release(&a_oc);
>  	object_context_release(&b_oc);

  reply	other threads:[~2026-03-26 19:28 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-26 15:22 [PATCH 0/6] ISOC23: quell warnings on discarding const Michael J Gruber
2026-03-26 15:22 ` [PATCH 1/6] do not discard const: the simple cases Michael J Gruber
2026-03-26 17:34   ` Jeff King
2026-03-26 17:45     ` Junio C Hamano
2026-03-26 19:23       ` [PATCH] config: store allocated string in non-const pointer Jeff King
2026-03-26 15:22 ` [PATCH 2/6] do not discard const: make git-compat-util ISOC23-like Michael J Gruber
2026-03-26 15:22 ` [PATCH 3/6] do not discard const: adjust to non-const data types Michael J Gruber
2026-03-26 17:28   ` Junio C Hamano
2026-03-26 15:22 ` [PATCH 4/6] do not discard const: declare const where we stay const Michael J Gruber
2026-03-26 15:22 ` [PATCH 5/6] do not discard const: keep signature Michael J Gruber
2026-03-26 17:18   ` Junio C Hamano
2026-03-26 15:22 ` [PATCH 6/6] do not discard const: the ugly truth Michael J Gruber
2026-03-26 17:07   ` Junio C Hamano
2026-03-26 17:42   ` Jeff King
2026-03-26 19:02     ` [PATCH 0/4] fix const issues in revision parser Jeff King
2026-03-26 19:04       ` [PATCH 1/4] revision: make handle_dotdot() interface less confusing Jeff King
2026-03-26 19:28         ` Junio C Hamano [this message]
2026-03-26 23:14           ` Jeff King
2026-03-27 15:55             ` Junio C Hamano
2026-03-26 19:05       ` [PATCH 2/4] rev-parse: simplify dotdot parsing Jeff King
2026-03-26 19:13       ` [PATCH 3/4] revision: avoid writing to const string for parent marks Jeff King
2026-03-26 19:14       ` [PATCH 4/4] rev-parse: " Jeff King
2026-03-26 16:26 ` [PATCH 0/6] ISOC23: quell warnings on discarding const D. Ben Knoble
2026-03-27 17:45   ` Michael J Gruber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqikaipf00.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@grubix.eu \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox