Skip to contentSkip to conversation

How to delete selected redundant null pointer checks with SmPL?

cocci Markus Elfring 2 participants 8 messages Apr 30, 2025 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hello,

I noticed also the contribution “[PATCH] drm/amdgpu/userq: remove unnecessary NULL check” by Dan Carpenter.
https://lore.kernel.org/kernel-janitors/[email protected]/
https://lkml.org/lkml/2025/4/30/389

I got into the mood to convert the source code adjustment approach into another script variant for the semantic patch language.

@deletion@
identifier member, object, pointer;
expression e;
@@
pointer = &object.member;
<+... when != pointer = e
(
-pointer &&
...
)
...+>

Questionable test result (also with the software combination “Coccinelle 1.3-00083-g4093aad2d”):
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> git checkout next-20250430 && spatch …/Projekte/Coccinelle/janitor/delete_redundant_check.cocci drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c …
deletion: position variables or mixed modifs interfere with comm_assoc iso
bool (pointer && ...)
…

Unfortunately, the known patch is not generated at the moment. How will development interests evolve further?

Regards,
Markus

#1 lore raw
Quoted 10 lines · Markus Elfring

Hello,

I noticed also the contribution “[PATCH] drm/amdgpu/userq: remove unnecessary NULL check” by Dan Carpenter.
https://lore.kernel.org/kernel-janitors/[email protected]/
https://lkml.org/lkml/2025/4/30/389

I got into the mood to convert the source code adjustment approach into another script variant for the semantic patch language.

Quoted 11 lines

@deletion@
identifier member, object, pointer;
expression e;
@@
pointer = &object.member;
<+... when != pointer = e
(
-pointer &&
...
)
...+>

I think you want to see if such a thing exists, not ensure that it occurs on all paths. You can say:

@deletion exists@
identifier member, object, pointer;
expression e;
@@
pointer = &object.member
...
- pointer &&
...

julia

Quoted 16 lines

Questionable test result (also with the software combination “Coccinelle 1.3-00083-g4093aad2d”):
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> git checkout next-20250430 && spatch …/Projekte/Coccinelle/janitor/delete_redundant_check.cocci drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c …
deletion: position variables or mixed modifs interfere with comm_assoc iso
bool (pointer && ...)
…

Unfortunately, the known patch is not generated at the moment. How will development interests evolve further?

Regards,
Markus

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 38 lines · Dan Carpenter
Quoted 29 lines · Julia Lawall
Quoted 10 lines · Markus Elfring

Hello,

I noticed also the contribution “[PATCH] drm/amdgpu/userq: remove unnecessary NULL check” by Dan Carpenter.
https://lore.kernel.org/kernel-janitors/[email protected]/
https://lkml.org/lkml/2025/4/30/389

I got into the mood to convert the source code adjustment approach into another script variant for the semantic patch language.

Quoted 11 lines

@deletion@
identifier member, object, pointer;
expression e;
@@
pointer = &object.member;
<+... when != pointer = e
(
-pointer &&
...
)
...+>

I think you want to see if such a thing exists, not ensure that it occurs on all paths. You can say:

Quoted 7 lines

@deletion exists@
identifier member, object, pointer;
expression e;
@@
pointer = &object.member
...
- pointer &&

Quoted 1 line

Any NULL checks should be removed, I believe?

That's not what Markus asked about, but one could do that similarly.

- pointer == NULL ||
...

julia

Quoted 5 lines

regards,
dan carpenter

#4 lore raw
Quoted 3 lines
Quoted 1 line

Any NULL checks should be removed, I believe?

That's not what Markus asked about,

I tried to express something in this direction.

Quoted 4 lines

but one could do that similarly.

- pointer == NULL ||
...

I would prefer to use the operator “&&” (logical AND) for the shown test case. Can it eventually work better?

Regards,
Markus

#5 lore raw
Quoted 7 lines · Markus Elfring
Quoted 3 lines
Quoted 1 line

Any NULL checks should be removed, I believe?

That's not what Markus asked about,

I tried to express something in this direction.

Quoted 6 lines
Quoted 4 lines

but one could do that similarly.

- pointer == NULL ||
...

I would prefer to use the operator “&&” (logical AND) for the shown test case. Can it eventually work better?

Maybe the function triggers a parse error?

julia

#6 lore raw
Quoted 1 line

Maybe the function triggers a parse error?

Do you observe a corresponding error message with your current software version?

Would any data processing be still improvable here?

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> spatch --parse-c drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c …
nb good = 818, nb bad = 97 =========> 89.40% good or passed

Regards,
Markus

#7 lore raw
In reply to Julia Lawall
Quoted 2 lines

I think you want to see if such a thing exists, not ensure that it occurs on all paths. You can say:

How good does this feedback fit to the following source code analysis approach?

@display exists@
identifier member, object, pointer;
expression e;
statement is, es;
@@
*pointer = &object.member;
... when != pointer = e
if
(
*pointer &&
...
)
is
else
es

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> git checkout next-20250430 && spatch …/Projekte/Coccinelle/janitor/show_redundant_checks3.cocci drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c …

Unfortunately, the expected patch is also not generated then.

Regards,
Markus

#8 lore raw
In reply to Julia Lawall
Quoted 2 lines

I think you want to see if such a thing exists, not ensure that it occurs on all paths. You can say:

It seems that the Coocinelle software needs still an extension for “iterators” according to special source code in the mentioned update candidate “amdgpu_userq_validate_bos”.
https://elixir.bootlin.com/linux/v6.15-rc4/source/include/drm/drm_exec.h#L95-L103

@deletion@
expression e;
identifier member, object, pointer;
iterator name drm_exec_until_all_locked;
@@
pointer = &object.member;
<+... when != pointer = e
(
-pointer &&
...
)
...+>

A patch can be generated as expected then.

Regards,
Markus