All of lore.kernel.org
 help / color / mirror / Atom feed
From: julia.lawall@lip6.fr (Julia Lawall)
To: cocci@systeme.lip6.fr
Subject: [Cocci] malloc/calloc/strup adding missing NULL checks
Date: Fri, 16 Dec 2016 12:44:46 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.10.1612161242300.3566@hadrien> (raw)
In-Reply-To: <20161216113410.4vauxw4u2f364l7v@fbsd-laptop>



On Fri, 16 Dec 2016, Thomas Adam wrote:

> Hi,
>
> This is probably a classic example, but I'm struggling and was hoping the
> wisdom of the fine folks here could help.
>
> I'm trying to add any missing NULL checks to a few function calls, namely:
>
> 	malloc
> 	calloc
> 	strdup
>
> At present, I have the following rule:
>
> 	@@
> 	expression T;
> 	@@
>
> 	T = strdup(...);
> 	+ if (T == NULL)
> 	+ 	pkg_emit_errno("strdup", __func__);
> 	... when != (T == NULL)
> 	    when != (T != NULL)
>
>
> This is the same for calloc() and malloc().  And it works OK.  The problem I
> have is that it's not capturing all the cases.  So for example, the following
> is matched:
>
> 	char *foo;
> 	char *bar = "hello";
> 	foo = strdup(foo);
>
> But if I have something more complicated, such as this:
>
> 	struct *foo;
> 	foo->member = strdup("hello");
>
> Then the Cocci rule I have doesn't match -- and I can only assume at this
> point that struct members aren't covered by using an "expression"
> metavariable?

This is strange.  Because struct members are quite definitely covered by
the expression metavariable.  Perhaps the function that contains this code
incurs a parse error?  One way to see this is to say spatch --type-c
file.c.  If you don't see any type annotations in a function then there is
a problem.  You can also use --parse-c instead of --type-c to get some
information about the precise problem.  But the output can be verbose.
Look for the lines containing BAD.

julia



>
> You might also ask why I'm using "strdup(...)" -- this is because in some
> cases calls inside strup could be other function calls, such as:
>
> 	strdup(say_hello("Thomas"));
>
> ... and I wasn't sure how best to handle that either, so I just went with
> "..." which seems to work.
>
> How can I better ensure that my rule covers more of my code?
>
> TIA!
>
> Thomas Adam
> _______________________________________________
> Cocci mailing list
> Cocci at systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>

  reply	other threads:[~2016-12-16 11:44 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-16 11:34 [Cocci] malloc/calloc/strup adding missing NULL checks Thomas Adam
2016-12-16 11:44 ` Julia Lawall [this message]
2016-12-16 13:13   ` Thomas Adam
2016-12-16 13:54     ` Julia Lawall
2016-12-16 14:12       ` Thomas Adam
2016-12-16 14:33         ` Julia Lawall
2016-12-16 14:46           ` Thomas Adam
     [not found]             ` <alpine.DEB.2.10.1612161549220.6500@hadrien>
2016-12-16 17:08               ` Thomas Adam
     [not found]                 ` <alpine.DEB.2.10.1612161929450.3239@hadrien>
2016-12-16 18:37                   ` Thomas Adam
2016-12-16 19:05                     ` Julia Lawall
2016-12-16 19:10                       ` Thomas Adam
2016-12-16 19:21                         ` Julia Lawall
2016-12-16 19:31                           ` Thomas Adam
2016-12-16 19:33                             ` Julia Lawall
2016-12-16 21:55                               ` Thomas Adam
2016-12-20 19:45                                 ` [Cocci] … " SF Markus Elfring
2016-12-22 22:53                                   ` Thomas Adam
2016-12-23  6:43                                     ` Julia Lawall
2016-12-23  6:54                                     ` SF Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.10.1612161242300.3566@hadrien \
    --to=julia.lawall@lip6.fr \
    --cc=cocci@systeme.lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.