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

4 messages from 2017-02-11 to 2017-02-11. Participants: René Scharfe, Junio C Hamano, Lars Schneider.
Thread: https://gitlist.dev/t/45113

## René Scharfe, 2017-02-11 13:58

Subject: [PATCH] cocci: detect useless free(3) calls
Message-ID: <7e10f934-f084-ceb4-00eb-b75cdb01886b@web.de>
URL: https://gitlist.dev/e/7e10f934-f084-ceb4-00eb-b75cdb01886b%40web.de

```
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(+)

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, 2017-02-11 19:01

Subject: Re: [PATCH] cocci: detect useless free(3) calls
Message-ID: <xmqqr334h7rg.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqr334h7rg.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <7e10f934-f084-ceb4-00eb-b75cdb01886b@web.de>

```
René Scharfe <l.s.r@web.de> writes:

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

>
>  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, 2017-02-11 19:31

Subject: Re: [PATCH] cocci: detect useless free(3) calls
Message-ID: <5DF3AA3E-90C6-443C-A22C-489CE2C89A51@gmail.com>
URL: https://gitlist.dev/e/5DF3AA3E-90C6-443C-A22C-489CE2C89A51%40gmail.com
In-Reply-To: <7e10f934-f084-ceb4-00eb-b75cdb01886b@web.de>

```

> 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, 2017-02-11 19:50

Subject: Re: [PATCH] cocci: detect useless free(3) calls
Message-ID: <ca01f8de-f5ce-2201-450d-cadd6aca7cb8@web.de>
URL: https://gitlist.dev/e/ca01f8de-f5ce-2201-450d-cadd6aca7cb8%40web.de
In-Reply-To: <5DF3AA3E-90C6-443C-A22C-489CE2C89A51@gmail.com>

```
Am 11.02.2017 um 20:31 schrieb Lars Schneider:
> 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/

```
