{"thread":{"id":"57826","subject":"[PATCH] cocci: drop bogus xstrdup_or_null() rule","startedAt":"2022-05-01T05:00:47Z","lastAt":"2022-05-02T17:07:41Z","messageCount":3,"participants":["Junio C Hamano","Derrick Stolee"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"454694","messageId":"xmqq1qxd6e4x.fsf@gitster.g","threadId":"57826","inReplyTo":null,"subject":"[PATCH] cocci: drop bogus xstrdup_or_null() rule","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-05-01T05:00:30Z","receivedAt":"2022-05-01T05:00:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"13092a91 (cocci: refactor common patterns to use xstrdup_or_null(),\n2016-10-12) introduced a rule to rewrite this conditional call to\nxstrdup(E) and an assignment to variable V:\n\n    - if (E)\n    -    V = xstrdup(E);\n\ninto an unconditional call to xstrdup_or_null(E) and an assignment\nto variable V:\n\n    + V = xstrdup_or_null(E);\n\nwhich is utterly bogus.  The original code may already have an\nacceptable value in V and the conditional assignment may be to\nimprove the value already in V with a copy of a better value E when\n(and only when) E is not NULL.\n\nThe rewritten construct unconditionally discards the existing value\nof V and replaces it with a copy of E, even when E is NULL, which\nchanges the meaning of the program.\n\nBy the way, if it were\n\n\t-if (E && !V)\n\t-\tV = xstrdup(E);\n\t+V = xstrdup_or_null(E);\n\nit would probably have been correct.  But there is no existing code\nthat would have been improved by such a rule, so let's just remove\nthe bogus one without replacing with the more specific one.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n contrib/coccinelle/xstrdup_or_null.cocci | 8 --------\n 1 file changed, 8 deletions(-)\n\ndiff --git a/contrib/coccinelle/xstrdup_or_null.cocci b/contrib/coccinelle/xstrdup_or_null.cocci\nindex 8e05d1ca4b..9c1d2939b6 100644\n--- a/contrib/coccinelle/xstrdup_or_null.cocci\n+++ b/contrib/coccinelle/xstrdup_or_null.cocci\n@@ -1,11 +1,3 @@\n-@@\n-expression E;\n-expression V;\n-@@\n-- if (E)\n--    V = xstrdup(E);\n-+ V = xstrdup_or_null(E);\n-\n @@\n expression E;\n @@\n-- \n2.36.0-233-gea8f06b421\n\n"},{"id":"454715","messageId":"4181f814-6d8e-ff2c-6fc9-a625ac189eae@github.com","threadId":"57826","inReplyTo":"xmqq1qxd6e4x.fsf@gitster.g","subject":"Re: [PATCH] cocci: drop bogus xstrdup_or_null() rule","fromName":"Derrick Stolee","fromEmail":"derrickstolee@github.com","sentAt":"2022-05-02T13:46:11Z","receivedAt":"2022-05-02T13:46:16Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 5/1/2022 1:00 AM, Junio C Hamano wrote:\n> 13092a91 (cocci: refactor common patterns to use xstrdup_or_null(),\n> 2016-10-12) introduced a rule to rewrite this conditional call to\n> xstrdup(E) and an assignment to variable V:\n> \n>     - if (E)\n>     -    V = xstrdup(E);\n> \n> into an unconditional call to xstrdup_or_null(E) and an assignment\n> to variable V:\n> \n>     + V = xstrdup_or_null(E);\n> \n> which is utterly bogus.  The original code may already have an\n> acceptable value in V and the conditional assignment may be to\n> improve the value already in V with a copy of a better value E when\n> (and only when) E is not NULL.\n\nYes, this makes sense.\n \n> The rewritten construct unconditionally discards the existing value\n> of V and replaces it with a copy of E, even when E is NULL, which\n> changes the meaning of the program.\n> \n> By the way, if it were\n> \n> \t-if (E && !V)\n> \t-\tV = xstrdup(E);\n> \t+V = xstrdup_or_null(E);\n\nI think you mean if it were\n\n\t-if (E && !V)\n\t-\tV = xstrdup(E);\n\t+if (!V)\n\t+\tV = xstrdup_or_null(E);\n\nor\n\n\t-if (E && !V)\n\t-\tV = xstrdup(E);\n\t+free(V);\n\t+V = xstrdup_or_null(E);\n\nBut yes, there is no preimage matching this pattern, so it\ndoesn't matter.\n\nDropping the rule makes the most sense.\n\nThanks,\n-Stolee\n"},{"id":"454725","messageId":"xmqq4k272796.fsf@gitster.g","threadId":"57826","inReplyTo":"4181f814-6d8e-ff2c-6fc9-a625ac189eae@github.com","subject":"Re: [PATCH] cocci: drop bogus xstrdup_or_null() rule","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-05-02T17:07:17Z","receivedAt":"2022-05-02T17:07:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Derrick Stolee <derrickstolee@github.com> writes:\n\n> I think you mean if it were\n> ...\n\nPlease disregard.  That one was completely bogus.\n\n> Dropping the rule makes the most sense.\n\nYup.  ThHanks for a doze of sanity.\n"}]}