From: Alejandro Colomar <alx@kernel.org>
To: linux-kernel@vger.kernel.org, linux-mm@kvack.org
Cc: Alejandro Colomar <alx@kernel.org>, Kees Cook <kees@kernel.org>,
Christopher Bazley <chris.bazley.wg14@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Marco Elver <elver@google.com>, Michal Hocko <mhocko@suse.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
Al Viro <viro@zeniv.linux.org.uk>,
Alexander Potapenko <glider@google.com>,
Dmitry Vyukov <dvyukov@google.com>, Jann Horn <jannh@google.com>,
Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs
Date: Sun, 9 Nov 2025 19:06:49 +0100 [thread overview]
Message-ID: <cover.1762711279.git.alx@kernel.org> (raw)
In-Reply-To: <cover.1758806023.git.alx@kernel.org>
ARRAY_END() is a macro to calculate a pointer to one past the last
element of an array argument. This is a very common pointer, which is
used to iterate over all elements of an array:
for (T *p = a; p < ARRAY_END(a); p++)
...
Of course, this pointer should never be dereferenced. A pointer one
past the last element of an array should not be dereferenced; it's
perfectly fine to hold such a pointer --and a good thing to do--, but
the only thing it should be used for is comparing it with other pointers
derived from the same array.
Due to how special these pointers are, it would be good to use
consistent naming. It's common to name such a pointer 'end' --in fact,
we have many such cases in the kernel--. C++ even standardized this
name with std::end(). Let's try naming such pointers 'end', and try
also avoid using 'end' for pointers that are not the result of
ARRAY_END().
It has been incorrectly suggested that these pointers are dangerous, and
that they should never be used, suggesting to use something like
#define ARRAY_LAST(a) ((a) + ARRAY_SIZE(a) - 1)
for (T *p = a; p <= ARRAY_LAST(a); p++)
...
This is bogus, as it doesn't scale down to arrays of 0 elements. In the
case of an array of 0 elements, ARRAY_LAST() would underflow the
pointer, which not only it can't be dereferenced, it can't even be held.
That would be a footgun. Such arrays don't exist per the C standard;
however, GCC supports them as an extension (with partial support,
though; GCC has a few bugs which need to be fixed).
This patch set fixes a few places where it was intended to use the array
end (that is, one past the last element), but accidentally a pointer to
the last element was used instead, thus wasting one byte.
It also replaces other places where the array end was correctly
calculated with ARRAY_SIZE(), by using the simpler ARRAY_END().
Also, there was one drivers/ file that already defined this macro. We
remove that definition, to not conflict with this one.
---
Hi!
Changes in v3:
- Fix commit message.
- Remove old definition from "drivers/block/floppy.c".
[Reported-by: kernel test robot <lkp@intel.com>]
- Use definition of ARRAY_END() with array notation. There's work in
the C committee to make array notation slightly safer than pointer
arithmetic.
Have a lovely night!
Alex
Alejandro Colomar (4):
array_size.h: Add ARRAY_END()
mm: Fix benign off-by-one bugs
kernel: Fix off-by-one benign bugs
mm: Use ARRAY_END() instead of open-coding it
drivers/block/floppy.c | 2 --
include/linux/array_size.h | 6 ++++++
kernel/kcsan/kcsan_test.c | 4 ++--
mm/kfence/kfence_test.c | 4 ++--
mm/kmemleak.c | 2 +-
mm/kmsan/kmsan_test.c | 2 +-
mm/memcontrol-v1.c | 4 ++--
7 files changed, 14 insertions(+), 10 deletions(-)
Range-diff against v2:
1: 35255c1ceb54 ! 1: 2cb4ddff93b3 array_size.h: Add ARRAY_END()
@@ Commit message
the array argument. This pointer is useful for iterating over the
elements of an array:
- for (T *p = a, p < ENDOF(a); p++)
+ for (T *p = a, p < ARRAY_END(a); p++)
...
Cc: Kees Cook <kees@kernel.org>
@@ Commit message
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Message-ID: <37b1088dbd01a21d2f9d460aa510726119b3bcb0.1752193588.git.alx@kernel.org>
+ ## drivers/block/floppy.c ##
+@@ drivers/block/floppy.c: static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
+ }
+ }
+
+-#define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
+-
+ static int floppy_request_regions(int fdc)
+ {
+ const struct io_region *p;
+
## include/linux/array_size.h ##
@@
*/
@@ include/linux/array_size.h
+ * ARRAY_END - get a pointer to one past the last element in array @a
+ * @a: array
+ */
-+#define ARRAY_END(a) (a + ARRAY_SIZE(a))
++#define ARRAY_END(a) (&(a)[ARRAY_SIZE(a)])
+
#endif /* _LINUX_ARRAY_SIZE_H */
2: acd8bcbb05d3 = 2: 831155f02bec mm: Fix benign off-by-one bugs
3: 781cce547eb2 = 3: d8128f0c8b9f kernel: Fix off-by-one benign bugs
4: 094878542457 = 4: 9646a1d194a5 mm: Use ARRAY_END() instead of open-coding it
--
2.51.0
next prev parent reply other threads:[~2025-11-09 18:06 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-25 13:20 [PATCH v1 0/3] Add ENDOF(), and use it to fix off-by-one bugs Alejandro Colomar
2025-09-25 13:20 ` [PATCH v1 1/3] array_size.h: Add ENDOF() Alejandro Colomar
2025-09-25 13:24 ` Alejandro Colomar
2025-09-25 13:20 ` [PATCH v1 2/3] mm: Fix benign off-by-one bugs Alejandro Colomar
2025-11-10 17:47 ` kernel test robot
2025-11-10 22:06 ` Alejandro Colomar
2025-11-10 18:40 ` kernel test robot
2025-11-11 10:42 ` kernel test robot
2025-09-25 13:20 ` [PATCH v1 3/3] kernel: Fix off-by-one benign bugs Alejandro Colomar
2025-09-25 20:48 ` [PATCH v1 0/3] Add ENDOF(), and use it to fix off-by-one bugs Andrew Morton
2025-09-26 0:00 ` Kees Cook
[not found] ` <CAHk-=wg2M+v5wFQLK3u3DuchpCbuHF8Z7_if3=foECVRXF+8vg@mail.gmail.com>
2025-09-26 1:31 ` Kees Cook
2025-09-26 2:36 ` Linus Torvalds
2025-09-26 3:37 ` Kees Cook
2025-09-26 13:07 ` Alejandro Colomar
2025-09-26 8:29 ` Alejandro Colomar
2025-09-26 9:00 ` Alejandro Colomar
2025-11-08 22:20 ` [PATCH v2 0/4] Add ARRAY_END(), " Alejandro Colomar
2025-11-08 22:20 ` [PATCH v2 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
2025-11-09 10:43 ` kernel test robot
2025-11-09 12:30 ` Alejandro Colomar
2025-11-09 13:14 ` kernel test robot
2025-11-08 22:20 ` [PATCH v2 2/4] mm: Fix benign off-by-one bugs Alejandro Colomar
2025-11-08 22:20 ` [PATCH v2 3/4] kernel: Fix off-by-one benign bugs Alejandro Colomar
2025-11-08 22:20 ` [PATCH v2 4/4] mm: Use ARRAY_END() instead of open-coding it Alejandro Colomar
2025-11-09 18:06 ` Alejandro Colomar [this message]
2025-11-09 18:06 ` [PATCH v3 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
2025-11-09 19:05 ` Maciej W. Rozycki
2025-11-09 19:18 ` Alejandro Colomar
2025-11-09 18:06 ` [PATCH v3 2/4] mm: Fix benign off-by-one bugs Alejandro Colomar
2025-11-09 18:07 ` [PATCH v3 3/4] kernel: Fix off-by-one benign bugs Alejandro Colomar
2025-11-09 18:07 ` [PATCH v3 4/4] mm: Use ARRAY_END() instead of open-coding it Alejandro Colomar
2025-11-09 19:45 ` [PATCH v4 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
2025-11-09 19:45 ` [PATCH v4 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
2025-11-09 19:45 ` [PATCH v4 2/4] mm: Fix benign off-by-one bugs Alejandro Colomar
2025-11-09 19:45 ` [PATCH v4 3/4] kernel: Fix off-by-one benign bugs Alejandro Colomar
2025-11-09 19:45 ` [PATCH v4 4/4] mm: Use ARRAY_END() instead of open-coding it Alejandro Colomar
2025-12-10 22:46 ` [PATCH v5 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
2025-12-10 22:46 ` [PATCH v5 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
2025-12-10 22:46 ` [PATCH v5 2/4] mm: Fix benign off-by-one bugs Alejandro Colomar
2025-12-10 22:46 ` [PATCH v5 3/4] kernel: Fix off-by-one benign bugs Alejandro Colomar
2025-12-10 22:46 ` [PATCH v5 4/4] mm: Use ARRAY_END() instead of open-coding it Alejandro Colomar
2025-12-10 23:18 ` Kees Cook
2025-12-11 0:21 ` Alejandro Colomar
2025-12-11 1:37 ` Kees Cook
2025-12-21 14:07 ` Alejandro Colomar
2025-12-22 23:21 ` Kees Cook
2025-12-23 1:07 ` Alejandro Colomar
2025-12-11 10:43 ` [PATCH v6 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
2025-12-11 10:43 ` [PATCH v6 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
2025-12-11 10:43 ` [PATCH v6 2/4] mm: Fix benign off-by-one bugs Alejandro Colomar
2025-12-11 10:44 ` [PATCH v6 3/4] kernel: Fix off-by-one benign bugs Alejandro Colomar
2025-12-11 10:44 ` [PATCH v6 4/4] mm: Use ARRAY_END() instead of open-coding it Alejandro Colomar
2026-02-08 20:10 ` SeongJae Park
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cover.1762711279.git.alx@kernel.org \
--to=alx@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=chris.bazley.wg14@gmail.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=jannh@google.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mhocko@suse.com \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox