Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1703426 > unrolled thread

[PATCH v2] bitops: Avoid integer overflow in GENMASK(_ULL)

Started byMatthias Kaehlcke <mka@chromium.org>
First post2017-08-03 23:30 +0200
Last post2017-08-03 23:50 +0200
Articles 2 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH v2] bitops: Avoid integer overflow in GENMASK(_ULL) Matthias Kaehlcke <mka@chromium.org> - 2017-08-03 23:30 +0200
    Re: [PATCH v2] bitops: Avoid integer overflow in GENMASK(_ULL) Andrew Morton <akpm@linux-foundation.org> - 2017-08-03 23:50 +0200

#1703426 — [PATCH v2] bitops: Avoid integer overflow in GENMASK(_ULL)

FromMatthias Kaehlcke <mka@chromium.org>
Date2017-08-03 23:30 +0200
Subject[PATCH v2] bitops: Avoid integer overflow in GENMASK(_ULL)
Message-ID<uavQR-5Ka-7@gated-at.bofh.it>
GENMASK(_ULL) performs a left-shift of ~0UL(L), which technically
results in an integer overflow. clang raises a warning if the overflow
occurs in a preprocessor expression. Clear the low-order bits through
a substraction instead of the left-shift to avoid the overflow.

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---
Changes in v2:
- use substraction instead of left-shift, get rid off odd right-shift
- also change GENMASK
- updated commit message

 include/linux/bitops.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index a83c822c35c2..8fbe259b197c 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -19,10 +19,11 @@
  * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
  */
 #define GENMASK(h, l) \
-	(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
+	(((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
 
 #define GENMASK_ULL(h, l) \
-	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
+	(((~0ULL) - (1ULL << (l)) + 1) & \
+	 (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
 
 extern unsigned int __sw_hweight8(unsigned int w);
 extern unsigned int __sw_hweight16(unsigned int w);
-- 
2.14.0.rc1.383.gd1ce394fe2-goog

[toc] | [next] | [standalone]


#1703448

FromAndrew Morton <akpm@linux-foundation.org>
Date2017-08-03 23:50 +0200
Message-ID<uawad-5UG-13@gated-at.bofh.it>
In reply to#1703426
On Thu,  3 Aug 2017 14:20:20 -0700 Matthias Kaehlcke <mka@chromium.org> wrote:

> GENMASK(_ULL) performs a left-shift of ~0UL(L), which technically
> results in an integer overflow. clang raises a warning if the overflow
> occurs in a preprocessor expression.

That's just irritaing.

> Clear the low-order bits through
> a substraction instead of the left-shift to avoid the overflow.

There's no change in .text size for a few files I tested so guess we
can live with it.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web