mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Premi, Sanjeev" <premi@ti.com>
To: Niels de Vos <ndevos@redhat.com>,
	"linux-omap@vger.kernel.org" <linux-omap@vger.kernel.org>
Cc: "linux-fbdev@vger.kernel.org" <linux-fbdev@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH] omap2/omapfb: make DBG() more resistant in if-else constructions
Date: Tue, 10 May 2011 15:07:04 +0530	[thread overview]
Message-ID: <B85A65D85D7EB246BE421B3FB0FBB593024CDACEDC@dbde02.ent.ti.com> (raw)
In-Reply-To: <1305019249-9898-1-git-send-email-ndevos@redhat.com>

> -----Original Message-----
> From: linux-omap-owner@vger.kernel.org 
> [mailto:linux-omap-owner@vger.kernel.org] On Behalf Of Niels de Vos
> Sent: Tuesday, May 10, 2011 2:51 PM
> To: linux-omap@vger.kernel.org
> Cc: linux-fbdev@vger.kernel.org; 
> linux-kernel@vger.kernel.org; Niels de Vos
> Subject: [PATCH] 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.
> 
> Signed-off-by: Niels de Vos <ndevos@redhat.com>
> 
> ---
> 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(-)
> 
> diff --git a/drivers/video/omap2/omapfb/omapfb.h 
> b/drivers/video/omap2/omapfb/omapfb.h
> index 1305fc9..a01b0bb 100644
> --- a/drivers/video/omap2/omapfb/omapfb.h
> +++ b/drivers/video/omap2/omapfb/omapfb.h
> @@ -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

>  #else
>  #define DBG(format, ...)
>  #endif
> -- 
> 1.7.4.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe 
> linux-omap" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

  reply	other threads:[~2011-05-10  9:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-10  9:20 Niels de Vos
2011-05-10  9:37 ` Premi, Sanjeev [this message]
2011-05-10  9:42 ` Geert Uytterhoeven
2011-05-10 10:57   ` Niels de Vos
2011-05-10 11:07   ` [PATCH V2] " Niels de Vos
2011-05-10 12:16     ` Tomi Valkeinen
2011-05-10 12:08   ` [PATCH] " Tomi Valkeinen
2011-05-10 12:14     ` Geert Uytterhoeven

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B85A65D85D7EB246BE421B3FB0FBB593024CDACEDC@dbde02.ent.ti.com \
    --to=premi@ti.com \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=ndevos@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®