public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Jeff King <peff@peff•net>
To: Deveshi Dwivedi <deveshigurgaon@gmail•com>
Cc: git@vger•kernel.org, gitster@pobox•com
Subject: Re: [PATCH v1 2/2] list-objects-filter-options: avoid strbuf_split_str()
Date: Mon, 9 Mar 2026 15:08:23 -0400	[thread overview]
Message-ID: <20260309190823.GB309867@coredump.intra.peff.net> (raw)
In-Reply-To: <20260308180359.31188-3-deveshigurgaon@gmail.com>

On Sun, Mar 08, 2026 at 06:03:59PM +0000, Deveshi Dwivedi wrote:

> +	while (*p && !result) {
> +		const char *sep = strchr(p, '+');
> +		size_t len = sep ? (size_t)(sep - p + 1) : strlen(p);
> +		char *sub = xmemdupz(p, len);

The cast to size_t made me look twice to see if something tricky was
going on. We don't usually bother explicitly casting from a ptrdiff_t
into a size_t.

However, might this all be simpler with strchrnul? Something like:

  const char *end = strchrnul(p, '+');
  char *sub = xmemdupz(p, end - p);

  ...parse sub...

  if (!*end)
	break; /* found NUL at end of string */
  p = end + 1;

Notice I cut off the "+" when we find it, because I think...

> +		/* strip '+' separator, but only when more sub-specs follow */
> +		if (sep && *(sep + 1))
> +			sub[len - 1] = '\0';

...this is wrong. I know you are matching what the current code does,
but it does not match the documentation, and does not actually make any
sense in practice.


Other than that, this looks nice, and I am happy to see more
strbuf_split() calls going away.

I think you could in theory drop the xmemdupz() here, too, and feed the
ptr/len combo into parse_combine_subfilter(), which then percent-decodes
into a newly allocated buffer. But it is probably not worth trying to
squeeze out one extra allocation here. It is not like people have huge
lists of combined filters; we'd expect to see a couple at most.

-Peff

      parent reply	other threads:[~2026-03-09 19:08 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-08 18:03 [PATCH v1 0/2] avoid unnecessary strbuf_split*() and strbuf-by-value usage Deveshi Dwivedi
2026-03-08 18:03 ` [PATCH v1 1/2] worktree: do not pass strbuf by value Deveshi Dwivedi
2026-03-09 14:48   ` Junio C Hamano
2026-03-09 19:26   ` coccinelle to catch pass-by-value?, was: " Jeff King
2026-03-08 18:03 ` [PATCH v1 2/2] list-objects-filter-options: avoid strbuf_split_str() Deveshi Dwivedi
2026-03-09 15:38   ` Junio C Hamano
2026-03-09 19:01     ` Jeff King
2026-03-09 19:08   ` Jeff King [this message]

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=20260309190823.GB309867@coredump.intra.peff.net \
    --to=peff@peff$(echo .)net \
    --cc=deveshigurgaon@gmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=gitster@pobox$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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