threads / patch / 45113

patchcocci: detect useless free(3) calls

Subject: [PATCH] cocci: detect useless free(3) calls

## tl;dr

4 messages between Feb 11, 2017 and Feb 11, 2017. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

René Scharfe· Feb 11, 2017, 13:58 UTC · lore

Add a semantic patch for removing checks that cause free(3) to only be called with a NULL pointer, as that must be a programming mistake.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
No cases are found in master or next, but 1d263b93 (bisect--helper:
`bisect_next_check` & bisect_voc shell function in C) introduced four
of them to pu.
 contrib/coccinelle/free.cocci | 6 ++++++
 1 file changed, 6 insertions(+)
Show changes to contrib/coccinelle/free.cocci +6 −0
diff --git a/contrib/coccinelle/free.cocci b/contrib/coccinelle/free.cocci
index e28213161a..c03ba737e5 100644
--- a/contrib/coccinelle/free.cocci
+++ b/contrib/coccinelle/free.cocci
@@ -3,3 +3,9 @@ expression E;
 @@
 - if (E)
   free(E);
+
+@@
+expression E;
+@@
+- if (!E)
+  free(E);
-- 
2.11.1
Junio C Hamano· Feb 11, 2017, 19:01 UTC · re: René Scharfe · lore

Re: [PATCH] cocci: detect useless free(3) calls

René Scharfe <l.s.r@web.de> writes:
Show 8 quoted lines
> Add a semantic patch for removing checks that cause free(3) to only be
> called with a NULL pointer, as that must be a programming mistake.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
> No cases are found in master or next, but 1d263b93 (bisect--helper:
> `bisect_next_check` & bisect_voc shell function in C) introduced four
> of them to pu.

Thanks. This should have been caught by code inspection, but having automated way to do so is always good.

Show 18 quoted lines
>
>  contrib/coccinelle/free.cocci | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/contrib/coccinelle/free.cocci b/contrib/coccinelle/free.cocci
> index e28213161a..c03ba737e5 100644
> --- a/contrib/coccinelle/free.cocci
> +++ b/contrib/coccinelle/free.cocci
> @@ -3,3 +3,9 @@ expression E;
>  @@
>  - if (E)
>    free(E);
> +
> +@@
> +expression E;
> +@@
> +- if (!E)
> +  free(E);
Lars Schneider· Feb 11, 2017, 19:31 UTC · re: René Scharfe · lore

Re: [PATCH] cocci: detect useless free(3) calls

Show 10 quoted lines
> On 11 Feb 2017, at 14:58, René Scharfe <l.s.r@web.de> wrote:
> 
> Add a semantic patch for removing checks that cause free(3) to only be
> called with a NULL pointer, as that must be a programming mistake.
> 
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
> No cases are found in master or next, but 1d263b93 (bisect--helper:
> `bisect_next_check` & bisect_voc shell function in C) introduced four
> of them to pu.
Hi Rene,

how do you run these checks on the entire Git source? Do you run each semantic patch file on the source like this?

spatch --sp-file contrib/coccinelle/qsort.cocci --dir /path/to/git/git ... spatch --sp-file contrib/coccinelle/free.cocci --dir /path/to/git/git

How stable do you consider these checks? Would it make sense to run them as part of the Travis-CI build [1]?

Thanks, Lars

[1] https://travis-ci.org/git/git/branches
René Scharfe· Feb 11, 2017, 19:50 UTC · re: Lars Schneider · lore

Re: [PATCH] cocci: detect useless free(3) calls

Am 11.02.2017 um 20:31 schrieb Lars Schneider:
Show 6 quoted lines
> how do you run these checks on the entire Git source?
> Do you run each semantic patch file on the source like this?
> 
> spatch --sp-file contrib/coccinelle/qsort.cocci --dir /path/to/git/git
> ...
> spatch --sp-file contrib/coccinelle/free.cocci --dir /path/to/git/git

With "make coccicheck", which runs spatch against the items in the make variable C_SOURCES, for all .cocci files.

> How stable do you consider these checks? Would it make sense to run them
> as part of the Travis-CI build [1]? 

There seem to have been problems with older versions[2], but I'm not aware of other issues.

Having these checks run automatically would be nice because they require a special tool which I guess not everybody is willing (or able) to install and because they take multiple minutes.

René
[2] https://public-inbox.org/git/014ef44e-9dd8-40b3-a3ec-b483f938ee02@web.de/

← back to recent threads