* [PATCH] Make sure abs() always works on a signed argument
@ 2010-07-28 12:44 Michal Januszewski
2010-07-28 13:44 ` Andreas Schwab
0 siblings, 1 reply; 3+ messages in thread
From: Michal Januszewski @ 2010-07-28 12:44 UTC (permalink / raw)
To: linux-kernel
The abs() macro is commonly used to calculate the modulus of a difference
of two numbers. With the introduction of "long" inside the definition
of abs() in the commit a49c59c042c63b432307c1bbf7dac5a104c786e6, some of
the abs() calls in the kernel started returning unexpected values (for
instance, see abs() usage in drivers/video/modedb.c and the resulting
problems reported in https://bugs.gentoo.org/show_bug.cgi?id=296539).
The problem is apparent if the argument of abs() is a difference of two
32-bit integers, at least one of which is unsigned. The result is then
assumed to be an unsigned integer, which gets cast to a positive long. The
return value of abs() is then this large positive integer, instead of the
expected small positive integer representing the modulus of the argument.
Example:
u32 a = 0, b = 1;
u32 c = abs(a - b);
'c' will end up with a value of 0xffffffff instead of the expected 0x1.
To fix this problem, explicitly cast the argument of abs() to signed, so
that it gets properly expanded to long in case the original argument was
an int.
Signed-off-by: Michal Januszewski <michalj@gmail.com>
--
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 8317ec4..29fd43e 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -158,7 +158,7 @@ extern int _cond_resched(void);
#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
#define abs(x) ({ \
- long __x = (x); \
+ long __x = (signed)(x); \
(__x < 0) ? -__x : __x; \
})
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] Make sure abs() always works on a signed argument
2010-07-28 12:44 [PATCH] Make sure abs() always works on a signed argument Michal Januszewski
@ 2010-07-28 13:44 ` Andreas Schwab
2010-07-28 15:28 ` [PATCH] Make sure abs() works as expected with both ints and longs Michal Januszewski
0 siblings, 1 reply; 3+ messages in thread
From: Andreas Schwab @ 2010-07-28 13:44 UTC (permalink / raw)
To: michalj; +Cc: linux-kernel
Michal Januszewski <michalj@gmail.com> writes:
> To fix this problem, explicitly cast the argument of abs() to signed, so
> that it gets properly expanded to long in case the original argument was
> an int.
This will of course re-introduce the truncation, and effectively reverts
a49c59c0.
Andreas.
--
Andreas Schwab, schwab@redhat.com
GPG Key fingerprint = D4E8 DBE3 3813 BB5D FA84 5EC7 45C6 250E 6F00 984E
"And now for something completely different."
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH] Make sure abs() works as expected with both ints and longs
2010-07-28 13:44 ` Andreas Schwab
@ 2010-07-28 15:28 ` Michal Januszewski
0 siblings, 0 replies; 3+ messages in thread
From: Michal Januszewski @ 2010-07-28 15:28 UTC (permalink / raw)
To: Andreas Schwab; +Cc: linux-kernel
The abs() macro is commonly used to calculate the modulus of a difference
of two numbers. With the introduction of "long" inside the definition
of abs() in the commit a49c59c042c63b432307c1bbf7dac5a104c786e6, some of
the abs() calls in the kernel started returning unexpected values (for
instance, see abs() usage in drivers/video/modedb.c).
The problem is apparent if the argument of abs() is a difference of two
32-bit integers, at least one of which is unsigned. The result is then
assumed to be an unsigned integer, which gets cast to a positive long. The
return value of abs() is then this large positive integer, instead of the
expected small positive integer representing the modulus of the argument.
Example:
u32 a = 0, b = 1;
u32 c = abs(a - b);
'c' will end up with a value of 0xffffffff instead of the expected 0x1.
To fix this problem, modify the abs() macro so that it detects the size
of the argument, and if it's not larger than 32 bits, uses an int instead
of a long.
Signed-off-by: Michal Januszewski <michalj@gmail.com>
---
OK, please disregard my previous patch. How about this new one?
A solution with sizeof() was mentioned as a possibility in the original
discussion of a49c59c0, but it looks like it was never actually
presented and considered.
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 8317ec4..bacefff 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -157,10 +157,14 @@ extern int _cond_resched(void);
#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
-#define abs(x) ({ \
- long __x = (x); \
- (__x < 0) ? -__x : __x; \
- })
+#define abs(x) ( \
+ (sizeof(x) <= 4) ? ({ \
+ int __x = (x); \
+ (__x < 0) ? -__x : __x; \
+ }) : ({ \
+ long __x = (x); \
+ (__x < 0) ? -__x : __x; \
+ }))
#ifdef CONFIG_PROVE_LOCKING
void might_fault(void);
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2010-07-28 15:28 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-28 12:44 [PATCH] Make sure abs() always works on a signed argument Michal Januszewski
2010-07-28 13:44 ` Andreas Schwab
2010-07-28 15:28 ` [PATCH] Make sure abs() works as expected with both ints and longs Michal Januszewski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox