linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] media: ABS macro parameter parenthesization
@ 2017-11-17 14:55 dgopstein
  2017-12-13 12:12 ` Mauro Carvalho Chehab
  0 siblings, 1 reply; 2+ messages in thread
From: dgopstein @ 2017-11-17 14:55 UTC (permalink / raw)
  To: linux-media; +Cc: baruch, Dan Gopstein

From: Dan Gopstein <dgopstein@nyu.edu>

Two definitions of the ABS (absolute value) macro fail to parenthesize
their parameter properly. This can lead to a bad expansion for
low-precedence expression arguments. Add parens to protect against
troublesome arguments.

For example: ABS(1-2) currently expands to ((1-2) < 0 ? (-1-2) : (1-2))
which evaluates to -3. But the correct expansion would be
((1-2) < 0 ? -(1-2) : (1-2)) which evaluates to 1.

Signed-off-by: Dan Gopstein <dgopstein@nyu.edu>
---
v1->v2:
* unmangled the patch
* added example to commit text

 drivers/media/dvb-frontends/dibx000_common.h | 2 +-
 drivers/media/dvb-frontends/mb86a16.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/dvb-frontends/dibx000_common.h b/drivers/media/dvb-frontends/dibx000_common.h
index 8784af9..ae60f5d 100644
--- a/drivers/media/dvb-frontends/dibx000_common.h
+++ b/drivers/media/dvb-frontends/dibx000_common.h
@@ -223,7 +223,7 @@ struct dvb_frontend_parametersContext {
 
 #define FE_CALLBACK_TIME_NEVER 0xffffffff
 
-#define ABS(x) ((x < 0) ? (-x) : (x))
+#define ABS(x) (((x) < 0) ? -(x) : (x))
 
 #define DATA_BUS_ACCESS_MODE_8BIT                 0x01
 #define DATA_BUS_ACCESS_MODE_16BIT                0x02
diff --git a/drivers/media/dvb-frontends/mb86a16.c b/drivers/media/dvb-frontends/mb86a16.c
index dfe322e..2d921c7 100644
--- a/drivers/media/dvb-frontends/mb86a16.c
+++ b/drivers/media/dvb-frontends/mb86a16.c
@@ -31,7 +31,7 @@
 static unsigned int verbose = 5;
 module_param(verbose, int, 0644);
 
-#define ABS(x)		((x) < 0 ? (-x) : (x))
+#define ABS(x)		((x) < 0 ? -(x) : (x))
 
 struct mb86a16_state {
 	struct i2c_adapter		*i2c_adap;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] media: ABS macro parameter parenthesization
  2017-11-17 14:55 [PATCH v2] media: ABS macro parameter parenthesization dgopstein
@ 2017-12-13 12:12 ` Mauro Carvalho Chehab
  0 siblings, 0 replies; 2+ messages in thread
From: Mauro Carvalho Chehab @ 2017-12-13 12:12 UTC (permalink / raw)
  To: dgopstein; +Cc: linux-media, baruch

Em Fri, 17 Nov 2017 09:55:44 -0500
dgopstein@nyu.edu escreveu:

> From: Dan Gopstein <dgopstein@nyu.edu>
> 
> Two definitions of the ABS (absolute value) macro fail to parenthesize
> their parameter properly. This can lead to a bad expansion for
> low-precedence expression arguments. Add parens to protect against
> troublesome arguments.
> 
> For example: ABS(1-2) currently expands to ((1-2) < 0 ? (-1-2) : (1-2))
> which evaluates to -3. But the correct expansion would be
> ((1-2) < 0 ? -(1-2) : (1-2)) which evaluates to 1.
> 
> Signed-off-by: Dan Gopstein <dgopstein@nyu.edu>
> ---
> v1->v2:
> * unmangled the patch
> * added example to commit text
> 
>  drivers/media/dvb-frontends/dibx000_common.h | 2 +-
>  drivers/media/dvb-frontends/mb86a16.c        | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/dvb-frontends/dibx000_common.h b/drivers/media/dvb-frontends/dibx000_common.h
> index 8784af9..ae60f5d 100644
> --- a/drivers/media/dvb-frontends/dibx000_common.h
> +++ b/drivers/media/dvb-frontends/dibx000_common.h
> @@ -223,7 +223,7 @@ struct dvb_frontend_parametersContext {
>  
>  #define FE_CALLBACK_TIME_NEVER 0xffffffff
>  
> -#define ABS(x) ((x < 0) ? (-x) : (x))
> +#define ABS(x) (((x) < 0) ? -(x) : (x))
>  
>  #define DATA_BUS_ACCESS_MODE_8BIT                 0x01
>  #define DATA_BUS_ACCESS_MODE_16BIT                0x02
> diff --git a/drivers/media/dvb-frontends/mb86a16.c b/drivers/media/dvb-frontends/mb86a16.c
> index dfe322e..2d921c7 100644
> --- a/drivers/media/dvb-frontends/mb86a16.c
> +++ b/drivers/media/dvb-frontends/mb86a16.c
> @@ -31,7 +31,7 @@
>  static unsigned int verbose = 5;
>  module_param(verbose, int, 0644);
>  
> -#define ABS(x)		((x) < 0 ? (-x) : (x))
> +#define ABS(x)		((x) < 0 ? -(x) : (x))
>  
>  struct mb86a16_state {
>  	struct i2c_adapter		*i2c_adap;

Actually, the Kernel has already a macro for that, called abs().

So, the better would be to just remove those macros,
replacing ABS(foo) by abs(foo).

Thanks,
Mauro

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-12-13 12:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-17 14:55 [PATCH v2] media: ABS macro parameter parenthesization dgopstein
2017-12-13 12:12 ` Mauro Carvalho Chehab

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).