Remove redundant NULL checks.
Subsystems (7)
cover Remove redundant NULL checks.
Remove redundant NULL checks before calls to functions that are equivalent to a no-op when run on a NULL pointer.
These patches were generated by the following semantic patch, and manually reviewed:
<smpl>
@r@
identifier noop_func;
identifier param;
type T;
@@
noop_func (T *param) {
...
if (!param) return;
...
}@@
identifier r.noop_func;
expression E;
statement S;
@@
(
- if (E) noop_func(E);
+ noop_func(E);
|
- if (E) { noop_func(E); E = NULL; }
+ noop_func(E);
+ E = NULL;
)
</smpl>
Regards,
Cyril Roelandt
---
Cyril Roelandt (5):
radeon: Remove redundant NULL check before radeon_i2c_destroy().
iommu: remove redundant NULL check before dma_ops_domain_free().
staging: dgrp: remove redundant NULL check before
unregister_dgrp_device().
staging: tidspbridge: remove redundant NULL check before
delete_msg_mgr().
xen: remove redundant NULL check before unregister_and_remove_pcpu().
drivers/gpu/drm/radeon/radeon_i2c.c | 6 ++---- drivers/iommu/amd_iommu.c | 3 +-- drivers/staging/dgrp/dgrp_specproc.c | 9 +++------ drivers/staging/tidspbridge/core/msg_sm.c | 3 +-- drivers/xen/pcpu.c | 3 +-- 5 files changed, 8 insertions(+), 16 deletions(-)
Signature · 1 line
1.7.10.4
PATCH 1/5 radeon: Remove redundant NULL check before radeon_i2c_destroy().
radeon_i2c_destroy on a NULL pointer is a no-op.
--- drivers/gpu/drm/radeon/radeon_i2c.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/radeon/radeon_i2c.c +2 -4
@@ -1032,10 +1032,8 @@ void radeon_i2c_fini(struct radeon_device *rdev)int i;for (i =0 ; i <RADEON_MAX_I2C_BUS ; i++) { -if (rdev->i2c_bus[i]) { -radeon_i2c_destroy (rdev->i2c_bus[i]); - rdev->i2c_bus[i] =NULL ; - } +radeon_i2c_destroy (rdev->i2c_bus[i]); + rdev->i2c_bus[i] =NULL ; } }
Signature · 1 line
1.7.10.4
PATCH 2/5 iommu: remove redundant NULL check before dma_ops_domain_free().
In reply to Cyril Roelandtdma_ops_domain_free on a NULL pointer is a no-op, so the NULL check in amd_iommu_init_dma_ops() can be removed.
--- drivers/iommu/amd_iommu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iommu/amd_iommu.c +1 -2
@@ -3187,8 +3187,7 @@ int __init amd_iommu_init_dma_ops(void) free_domains:for_each_iommu (iommu) { -if (iommu->default_dom) -dma_ops_domain_free (iommu->default_dom); +dma_ops_domain_free (iommu->default_dom); }return ret;
Signature · 1 line
1.7.10.4
Re: [PATCH 2/5] iommu: remove redundant NULL check before dma_ops_domain_free().
Quoted 7 lines · Cyril Roelandt
dma_ops_domain_freeon a NULL pointer is a no-op, so the NULL check inamd_iommu_init_dma_ops()can be removed.Signed-off-by Cyril Roelandt <[email protected]>--- drivers/iommu/amd_iommu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Applied to x86/amd, Thanks.
PATCH 3/5 staging: dgrp: remove redundant NULL check before unregister_dgrp_device().
In reply to Cyril Roelandtunregister_dgrp_device on a NULL pointer is a no-op, so the NULL checks in dgrp_remove_nd() can be removed.
--- drivers/staging/dgrp/dgrp_specproc.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/dgrp/dgrp_specproc.c +3 -6
@@ -777,14 +777,11 @@ static int dgrp_remove_nd(struct nd_struct *nd)dgrp_remove_node_class_sysfs_files (nd); } -if (nd->nd_mon_de) -unregister_dgrp_device (nd->nd_mon_de); +unregister_dgrp_device (nd->nd_mon_de); -if (nd->nd_ports_de) -unregister_dgrp_device (nd->nd_ports_de); +unregister_dgrp_device (nd->nd_ports_de); -if (nd->nd_dpa_de) -unregister_dgrp_device (nd->nd_dpa_de); +unregister_dgrp_device (nd->nd_dpa_de);dgrp_tty_uninit (nd);
Signature · 1 line
1.7.10.4
PATCH 4/5 staging: tidspbridge: remove redundant NULL check before delete_msg_mgr().
In reply to Cyril Roelandtdelete_msg_mgr on a NULL pointer is a no-op, so the NULL check in bridge_msg_delete can be removed.
--- drivers/staging/tidspbridge/core/msg_sm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/staging/tidspbridge/core/msg_sm.c +1 -2
@@ -198,8 +198,7 @@ out_err:*/ void bridge_msg_delete (struct msg_mgr *hmsg_mgr) { -if (hmsg_mgr) -delete_msg_mgr (hmsg_mgr); +delete_msg_mgr (hmsg_mgr); }/*
Signature · 1 line
1.7.10.4
PATCH 5/5 xen: remove redundant NULL check before unregister_and_remove_pcpu().
In reply to Cyril Roelandtunregister_and_remove_pcpu on a NULL pointer is a no-op, so the NULL check in sync_pcpu can be removed.
--- drivers/xen/pcpu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/xen/pcpu.c +1 -2
@@ -278,8 +278,7 @@ static int sync_pcpu(uint32_t cpu, uint32_t *max_cpu)* Only those at cpu present map has its sys interface. */ if (info->flags &XEN_PCPU_FLAGS_INVALID ) { -if (pcpu) -unregister_and_remove_pcpu (pcpu); +unregister_and_remove_pcpu (pcpu);return 0 ; }
Signature · 1 line
1.7.10.4
PATCH 5/5 xen: remove redundant NULL check before unregister_and_remove_pcpu().
In reply to Cyril Roelandtunregister_and_remove_pcpu on a NULL pointer is a no-op, so the NULL check in sync_pcpu can be removed.
--- drivers/xen/pcpu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/xen/pcpu.c +1 -2
@@ -278,8 +278,7 @@ static int sync_pcpu(uint32_t cpu, uint32_t *max_cpu)* Only those at cpu present map has its sys interface. */ if (info->flags &XEN_PCPU_FLAGS_INVALID ) { -if (pcpu) -unregister_and_remove_pcpu (pcpu); +unregister_and_remove_pcpu (pcpu);return 0 ; }
Signature · 1 line
1.7.10.4
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
cover Remove redundant NULL checks.
Quoted 1 line · Cyril Roelandt
(Shouldn't we be discussing this on Coccinelle's mailing list ?)
Sure
Quoted 41 lines · Julia Lawall
Quoted 36 lines · Cyril Roelandt
Quoted 9 lines · Julia Lawall
Quoted 2 lines
Why do you consider only void returning functions?
I was afraid to find cases like:
if (x)
ret = noop_func(x);Quoted 8 lines
/* Code where the value of "ret" matters */
In such cases, removing the NULL check might change the behaviour of the code.
It is not a problem when the return value ofnoop_funcis ignored, though (as
incpdma_ctlr_destroy()indrivers/net/ethernet/ti/davinci_cpdma.c).OK, good point. There are indeed two things happening in this case - the call and the assignment.
Quoted 5 lines
There are other issues with this semantic patch:
1) It only works if thenoop_funcis called in the file where it is defined,
which means it cannot catch "if (x) kfree(x)", for instance. I think I could:Quoted 8 lines
- run Coccinelle on the whole kernel to find functions that are no-ops when
given a NULL pointer as an argument;
- generate a second semantic patch that looks for the "if (x)noop_func(x)"
pattern withnoop_funcbeing one of the functions found by the first semantic
patch.Quoted 3 lines
You can use iteration for this. There is an example in the demos directory.
iteration.cocci, I believe.
Quoted 7 lines
I did that, and it seems to work well on small examples. I'm afraid it might take a long time on the Linux kernel, though. The patch is made of two parts:
one that looks for functions that are no-ops if one of the parameters is NULL, and one that looks for redundant NULL checks. Will the second part of the semantic patch be run on every file in the kernel for every function found by the first part ? I found 340 functions that we might be interested in, which is a lot... I attached the semantic patch.
If you are using glimpse or idutils it should continue doing that for the iterations and complete in a reasonable amount of time.
julia
Quoted 11 lines
Quoted 6 lines
Quoted 1 line
2) It doesn't work with functions that take more than one argument.
That should be fixable. You can use eg
expression list[n] es;
Indeed, works like a charm !
Cyril.
cover Remove redundant NULL checks.
Quoted 2 lines · Julia Lawall
If you are using glimpse or idutils it should continue doing that for the iterations and complete in a reasonable amount of time.
Indeed. There are __lots__ of matches, so I guess I'll mostly send patches to the staging tree where they'll probably be welcome.
Thanks for your help!
Cyril.