* [patch 2.6.26-rc1] i2c_use_client() defends against NULL
@ 2008-05-04 20:06 David Brownell
[not found] ` <200805041306.21074.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
0 siblings, 1 reply; 5+ messages in thread
From: David Brownell @ 2008-05-04 20:06 UTC (permalink / raw)
To: i2c-GZX6beZjE8VD60Wz+7aTrA
Defend the i2c refcount calls against NULL pointers, as is important
(and conventional) for such calls ... we don't want to morph NULL
pointers into bogus non-null ones. Note that none of the current
callers of i2c_use_client() use its return value.
Signed-off-by: David Brownell <dbrownell-Rn4VEauK+AKRv+LV9MX5uipxlwaOVQ5f@public.gmane.org>
---
drivers/i2c/i2c-core.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
--- g26.orig/drivers/i2c/i2c-core.c 2008-05-04 12:50:33.000000000 -0700
+++ g26/drivers/i2c/i2c-core.c 2008-05-04 12:51:04.000000000 -0700
@@ -1013,8 +1013,9 @@ EXPORT_SYMBOL(i2c_detach_client);
*/
struct i2c_client *i2c_use_client(struct i2c_client *client)
{
- get_device(&client->dev);
- return client;
+ if (client && get_device(&client->dev))
+ return client;
+ return NULL;
}
EXPORT_SYMBOL(i2c_use_client);
@@ -1026,7 +1027,8 @@ EXPORT_SYMBOL(i2c_use_client);
*/
void i2c_release_client(struct i2c_client *client)
{
- put_device(&client->dev);
+ if (client)
+ put_device(&client->dev);
}
EXPORT_SYMBOL(i2c_release_client);
_______________________________________________
i2c mailing list
i2c-GZX6beZjE8VD60Wz+7aTrA@public.gmane.org
http://lists.lm-sensors.org/mailman/listinfo/i2c
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [patch 2.6.26-rc1] i2c_use_client() defends against NULL
[not found] ` <200805041306.21074.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
@ 2008-05-17 12:40 ` Jean Delvare
[not found] ` <20080517144007.7611ab32-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
0 siblings, 1 reply; 5+ messages in thread
From: Jean Delvare @ 2008-05-17 12:40 UTC (permalink / raw)
To: David Brownell; +Cc: i2c-GZX6beZjE8VD60Wz+7aTrA
Hi David,
On Sun, 4 May 2008 13:06:20 -0700, David Brownell wrote:
> Defend the i2c refcount calls against NULL pointers, as is important
> (and conventional) for such calls ... we don't want to morph NULL
> pointers into bogus non-null ones.
Passing NULL to the current functions would just crash, it wouldn't
morph anything.
> Note that none of the current
> callers of i2c_use_client() use its return value.
Which sounds sane... when a function can only fail if you give it a
bogus parameter, it makes more sense to check the parameter for
validity yourself than to check the value returned by the function
afterwards. I'm not even sure why these functions return something in
the first place.
>
> Signed-off-by: David Brownell <dbrownell-Rn4VEauK+AKRv+LV9MX5uipxlwaOVQ5f@public.gmane.org>
> ---
> drivers/i2c/i2c-core.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> --- g26.orig/drivers/i2c/i2c-core.c 2008-05-04 12:50:33.000000000 -0700
> +++ g26/drivers/i2c/i2c-core.c 2008-05-04 12:51:04.000000000 -0700
> @@ -1013,8 +1013,9 @@ EXPORT_SYMBOL(i2c_detach_client);
> */
> struct i2c_client *i2c_use_client(struct i2c_client *client)
> {
> - get_device(&client->dev);
> - return client;
> + if (client && get_device(&client->dev))
> + return client;
> + return NULL;
> }
> EXPORT_SYMBOL(i2c_use_client);
>
> @@ -1026,7 +1027,8 @@ EXPORT_SYMBOL(i2c_use_client);
> */
> void i2c_release_client(struct i2c_client *client)
> {
> - put_device(&client->dev);
> + if (client)
> + put_device(&client->dev);
> }
> EXPORT_SYMBOL(i2c_release_client);
>
I hate this defensive programming, to me it's a waste of CPU cycles.
But as you said, it's apparently the norm in the Linux kernel to do
these useless checks for that specific type of functions, so I'll take
your patch even though I don't like it.
Thanks,
--
Jean Delvare
_______________________________________________
i2c mailing list
i2c-GZX6beZjE8VD60Wz+7aTrA@public.gmane.org
http://lists.lm-sensors.org/mailman/listinfo/i2c
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [patch 2.6.26-rc1] i2c_use_client() defends against NULL
[not found] ` <20080517144007.7611ab32-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
@ 2008-05-17 13:57 ` David Brownell
[not found] ` <200805170657.56833.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
0 siblings, 1 reply; 5+ messages in thread
From: David Brownell @ 2008-05-17 13:57 UTC (permalink / raw)
To: Jean Delvare; +Cc: i2c-GZX6beZjE8VD60Wz+7aTrA
On Saturday 17 May 2008, Jean Delvare wrote:
> On Sun, 4 May 2008 13:06:20 -0700, David Brownell wrote:
> > Defend the i2c refcount calls against NULL pointers, as is important
> > (and conventional) for such calls ... we don't want to morph NULL
> > pointers into bogus non-null ones.
>
> Passing NULL to the current functions would just crash, it wouldn't
> morph anything.
That's only if you assume an MMU set up to cause oopsing on reference
to the first and last pages of memory. Not the best of assumptions...
_______________________________________________
i2c mailing list
i2c-GZX6beZjE8VD60Wz+7aTrA@public.gmane.org
http://lists.lm-sensors.org/mailman/listinfo/i2c
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [patch 2.6.26-rc1] i2c_use_client() defends against NULL
[not found] ` <200805170657.56833.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
@ 2008-05-17 17:53 ` Trent Piepho
[not found] ` <Pine.LNX.4.58.0805171048070.4196-13q4cmjDBaTP3RPoUHIrnuTW4wlIGRCZ@public.gmane.org>
0 siblings, 1 reply; 5+ messages in thread
From: Trent Piepho @ 2008-05-17 17:53 UTC (permalink / raw)
To: David Brownell; +Cc: i2c-GZX6beZjE8VD60Wz+7aTrA
On Sat, 17 May 2008, David Brownell wrote:
> On Saturday 17 May 2008, Jean Delvare wrote:
> > On Sun, 4 May 2008 13:06:20 -0700, David Brownell wrote:
> > > Defend the i2c refcount calls against NULL pointers, as is important
> > > (and conventional) for such calls ... we don't want to morph NULL
> > > pointers into bogus non-null ones.
> >
> > Passing NULL to the current functions would just crash, it wouldn't
> > morph anything.
>
> That's only if you assume an MMU set up to cause oopsing on reference
> to the first and last pages of memory. Not the best of assumptions...
Maybe BUG_ON(!client) would be better? It seems more likely to catch
a programming error.
_______________________________________________
i2c mailing list
i2c-GZX6beZjE8VD60Wz+7aTrA@public.gmane.org
http://lists.lm-sensors.org/mailman/listinfo/i2c
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [patch 2.6.26-rc1] i2c_use_client() defends against NULL
[not found] ` <Pine.LNX.4.58.0805171048070.4196-13q4cmjDBaTP3RPoUHIrnuTW4wlIGRCZ@public.gmane.org>
@ 2008-05-17 18:53 ` David Brownell
0 siblings, 0 replies; 5+ messages in thread
From: David Brownell @ 2008-05-17 18:53 UTC (permalink / raw)
To: Trent Piepho; +Cc: i2c-GZX6beZjE8VD60Wz+7aTrA
On Saturday 17 May 2008, Trent Piepho wrote:
> > > Passing NULL to the current functions would just crash, it wouldn't
> > > morph anything.
> >
> > That's only if you assume an MMU set up to cause oopsing on reference
> > to the first and last pages of memory. Not the best of assumptions...
>
> Maybe BUG_ON(!client) would be better? It seems more likely to catch
> a programming error.
BUG_ON() is almost always the wrong answer though ... it's only
appropriate when there's no way for the system to continue.
And in fact, here it's *not* a bug...
Passing NULL around is more often OK than not. Example, it's
expected that passing it to a "free this object" routine will
work, saving callers a NULL check. Similarly, refcount calls.
_______________________________________________
i2c mailing list
i2c-GZX6beZjE8VD60Wz+7aTrA@public.gmane.org
http://lists.lm-sensors.org/mailman/listinfo/i2c
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2008-05-17 18:53 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-04 20:06 [patch 2.6.26-rc1] i2c_use_client() defends against NULL David Brownell
[not found] ` <200805041306.21074.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
2008-05-17 12:40 ` Jean Delvare
[not found] ` <20080517144007.7611ab32-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2008-05-17 13:57 ` David Brownell
[not found] ` <200805170657.56833.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
2008-05-17 17:53 ` Trent Piepho
[not found] ` <Pine.LNX.4.58.0805171048070.4196-13q4cmjDBaTP3RPoUHIrnuTW4wlIGRCZ@public.gmane.org>
2008-05-17 18:53 ` David Brownell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox