public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* spatch for trivial pointer comparison style?
@ 2014-11-13 19:55 Joe Perches
  2014-11-14  6:06 ` [Cocci] " Julia Lawall
  0 siblings, 1 reply; 8+ messages in thread
From: Joe Perches @ 2014-11-13 19:55 UTC (permalink / raw)
  To: cocci; +Cc: LKML

I added a checkpatch entry for this.
Maybe some cocci test like this would be useful?

@@
type t;
t *p;
@@
-	p == NULL
+	!p

@@
type t;
t *p;
@@
-	p != NULL
+	p

@@
type t;
t *p;
@@
-	NULL == p
+	!p

@@
type t;
t *p;
@@
-	NULL != p
+	p



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

* Re: [Cocci] spatch for trivial pointer comparison style?
  2014-11-13 19:55 spatch for trivial pointer comparison style? Joe Perches
@ 2014-11-14  6:06 ` Julia Lawall
  2014-11-14  6:12   ` Joe Perches
  2014-11-14 10:08   ` SF Markus Elfring
  0 siblings, 2 replies; 8+ messages in thread
From: Julia Lawall @ 2014-11-14  6:06 UTC (permalink / raw)
  To: Joe Perches; +Cc: cocci, LKML

On Thu, 13 Nov 2014, Joe Perches wrote:

> I added a checkpatch entry for this.
> Maybe some cocci test like this would be useful?
> 
> @@
> type t;
> t *p;
> @@
> -	p == NULL
> +	!p
> 
> @@
> type t;
> t *p;
> @@
> -	p != NULL
> +	p
> 
> @@
> type t;
> t *p;
> @@
> -	NULL == p
> +	!p
> 
> @@
> type t;
> t *p;
> @@
> -	NULL != p
> +	p

This was discussed many years ago.  I don't think that the change is 
desirable in all cases.  There are functions like kmalloc where NULL means 
failure and !p seems like the reasonable choice.  But there maybe other 
cases where NULL is somehow a meaningful value.  

Here is a link to the part of the discussion:

https://lkml.org/lkml/2007/7/27/103

julia

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

* Re: [Cocci] spatch for trivial pointer comparison style?
  2014-11-14  6:06 ` [Cocci] " Julia Lawall
@ 2014-11-14  6:12   ` Joe Perches
  2014-11-14  9:18     ` Julia Lawall
  2014-11-14 10:08   ` SF Markus Elfring
  1 sibling, 1 reply; 8+ messages in thread
From: Joe Perches @ 2014-11-14  6:12 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, LKML

On Fri, 2014-11-14 at 07:06 +0100, Julia Lawall wrote:
> On Thu, 13 Nov 2014, Joe Perches wrote:
> 
> > I added a checkpatch entry for this.
> > Maybe some cocci test like this would be useful?
> > 
> > @@
> > type t;
> > t *p;
> > @@
> > -	p == NULL
> > +	!p
> > 
> > @@
> > type t;
> > t *p;
> > @@
> > -	p != NULL
> > +	p
> > 
> > @@
> > type t;
> > t *p;
> > @@
> > -	NULL == p
> > +	!p
> > 
> > @@
> > type t;
> > t *p;
> > @@
> > -	NULL != p
> > +	p
> 
> This was discussed many years ago.  I don't think that the change is 
> desirable in all cases.  There are functions like kmalloc where NULL means 
> failure and !p seems like the reasonable choice.  But there maybe other 
> cases where NULL is somehow a meaningful value.  
> 
> Here is a link to the part of the discussion:
> 
> https://lkml.org/lkml/2007/7/27/103

Yes, I agree with some of the things Al Viro said
there, but isn't 'type t; t *p;' a subset of
"expression *e"?




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

* Re: [Cocci] spatch for trivial pointer comparison style?
  2014-11-14  6:12   ` Joe Perches
@ 2014-11-14  9:18     ` Julia Lawall
  2014-11-14 16:22       ` Joe Perches
  0 siblings, 1 reply; 8+ messages in thread
From: Julia Lawall @ 2014-11-14  9:18 UTC (permalink / raw)
  To: Joe Perches; +Cc: cocci, LKML



On Thu, 13 Nov 2014, Joe Perches wrote:

> On Fri, 2014-11-14 at 07:06 +0100, Julia Lawall wrote:
> > On Thu, 13 Nov 2014, Joe Perches wrote:
> >
> > > I added a checkpatch entry for this.
> > > Maybe some cocci test like this would be useful?
> > >
> > > @@
> > > type t;
> > > t *p;
> > > @@
> > > -	p == NULL
> > > +	!p
> > >
> > > @@
> > > type t;
> > > t *p;
> > > @@
> > > -	p != NULL
> > > +	p
> > >
> > > @@
> > > type t;
> > > t *p;
> > > @@
> > > -	NULL == p
> > > +	!p
> > >
> > > @@
> > > type t;
> > > t *p;
> > > @@
> > > -	NULL != p
> > > +	p
> >
> > This was discussed many years ago.  I don't think that the change is
> > desirable in all cases.  There are functions like kmalloc where NULL means
> > failure and !p seems like the reasonable choice.  But there maybe other
> > cases where NULL is somehow a meaningful value.
> >
> > Here is a link to the part of the discussion:
> >
> > https://lkml.org/lkml/2007/7/27/103
>
> Yes, I agree with some of the things Al Viro said
> there, but isn't 'type t; t *p;' a subset of
> "expression *e"?

No.  How would you expect it to be different.  type t means that the type
is known.  expression *e means that there is a * in the type.  But there
is no way to know that there is a * in the type without knowing the full
type.

Maybe something like

e = f(...);
...
if (e == NULL) S1 else S2

would be acceptable?  But I was thinking that for some functions NULL
might be considered to be a meaningful result, rather than a sign of
failure.

The following semantic patch gives almost 3000 results:

@disable is_null@
expression e;
statement S1,S2;
@@

e = \(kmalloc\|kzalloc\|kcalloc\|devm_kmalloc\|devm_kzalloc\)(...);
... when != e
if (<+...
- e == NULL
+ !e
    ...+>) S1 else S2

julia

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

* Re: [Cocci] spatch for trivial pointer comparison style?
  2014-11-14  6:06 ` [Cocci] " Julia Lawall
  2014-11-14  6:12   ` Joe Perches
@ 2014-11-14 10:08   ` SF Markus Elfring
  2014-11-14 10:31     ` Julia Lawall
  1 sibling, 1 reply; 8+ messages in thread
From: SF Markus Elfring @ 2014-11-14 10:08 UTC (permalink / raw)
  To: Julia Lawall, Joe Perches; +Cc: Coccinelle, LKML

> I don't think that the change is desirable in all cases.  There are
> functions like kmalloc where NULL means failure and !p seems like the
> reasonable choice.  But there maybe other cases where NULL is somehow
> a meaningful value.

How do you think about to adjust checks for null pointers not only
in Linux source files but also in other applications?
Are there any more software design challenges to consider with the
definition of the preprocessor symbol "NULL"?

Regards,
Markus

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

* Re: [Cocci] spatch for trivial pointer comparison style?
  2014-11-14 10:08   ` SF Markus Elfring
@ 2014-11-14 10:31     ` Julia Lawall
  0 siblings, 0 replies; 8+ messages in thread
From: Julia Lawall @ 2014-11-14 10:31 UTC (permalink / raw)
  To: SF Markus Elfring; +Cc: Joe Perches, Coccinelle, LKML

On Fri, 14 Nov 2014, SF Markus Elfring wrote:

> > I don't think that the change is desirable in all cases.  There are
> > functions like kmalloc where NULL means failure and !p seems like the
> > reasonable choice.  But there maybe other cases where NULL is somehow
> > a meaningful value.
>
> How do you think about to adjust checks for null pointers not only
> in Linux source files but also in other applications?
> Are there any more software design challenges to consider with the
> definition of the preprocessor symbol "NULL"?

Other applications may have other preferences.

julia

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

* Re: [Cocci] spatch for trivial pointer comparison style?
  2014-11-14  9:18     ` Julia Lawall
@ 2014-11-14 16:22       ` Joe Perches
  2014-11-15  6:11         ` Julia Lawall
  0 siblings, 1 reply; 8+ messages in thread
From: Joe Perches @ 2014-11-14 16:22 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, LKML

On Fri, 2014-11-14 at 10:18 +0100, Julia Lawall wrote:
> On Thu, 13 Nov 2014, Joe Perches wrote:
[]
> > Yes, I agree with some of the things Al Viro said
> > there, but isn't 'type t; t *p;' a subset of
> > "expression *e"?

> No.  How would you expect it to be different.

[]

>   type t means that the type
> is known.  expression *e means that there is a * in the type.

I had thought "expression *" could be r-value and
"type t; t *p;" could be l-value.

But then I don't find (or maybe don't parse too well)
the coccinelle documentation that specifies these
type relationships.

cheers, Joe



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

* Re: [Cocci] spatch for trivial pointer comparison style?
  2014-11-14 16:22       ` Joe Perches
@ 2014-11-15  6:11         ` Julia Lawall
  0 siblings, 0 replies; 8+ messages in thread
From: Julia Lawall @ 2014-11-15  6:11 UTC (permalink / raw)
  To: Joe Perches; +Cc: cocci, LKML

On Fri, 14 Nov 2014, Joe Perches wrote:

> On Fri, 2014-11-14 at 10:18 +0100, Julia Lawall wrote:
> > On Thu, 13 Nov 2014, Joe Perches wrote:
> []
> > > Yes, I agree with some of the things Al Viro said
> > > there, but isn't 'type t; t *p;' a subset of
> > > "expression *e"?
> 
> > No.  How would you expect it to be different.
> 
> []

No. [] and * are treated completely differently.

> >   type t means that the type
> > is known.  expression *e means that there is a * in the type.
> 
> I had thought "expression *" could be r-value and
> "type t; t *p;" could be l-value.

No, you made that one up :)  As we considered that it would be common to 
want to specify the type of an expression, we thought it would be tiresome 
to have to put eg expression int x.  So you can just say int x.

The downside is that people write

identifer x;

and then don't understand the error message, because any misspelled 
metavariable kind is considered to be a type name.

julia

> But then I don't find (or maybe don't parse too well)
> the coccinelle documentation that specifies these
> type relationships.
> 
> cheers, Joe
> 
> 
> 

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

end of thread, other threads:[~2014-11-15  6:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-13 19:55 spatch for trivial pointer comparison style? Joe Perches
2014-11-14  6:06 ` [Cocci] " Julia Lawall
2014-11-14  6:12   ` Joe Perches
2014-11-14  9:18     ` Julia Lawall
2014-11-14 16:22       ` Joe Perches
2014-11-15  6:11         ` Julia Lawall
2014-11-14 10:08   ` SF Markus Elfring
2014-11-14 10:31     ` Julia Lawall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox