git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Derrick Stolee <derrickstolee@github.com>
To: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: Re: [PATCH] cocci: drop bogus xstrdup_or_null() rule
Date: Mon, 2 May 2022 09:46:11 -0400	[thread overview]
Message-ID: <4181f814-6d8e-ff2c-6fc9-a625ac189eae@github.com> (raw)
In-Reply-To: <xmqq1qxd6e4x.fsf@gitster.g>

On 5/1/2022 1:00 AM, Junio C Hamano wrote:
> 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.
 
> 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

  reply	other threads:[~2022-05-02 13:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-01  5:00 [PATCH] cocci: drop bogus xstrdup_or_null() rule Junio C Hamano
2022-05-02 13:46 ` Derrick Stolee [this message]
2022-05-02 17:07   ` Junio C Hamano

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=4181f814-6d8e-ff2c-6fc9-a625ac189eae@github.com \
    --to=derrickstolee@github.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).