Skip to contentSkip to conversation

thermal: Fix KELVIN_TO_CELSIUS macro

PATCH In review Guenter Roeck 3 participants 4 messages Mar 24, 2014 – Apr 3, 2014 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal: Fix KELVIN_TO_CELSIUS macro

It is always a good idea to use paranthesis around macro parameters to avoid undesired side effects.

In this specific case, KELVIN_TO_CELSIUS() is used in drivers/platform/x86/asus-wmi.c with parameter "value & 0xFFFF", which due to operator evaluation order causes more or less random results.

---
 include/linux/thermal.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/thermal.h +2 -2
index f7e11c7..02488c9 100644
@@ -41,8 +41,8 @@
 #define THERMAL_NO_LIMIT	THERMAL_CSTATE_INVALID
 
 /* Unit conversion macros */
-#define KELVIN_TO_CELSIUS(t)	(long)(((long)t-2732 >= 0) ?	\
-				((long)t-2732+5)/10 : ((long)t-2732-5)/10)
+#define KELVIN_TO_CELSIUS(t)	(long)(((long)(t)-2732 >= 0) ?	\
+				((long)(t)-2732+5)/10 : ((long)(t)-2732-5)/10)
 #define CELSIUS_TO_KELVIN(t)	((t)*10+2732)
 
 /* Adding event notification support elements */
Signature · 1 line
1.7.9.7
#1 lore raw
Quoted 7 lines · Guenter Roeck

It is always a good idea to use paranthesis around macro parameters to avoid undesired side effects.

In this specific case, KELVIN_TO_CELSIUS() is used in drivers/platform/x86/asus-wmi.c with parameter "value & 0xFFFF", which due to operator evaluation order causes more or less random results.

Maybe it's better to use a statement expression to avoid multiple calculations of t

Maybe;

#define KELVIN_TO_CELSIUS(t) \

({							\
	long _t = (long)(t) - 2732;			\
	_t >= 0 ? (_t + 5) / 10 : (_t - 5) / 10;	\
})
#2 lore raw
Quoted 12 lines · Joe Perches
Quoted 7 lines · Guenter Roeck

It is always a good idea to use paranthesis around macro parameters to avoid undesired side effects.

In this specific case, KELVIN_TO_CELSIUS() is used in drivers/platform/x86/asus-wmi.c with parameter "value & 0xFFFF", which due to operator evaluation order causes more or less random results.

Maybe it's better to use a statement expression to avoid multiple calculations of t

Quoted 9 lines

Maybe;

#define KELVIN_TO_CELSIUS(t) \

({							\
	long _t = (long)(t) - 2732;			\
	_t >= 0 ? (_t + 5) / 10 : (_t - 5) / 10;	\
})

Possibly, but I only want to have a bug fixed which should possibly be applied to -stable at some point. As such, I prefer the minimalistic approach. Anything more fancy on top of that can be submitted as separate patch.

Guenter

reviewer
#3 lore raw
Quoted 23 lines · Guenter Roeck
Quoted 12 lines · Joe Perches
Quoted 7 lines · Guenter Roeck

It is always a good idea to use paranthesis around macro parameters to avoid undesired side effects.

In this specific case, KELVIN_TO_CELSIUS() is used in drivers/platform/x86/asus-wmi.c with parameter "value & 0xFFFF", which due to operator evaluation order causes more or less random results.

Maybe it's better to use a statement expression to avoid multiple calculations of t

Quoted 9 lines

Maybe;

#define KELVIN_TO_CELSIUS(t) \

({							\
	long _t = (long)(t) - 2732;			\
	_t >= 0 ? (_t + 5) / 10 : (_t - 5) / 10;	\
})
Quoted 4 lines

Possibly, but I only want to have a bug fixed which should possibly be applied to -stable at some point. As such, I prefer the minimalistic approach. Anything more fancy on top of that can be submitted as separate patch.

I agreed. I will take this patch and push it for stable kernel.

thanks,
rui