Hello,
just found a mis-user of platform_set_drvdata(), so introduce typechecking
to these macro definitions. An alternative would be to make them static inline
functions.
comments welcome,
Marc
This patch introduces BUILD_BUG_ON_NOT_SAME_TYPE which forces a compilation
error if the two supplied variables don't have the same type.
Signed-off-by: Marc Kleine-Budde <[email protected]>
---
include/linux/kernel.h | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 2fe6e84..b637d57 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -612,6 +612,8 @@ extern int __build_bug_on_failed;
} while(0)
#endif
+#define BUILD_BUG_ON_NOT_SAME_TYPE(a, b) BUILD_BUG_ON(!__same_type((a), (b)));
+
/* Trap pasters of __FUNCTION__ at compile-time */
#define __FUNCTION__ (__func__)
--
1.7.2.3
Use the previously introduced BUILD_BUG_ON_NOT_SAME_TYPE to check if the
dev argument is a struct platform_device pointer.
Signed-off-by: Marc Kleine-Budde <[email protected]>
---
include/linux/platform_device.h | 10 ++++++++--
1 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
index 2e700ec..761a917 100644
--- a/include/linux/platform_device.h
+++ b/include/linux/platform_device.h
@@ -130,8 +130,14 @@ extern void platform_driver_unregister(struct platform_driver *);
extern int platform_driver_probe(struct platform_driver *driver,
int (*probe)(struct platform_device *));
-#define platform_get_drvdata(_dev) dev_get_drvdata(&(_dev)->dev)
-#define platform_set_drvdata(_dev,data) dev_set_drvdata(&(_dev)->dev, (data))
+#define platform_get_drvdata(_dev) ({ \
+ BUILD_BUG_ON_NOT_SAME_TYPE((_dev), (struct platform_device *)NULL); \
+ dev_get_drvdata(&(_dev)->dev); \
+})
+#define platform_set_drvdata(_dev,data) ({ \
+ BUILD_BUG_ON_NOT_SAME_TYPE((_dev), (struct platform_device *)NULL); \
+ dev_set_drvdata(&(_dev)->dev, (data)); \
+})
extern struct platform_device *platform_create_bundle(struct platform_driver *driver,
int (*probe)(struct platform_device *),
--
1.7.2.3
On 02/16/2011 10:55 PM, Andrew Morton wrote:
> On Wed, 16 Feb 2011 22:49:52 +0100
> Marc Kleine-Budde <[email protected]> wrote:
>
>> From: Marc Kleine-Budde <[email protected]>
>> To: [email protected]
>> Cc: Greg Kroah-Hartman <[email protected]>, Andrew Morton <[email protected]>
>
> No mailing list was cc'ed.
just noticed, sorry :(
>> Subject: [PATCH 0/2] RFC: introduce typechecking to platform_{get,set}_drvdata
>> Date: Wed, 16 Feb 2011 22:49:52 +0100
>> X-Mailer: git-send-email 1.7.2.3
>>
>> Hello,
>>
>> just found a mis-user of platform_set_drvdata(), so introduce typechecking
>> to these macro definitions.
>
> ick.
>
>> An alternative would be to make them static inline
>> functions.
>
> A vastly preferable alternative! How's about doing that?
Sure...wait for "v2"
regards, Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
On Wed, Feb 16, 2011 at 10:55:49PM +0100, Marc Kleine-Budde wrote:
> Hello,
>
> just found a mis-user of platform_set_drvdata(), so introduce typechecking
> to these macro definitions. An alternative would be to make them static inline
> functions.
Make it a static inline, that way you don't have to create that new
macro, as the build will warn/break right away.
thanks,
greg k-h