omap2/omapfb: make DBG() more resistant in if-else constructions
PATCH 1/1 omap2/omapfb: make DBG() more resistant in if-else constructions
When DBG() is used in a simple if-else, the resulting code path currently depends on the definition of DBG(). Inserting the statement in a "do { ... } while (0)" prevents this possible misuse.
---
Note, I have not found any offenders, but a mistake can easily be made. The following example shows what can go wrong if little intention is paid to the definition of the DBG() macro.
Example:
if something_went_wrong()
DBG("oh no, something went wrong!\n");
else
printk("all went fine\n");Old result where the else is placed inside the first if-statment:
if something_went_wrong() {
if (omapfb_debug) {
printk(KERN_DEBUG "oh no, something went wrong!\n");
} else {
printk("all went fine\n");
}
}New result where the else is an alternative to the first if-statement:
if something_went_wrong() {
do {
if (omapfb_debug)
printk(KERN_DEBUG "oh no, something went wrong!\n");
} while (0);
} else {
printk("all went fine\n");
}
---
drivers/video/omap2/omapfb/omapfb.h | 6 ++++--
1 files changed, 4 insertions(+), 2 deletions(-)drivers/video/omap2/omapfb/omapfb.h +4 -2
@@ -34,8 +34,10 @@#ifdef DEBUG extern unsigned int omapfb_debug;#define DBG (format, ...) \ - if (omapfb_debug) \ - printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__) + do { \ + if (omapfb_debug) \ + printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__); \ + while (0)#else #define DBG (format, ...)#endif
Signature · 1 line
1.7.4.4
Quoted 59 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Niels de Vos
Sent: Tuesday, May 10, 2011 2:51 PM
To: [email protected]
Cc: [email protected]; [email protected]; Niels de Vos
Subject: [PATCH] omap2/omapfb: makeDBG()more resistant in if-else constructionsWhen
DBG()is used in a simple if-else, the resulting code path currently depends on the definition ofDBG(). Inserting the statement in
a "do { ... } while (0)" prevents this possible misuse.Signed-off-by Niels de Vos <[email protected]>---
Note, I have not found any offenders, but a mistake can easily be made.
The following example shows what can go wrong if little intention is paid to the definition of theDBG()macro.Example:
if something_went_wrong() DBG("oh no, something went wrong!\n"); else printk("all went fine\n");Old result where the else is placed inside the first if-statment:
if something_went_wrong() { if (omapfb_debug) { printk(KERN_DEBUG "oh no, something went wrong!\n"); } else { printk("all went fine\n"); } }New result where the else is an alternative to the first if-statement:
if something_went_wrong() { do { if (omapfb_debug) printk(KERN_DEBUG "oh no, something went wrong!\n"); } while (0); } else { printk("all went fine\n"); } --- drivers/video/omap2/omapfb/omapfb.h | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)drivers/video/omap2/omapfb/omapfb.h +0
-0
drivers/video/omap2/omapfb/omapfb.h line 41
Quoted 10 lines
drivers/video/omap2/omapfb/omapfb.h +4
-2@@ -34,8 +34,10 @@#ifdef DEBUG extern unsigned int omapfb_debug;#define DBG (format, ...) \ - if (omapfb_debug) \ - printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__) + do { \ + if (omapfb_debug) \ + printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__); \ +while (0 )
A real good find. Wondering if it really didn't create any problems so far...
~sanjeev
Quoted 12 lines
drivers/video/omap2/omapfb/omapfb.h +0
-0#else #define DBG (format, ...)#endif
Signature · 7 lines
1.7.4.4 -- To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 9 lines · Niels de Vos
When
DBG()is used in a simple if-else, the resulting code path currently depends on the definition ofDBG(). Inserting the statement in a "do { ... } while (0)" prevents this possible misuse.Signed-off-by Niels de Vos <[email protected]>
drivers/video/omap2/omapfb/omapfb.h line 40
Quoted 9 lines
drivers/video/omap2/omapfb/omapfb.h +4
-2@@ -34,8 +34,10 @@ #ifdef DEBUG extern unsigned int omapfb_debug; #define DBG(format, ...) \ - if (omapfb_debug) \ - printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__) + do { \ + if (omapfb_debug) \ + printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__); \ + while (0)
Where's the closing '}'?
drivers/video/omap2/omapfb/omapfb.h line 42
Quoted 2 lines
drivers/video/omap2/omapfb/omapfb.h +0
-0#else #define DBG(format, ...)
BTW, no printf()-style format checking here.
drivers/video/omap2/omapfb/omapfb.h line 43
Quoted 1 line
drivers/video/omap2/omapfb/omapfb.h +0
-0#endif
What about using the standard pr_debug()/dev_dbg() instead?
With dynamic debug, it can be enabled at run time.
As a bonus, you get printf()-style format checking if debugging is disabled.
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 7 lines · Geert Uytterhoeven
Quoted 5 lines · Niels de Vos
When
DBG()is used in a simple if-else, the resulting code path currently depends on the definition ofDBG(). Inserting the statement in a "do { ... } while (0)" prevents this possible misuse.Signed-off-by Niels de Vos <[email protected]>
Quoted 14 lines
drivers/video/omap2/omapfb/omapfb.h line 40
Quoted 12 lines
drivers/video/omap2/omapfb/omapfb.h +4
-2@@ -34,8 +34,10 @@ #ifdef DEBUG extern unsigned int omapfb_debug; #define DBG(format, ...) \ - if (omapfb_debug) \ - printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__) + do { \ + if (omapfb_debug) \ + printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__); \ + while (0)Where's the closing '}'?
Good catch! That's in a "fixup!" that I forgot to squash :-/ I'll post an update version in a bit.
Quoted 10 lines
drivers/video/omap2/omapfb/omapfb.h line 42
Quoted 2 lines
drivers/video/omap2/omapfb/omapfb.h +0
-0#else #define DBG(format, ...)BTW, no printf()-style format checking here.
drivers/video/omap2/omapfb/omapfb.h line 43
Quoted 1 line
drivers/video/omap2/omapfb/omapfb.h +0
-0#endifWhat about using the standard pr_debug()/dev_dbg() instead?
With dynamic debug, it can be enabled at run time.
As a bonus, you get printf()-style format checking if debugging is disabled.
I think removing DBG() and the omapfb_debug module-parameter is surely a good thing. Unfortunately DBG() is used quite a bit in the code and replacing them 'll take some more time. I don't know yet when I find some time to do and test that.
Thanks for the pointers,
Niels
Quoted 18 lines
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
PATCH v2 1/1 omap2/omapfb: make DBG() more resistant in if-else constructions
In reply to Geert UytterhoevenWhen DBG() is used in a simple if-else, the resulting code path currently depends on the definition of DBG(). Inserting the statement in a "do { ... } while (0)" prevents this possible misuse.
---
V2: add the missing closing }
Note, I have not found any offenders, but a mistake can easily be made. The following example shows what can go wrong if little intention is paid to the definition of the DBG() macro.
Example:
if something_went_wrong()
DBG("oh no, something went wrong!\n");
else
printk("all went fine\n");Old result where the else is placed inside the first if-statment:
if something_went_wrong() {
if (omapfb_debug) {
printk(KERN_DEBUG "oh no, something went wrong!\n");
} else {
printk("all went fine\n");
}
}New result where the else is an alternative to the first if-statement:
if something_went_wrong() {
do {
if (omapfb_debug)
printk(KERN_DEBUG "oh no, something went wrong!\n");
} while (0);
} else {
printk("all went fine\n");
}
---
drivers/video/omap2/omapfb/omapfb.h | 6 ++++--
1 files changed, 4 insertions(+), 2 deletions(-)drivers/video/omap2/omapfb/omapfb.h +4 -2
@@ -34,8 +34,10 @@#ifdef DEBUG extern unsigned int omapfb_debug;#define DBG (format, ...) \ - if (omapfb_debug) \ - printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__) + do { \ + if (omapfb_debug) \ + printk(KERN_DEBUG "OMAPFB: " format, ## __VA_ARGS__); \ + } while (0)#else #define DBG (format, ...)#endif
Signature · 1 line
1.7.4.4
Quoted 5 lines · Niels de Vos
When
DBG()is used in a simple if-else, the resulting code path currently depends on the definition ofDBG(). Inserting the statement in a "do { ... } while (0)" prevents this possible misuse.Signed-off-by Niels de Vos <[email protected]>
Thanks, I'll add this to the dss2 tree.
Tomi
Quoted 3 lines · Geert Uytterhoeven
What about using the standard pr_debug()/dev_dbg() instead?
With dynamic debug, it can be enabled at run time.
As a bonus, you get printf()-style format checking if debugging is disabled.
Yes, dev_dbg & co. would be better.
However, one thing I dislike about them is the extra stuff they print. For example, for omapfb and omapdss dev_dbg will print:
omapfb omapfb: fooomapdss_dss omapdss_dss: foo
I originally added the debug macros to omapdss to be able to automatically print the DSS module name, as at that point there was only one big omapdss device. And I guess I just followed with similar macro in omapfb also. But I believe both omapdss and omapfb should be changed to dev_* prints sometime soon.
Tomi
Quoted 10 lines · Tomi Valkeinen
Quoted 3 lines · Geert Uytterhoeven
What about using the standard pr_debug()/dev_dbg() instead?
With dynamic debug, it can be enabled at run time.
As a bonus, you get printf()-style format checking if debugging is disabled.Yes,
dev_dbg& co. would be better.However, one thing I dislike about them is the extra stuff they print. For example, for omapfb and omapdss
dev_dbgwill print:
Quoted 8 lines
omapfb omapfb: foo
omapdss_dssomapdss_dss: fooI originally added the debug macros to omapdss to be able to automatically print the DSS module name, as at that point there was only one big omapdss device. And I guess I just followed with similar macro in omapfb also. But I believe both omapdss and omapfb should be changed to dev_* prints sometime soon.
If you don't want the extra baggage, do
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
and use pr_debug().
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds