git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v1 2/2] list-objects-filter-options: avoid strbuf_split_str()

From
Jeff King <peff@peff.net>
Date
Mar 9, 2026, 19:08 UTC
Message-ID
<20260309190823.GB309867@coredump.intra.peff.net>
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
Previous: Jeff King
Message 8 of 8 in “avoid unnecessary strbuf_split*() and strbuf-by-value usage”
  1. 0/2 avoid unnecessary strbuf_split*() and strbuf-by-value usageDeveshi Dwivedi, Mar 8, 2026
  2. 1/2 worktree: do not pass strbuf by valueDeveshi Dwivedi, Mar 8, 2026
  3. Junio C HamanoMar 9, 2026
  4. coccinelle to catch pass-by-value?, was: [PATCH v1 1/2] worktree: do not pass strbuf by valueJeff King, Mar 9, 2026
  5. 2/2 list-objects-filter-options: avoid strbuf_split_str()Deveshi Dwivedi, Mar 8, 2026
  6. Junio C HamanoMar 9, 2026
  7. Jeff KingMar 9, 2026
  8. Jeff KingMar 9, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.