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

Re: [PATCH] cocci: drop bogus xstrdup_or_null() rule

From
Derrick Stolee <derrickstolee@github.com>
Date
May 2, 2022, 13:46 UTC
Message-ID
<4181f814-6d8e-ff2c-6fc9-a625ac189eae@github.com>
In-Reply-To
<xmqq1qxd6e4x.fsf@gitster.g>
On 5/1/2022 1:00 AM, Junio C Hamano wrote:
Show 16 quoted lines
> 13092a91 (cocci: refactor common patterns to use xstrdup_or_null(),
> 2016-10-12) introduced a rule to rewrite this conditional call to
> xstrdup(E) and an assignment to variable V:
> 
>     - if (E)
>     -    V = xstrdup(E);
> 
> into an unconditional call to xstrdup_or_null(E) and an assignment
> to variable V:
> 
>     + V = xstrdup_or_null(E);
> 
> which is utterly bogus.  The original code may already have an
> acceptable value in V and the conditional assignment may be to
> improve the value already in V with a copy of a better value E when
> (and only when) E is not NULL.
Yes, this makes sense.
 
Show 9 quoted lines
> The rewritten construct unconditionally discards the existing value
> of V and replaces it with a copy of E, even when E is NULL, which
> changes the meaning of the program.
> 
> By the way, if it were
> 
> 	-if (E && !V)
> 	-	V = xstrdup(E);
> 	+V = xstrdup_or_null(E);
I think you mean if it were
	-if (E && !V)
	-	V = xstrdup(E);
	+if (!V)
	+	V = xstrdup_or_null(E);
or
	-if (E && !V)
	-	V = xstrdup(E);
	+free(V);
	+V = xstrdup_or_null(E);

But yes, there is no preimage matching this pattern, so it doesn't matter.

Dropping the rule makes the most sense.

Thanks, -Stolee

Previous: Junio C HamanoNext: Junio C Hamano
Message 2 of 3 in “cocci: drop bogus xstrdup_or_null() rule”
  1. cocci: drop bogus xstrdup_or_null() ruleJunio C Hamano, May 1, 2022
  2. Derrick StoleeMay 2, 2022
  3. Junio C HamanoMay 2, 2022

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.