All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] malloc/calloc/strup adding missing NULL checks
@ 2016-12-16 11:34 Thomas Adam
  2016-12-16 11:44 ` Julia Lawall
  0 siblings, 1 reply; 19+ messages in thread
From: Thomas Adam @ 2016-12-16 11:34 UTC (permalink / raw)
  To: cocci

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?

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2016-12-23  6:54 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-16 11:34 [Cocci] malloc/calloc/strup adding missing NULL checks Thomas Adam
2016-12-16 11:44 ` Julia Lawall
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

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.