git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] cocci: drop bogus xstrdup_or_null() rule
@ 2022-05-01  5:00 Junio C Hamano
  2022-05-02 13:46 ` Derrick Stolee
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2022-05-01  5:00 UTC (permalink / raw)
  To: git

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.

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);

it would probably have been correct.  But there is no existing code
that would have been improved by such a rule, so let's just remove
the bogus one without replacing with the more specific one.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 contrib/coccinelle/xstrdup_or_null.cocci | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/contrib/coccinelle/xstrdup_or_null.cocci b/contrib/coccinelle/xstrdup_or_null.cocci
index 8e05d1ca4b..9c1d2939b6 100644
--- a/contrib/coccinelle/xstrdup_or_null.cocci
+++ b/contrib/coccinelle/xstrdup_or_null.cocci
@@ -1,11 +1,3 @@
-@@
-expression E;
-expression V;
-@@
-- if (E)
--    V = xstrdup(E);
-+ V = xstrdup_or_null(E);
-
 @@
 expression E;
 @@
-- 
2.36.0-233-gea8f06b421


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] cocci: drop bogus xstrdup_or_null() rule
  2022-05-01  5:00 [PATCH] cocci: drop bogus xstrdup_or_null() rule Junio C Hamano
@ 2022-05-02 13:46 ` Derrick Stolee
  2022-05-02 17:07   ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Derrick Stolee @ 2022-05-02 13:46 UTC (permalink / raw)
  To: Junio C Hamano, git

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] cocci: drop bogus xstrdup_or_null() rule
  2022-05-02 13:46 ` Derrick Stolee
@ 2022-05-02 17:07   ` Junio C Hamano
  0 siblings, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2022-05-02 17:07 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: git

Derrick Stolee <derrickstolee@github.com> writes:

> I think you mean if it were
> ...

Please disregard.  That one was completely bogus.

> Dropping the rule makes the most sense.

Yup.  ThHanks for a doze of sanity.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-05-02 17:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-01  5:00 [PATCH] cocci: drop bogus xstrdup_or_null() rule Junio C Hamano
2022-05-02 13:46 ` Derrick Stolee
2022-05-02 17:07   ` Junio C Hamano

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).