2020-09-01 10:51:24

by Antoni Przybylik

[permalink] [raw]
Subject: [PATCH] staging: gdm724x: gdm_tty: corrected macro by adding brackets

Such macros are dangerous. Consider following example:
#define GDM_TTY_READY(gdm) (gdm && gdm->tty_dev && gdm->port.count)
GDM_TTY_READY(a + b)
This macro will be expanded in such a way:
(a + b && a + b->tty_dev && a + b->port.count)
And it will lead to errors.

Signed-off-by: Antoni Przybylik <[email protected]>
---
drivers/staging/gdm724x/gdm_tty.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/gdm724x/gdm_tty.c b/drivers/staging/gdm724x/gdm_tty.c
index 6e813693a766..eab5c75ee5b1 100644
--- a/drivers/staging/gdm724x/gdm_tty.c
+++ b/drivers/staging/gdm724x/gdm_tty.c
@@ -27,7 +27,7 @@

#define MUX_TX_MAX_SIZE 2048

-#define GDM_TTY_READY(gdm) (gdm && gdm->tty_dev && gdm->port.count)
+#define GDM_TTY_READY(gdm) ((gdm) && (gdm)->tty_dev && (gdm)->port.count)

static struct tty_driver *gdm_driver[TTY_MAX_COUNT];
static struct gdm *gdm_table[TTY_MAX_COUNT][GDM_TTY_MINOR];
--
2.28.0


2020-09-01 13:38:56

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH] staging: gdm724x: gdm_tty: corrected macro by adding brackets

On Tue, Sep 01, 2020 at 12:43:11PM +0200, antoniprzybylik wrote:
^^^^^^^^^^^^^^^
Please, fix your From: header so that it says "Antoni Przybylik".

> Such macros are dangerous. Consider following example:
> #define GDM_TTY_READY(gdm) (gdm && gdm->tty_dev && gdm->port.count)
> GDM_TTY_READY(a + b)
> This macro will be expanded in such a way:
> (a + b && a + b->tty_dev && a + b->port.count)
> And it will lead to errors.
>
> Signed-off-by: Antoni Przybylik <[email protected]>
> ---
> drivers/staging/gdm724x/gdm_tty.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/staging/gdm724x/gdm_tty.c b/drivers/staging/gdm724x/gdm_tty.c
> index 6e813693a766..eab5c75ee5b1 100644
> --- a/drivers/staging/gdm724x/gdm_tty.c
> +++ b/drivers/staging/gdm724x/gdm_tty.c
> @@ -27,7 +27,7 @@
>
> #define MUX_TX_MAX_SIZE 2048
>
> -#define GDM_TTY_READY(gdm) (gdm && gdm->tty_dev && gdm->port.count)
> +#define GDM_TTY_READY(gdm) ((gdm) && (gdm)->tty_dev && (gdm)->port.count)

I cannot imagine a real life example where adding these parentheses will
prevent a bug. One idea it to silence this by making dereference ops
like this a special case where checkpatch.pl doesn't suggest adding
parentheses.

regards,
dan carpenter

2020-09-01 14:55:50

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH] staging: gdm724x: gdm_tty: corrected macro by adding brackets

On Tue, Sep 01, 2020 at 12:43:11PM +0200, antoniprzybylik wrote:
> Such macros are dangerous. Consider following example:
> #define GDM_TTY_READY(gdm) (gdm && gdm->tty_dev && gdm->port.count)
> GDM_TTY_READY(a + b)
> This macro will be expanded in such a way:
> (a + b && a + b->tty_dev && a + b->port.count)
> And it will lead to errors.

This is for a pointer, no one would ever do that :)

But, if you really worry about this, turn it into an inline function,
that way you get the proper typedef safety, which is what something like
this should really be, not a macro.

thanks,

greg k-h