* [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs
[not found] <cover.1758806023.git.alx@kernel.org>
@ 2025-11-09 18:06 ` Alejandro Colomar
2025-11-09 18:06 ` [PATCH v3 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
` (3 more replies)
2025-11-09 19:45 ` [PATCH v4 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
` (4 subsequent siblings)
5 siblings, 4 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 18:06 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 1/4] array_size.h: Add ARRAY_END()
2025-11-09 18:06 ` [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
@ 2025-11-09 18:06 ` Alejandro Colomar
2025-11-09 19:05 ` Maciej W. Rozycki
2025-11-09 18:06 ` [PATCH v3 2/4] mm: Fix benign off-by-one bugs Alejandro Colomar
` (2 subsequent siblings)
3 siblings, 1 reply; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 18:06 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton
ARRAY_END() returns a pointer one past the end of the last element in
the array argument. This pointer is useful for iterating over the
elements of an array:
for (T *p = a, p < ARRAY_END(a); p++)
...
Cc: Kees Cook <kees@kernel.org>
Cc: Christopher Bazley <chris.bazley.wg14@gmail.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Marco Elver <elver@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Message-ID: <37b1088dbd01a21d2f9d460aa510726119b3bcb0.1752193588.git.alx@kernel.org>
---
drivers/block/floppy.c | 2 --
include/linux/array_size.h | 6 ++++++
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index 5336c3c5ca36..69661840397e 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -4802,8 +4802,6 @@ 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;
diff --git a/include/linux/array_size.h b/include/linux/array_size.h
index 06d7d83196ca..b5775b8f13de 100644
--- a/include/linux/array_size.h
+++ b/include/linux/array_size.h
@@ -10,4 +10,10 @@
*/
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+/**
+ * ARRAY_END - get a pointer to one past the last element in array @a
+ * @a: array
+ */
+#define ARRAY_END(a) (&(a)[ARRAY_SIZE(a)])
+
#endif /* _LINUX_ARRAY_SIZE_H */
--
2.51.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v3 2/4] mm: Fix benign off-by-one bugs
2025-11-09 18:06 ` [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
2025-11-09 18:06 ` [PATCH v3 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
@ 2025-11-09 18:06 ` 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
3 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 18:06 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton
We were wasting a byte due to an off-by-one bug. s[c]nprintf()
doesn't write more than $2 bytes including the null byte, so trying to
pass 'size-1' there is wasting one byte.
Acked-by: Marco Elver <elver@google.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Christopher Bazley <chris.bazley.wg14@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Marco Elver <elver@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Message-ID: <515445ae064d4b8599899bf0d8b480dadd2ff843.1752182685.git.alx@kernel.org>
---
mm/kfence/kfence_test.c | 4 ++--
mm/kmsan/kmsan_test.c | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c
index 00034e37bc9f..5725a367246d 100644
--- a/mm/kfence/kfence_test.c
+++ b/mm/kfence/kfence_test.c
@@ -110,7 +110,7 @@ static bool report_matches(const struct expect_report *r)
/* Title */
cur = expect[0];
- end = &expect[0][sizeof(expect[0]) - 1];
+ end = ARRAY_END(expect[0]);
switch (r->type) {
case KFENCE_ERROR_OOB:
cur += scnprintf(cur, end - cur, "BUG: KFENCE: out-of-bounds %s",
@@ -140,7 +140,7 @@ static bool report_matches(const struct expect_report *r)
/* Access information */
cur = expect[1];
- end = &expect[1][sizeof(expect[1]) - 1];
+ end = ARRAY_END(expect[1]);
switch (r->type) {
case KFENCE_ERROR_OOB:
diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c
index 902ec48b1e3e..b5ad5dfb2c00 100644
--- a/mm/kmsan/kmsan_test.c
+++ b/mm/kmsan/kmsan_test.c
@@ -105,7 +105,7 @@ static bool report_matches(const struct expect_report *r)
/* Title */
cur = expected_header;
- end = &expected_header[sizeof(expected_header) - 1];
+ end = ARRAY_END(expected_header);
cur += scnprintf(cur, end - cur, "BUG: KMSAN: %s", r->error_type);
--
2.51.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v3 3/4] kernel: Fix off-by-one benign bugs
2025-11-09 18:06 ` [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
2025-11-09 18:06 ` [PATCH v3 1/4] array_size.h: Add ARRAY_END() 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 ` Alejandro Colomar
2025-11-09 18:07 ` [PATCH v3 4/4] mm: Use ARRAY_END() instead of open-coding it Alejandro Colomar
3 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 18:07 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton
We were wasting a byte due to an off-by-one bug. s[c]nprintf()
doesn't write more than $2 bytes including the null byte, so trying to
pass 'size-1' there is wasting one byte.
This is essentially the same as the previous commit, in a different
file.
Cc: Marco Elver <elver@google.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Christopher Bazley <chris.bazley.wg14@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Marco Elver <elver@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
kernel/kcsan/kcsan_test.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c
index 219d22857c98..8ef8167be745 100644
--- a/kernel/kcsan/kcsan_test.c
+++ b/kernel/kcsan/kcsan_test.c
@@ -176,7 +176,7 @@ static bool __report_matches(const struct expect_report *r)
/* Title */
cur = expect[0];
- end = &expect[0][sizeof(expect[0]) - 1];
+ end = ARRAY_END(expect[0]);
cur += scnprintf(cur, end - cur, "BUG: KCSAN: %s in ",
is_assert ? "assert: race" : "data-race");
if (r->access[1].fn) {
@@ -200,7 +200,7 @@ static bool __report_matches(const struct expect_report *r)
/* Access 1 */
cur = expect[1];
- end = &expect[1][sizeof(expect[1]) - 1];
+ end = ARRAY_END(expect[1]);
if (!r->access[1].fn)
cur += scnprintf(cur, end - cur, "race at unknown origin, with ");
--
2.51.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v3 4/4] mm: Use ARRAY_END() instead of open-coding it
2025-11-09 18:06 ` [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
` (2 preceding siblings ...)
2025-11-09 18:07 ` [PATCH v3 3/4] kernel: Fix off-by-one benign bugs Alejandro Colomar
@ 2025-11-09 18:07 ` Alejandro Colomar
3 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 18:07 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton
Cc: Kees Cook <kees@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
mm/kmemleak.c | 2 +-
mm/memcontrol-v1.c | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 1ac56ceb29b6..fe33f2edfe07 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -510,7 +510,7 @@ static void mem_pool_free(struct kmemleak_object *object)
{
unsigned long flags;
- if (object < mem_pool || object >= mem_pool + ARRAY_SIZE(mem_pool)) {
+ if (object < mem_pool || object >= ARRAY_END(mem_pool)) {
kmem_cache_free(object_cache, object);
return;
}
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 6eed14bff742..b2f37bd939fa 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1794,7 +1794,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v)
mem_cgroup_flush_stats(memcg);
- for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
+ for (stat = stats; stat < ARRAY_END(stats); stat++) {
seq_printf(m, "%s=%lu", stat->name,
mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
false));
@@ -1805,7 +1805,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v)
seq_putc(m, '\n');
}
- for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
+ for (stat = stats; stat < ARRAY_END(stats); stat++) {
seq_printf(m, "hierarchical_%s=%lu", stat->name,
mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
--
2.51.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v3 1/4] array_size.h: Add ARRAY_END()
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
0 siblings, 1 reply; 12+ messages in thread
From: Maciej W. Rozycki @ 2025-11-09 19:05 UTC (permalink / raw)
To: Alejandro Colomar
Cc: linux-kernel, linux-mm, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton
On Sun, 9 Nov 2025, Alejandro Colomar wrote:
> diff --git a/include/linux/array_size.h b/include/linux/array_size.h
> index 06d7d83196ca..b5775b8f13de 100644
> --- a/include/linux/array_size.h
> +++ b/include/linux/array_size.h
> @@ -10,4 +10,10 @@
> */
> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
>
> +/**
> + * ARRAY_END - get a pointer to one past the last element in array @a
> + * @a: array
> + */
> +#define ARRAY_END(a) (&(a)[ARRAY_SIZE(a)])
Why `a' rather than `arr' as with the other macro?
Maciej
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3 1/4] array_size.h: Add ARRAY_END()
2025-11-09 19:05 ` Maciej W. Rozycki
@ 2025-11-09 19:18 ` Alejandro Colomar
0 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 19:18 UTC (permalink / raw)
To: Maciej W. Rozycki
Cc: linux-kernel, linux-mm, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton
[-- Attachment #1: Type: text/plain, Size: 979 bytes --]
Hi Maciej,
On Sun, Nov 09, 2025 at 07:05:40PM +0000, Maciej W. Rozycki wrote:
> On Sun, 9 Nov 2025, Alejandro Colomar wrote:
>
> > diff --git a/include/linux/array_size.h b/include/linux/array_size.h
> > index 06d7d83196ca..b5775b8f13de 100644
> > --- a/include/linux/array_size.h
> > +++ b/include/linux/array_size.h
> > @@ -10,4 +10,10 @@
> > */
> > #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
> >
> > +/**
> > + * ARRAY_END - get a pointer to one past the last element in array @a
> > + * @a: array
> > + */
> > +#define ARRAY_END(a) (&(a)[ARRAY_SIZE(a)])
>
> Why `a' rather than `arr' as with the other macro?
It's simpler, and equally informative. I wish ARRAY_SIZE() would have
also used 'a'. On the other hand, consistency is important. I'll make
it 'arr'.
Thanks!
Have a lovely night!
Alex
>
> Maciej
--
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v4 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs
[not found] <cover.1758806023.git.alx@kernel.org>
2025-11-09 18:06 ` [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
@ 2025-11-09 19:45 ` Alejandro Colomar
2025-11-09 19:45 ` [PATCH v4 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
` (3 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 19:45 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton, Maciej W. Rozycki
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
(it produces Undefined Behavior). That would be a footgun. Such arrays
don't exist per the ISO 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,
v4:
- Use 'arr' as the macro parameter, for consistency with ARRAY_SIZE().
[Reported-by: "Maciej W. Rozycki" <macro@orcam.me.uk>]
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 v3:
1: 2cb4ddff93b3 ! 1: 9f87d6208a6c array_size.h: Add ARRAY_END()
@@ Commit message
Cc: Michal Hocko <mhocko@suse.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
+ Cc: "Maciej W. Rozycki" <macro@orcam.me.uk>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Message-ID: <37b1088dbd01a21d2f9d460aa510726119b3bcb0.1752193588.git.alx@kernel.org>
@@ include/linux/array_size.h
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+/**
-+ * ARRAY_END - get a pointer to one past the last element in array @a
-+ * @a: array
++ * ARRAY_END - get a pointer to one past the last element in array @arr
++ * @arr: array
+ */
-+#define ARRAY_END(a) (&(a)[ARRAY_SIZE(a)])
++#define ARRAY_END(arr) (&(arr)[ARRAY_SIZE(arr)])
+
#endif /* _LINUX_ARRAY_SIZE_H */
2: 831155f02bec = 2: ac55d92551e4 mm: Fix benign off-by-one bugs
3: d8128f0c8b9f = 3: ca8dec7f5bc9 kernel: Fix off-by-one benign bugs
4: 9646a1d194a5 = 4: 980c8fe8a6de mm: Use ARRAY_END() instead of open-coding it
base-commit: 6146a0f1dfae5d37442a9ddcba012add260bceb0
--
2.51.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v4 1/4] array_size.h: Add ARRAY_END()
[not found] <cover.1758806023.git.alx@kernel.org>
2025-11-09 18:06 ` [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs 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 ` Alejandro Colomar
2025-11-09 19:45 ` [PATCH v4 2/4] mm: Fix benign off-by-one bugs Alejandro Colomar
` (2 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 19:45 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton, Maciej W. Rozycki
ARRAY_END() returns a pointer one past the end of the last element in
the array argument. This pointer is useful for iterating over the
elements of an array:
for (T *p = a, p < ARRAY_END(a); p++)
...
Cc: Kees Cook <kees@kernel.org>
Cc: Christopher Bazley <chris.bazley.wg14@gmail.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Marco Elver <elver@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: "Maciej W. Rozycki" <macro@orcam.me.uk>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Message-ID: <37b1088dbd01a21d2f9d460aa510726119b3bcb0.1752193588.git.alx@kernel.org>
---
drivers/block/floppy.c | 2 --
include/linux/array_size.h | 6 ++++++
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index 5336c3c5ca36..69661840397e 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -4802,8 +4802,6 @@ 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;
diff --git a/include/linux/array_size.h b/include/linux/array_size.h
index 06d7d83196ca..0c4fec98822e 100644
--- a/include/linux/array_size.h
+++ b/include/linux/array_size.h
@@ -10,4 +10,10 @@
*/
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+/**
+ * ARRAY_END - get a pointer to one past the last element in array @arr
+ * @arr: array
+ */
+#define ARRAY_END(arr) (&(arr)[ARRAY_SIZE(arr)])
+
#endif /* _LINUX_ARRAY_SIZE_H */
--
2.51.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v4 2/4] mm: Fix benign off-by-one bugs
[not found] <cover.1758806023.git.alx@kernel.org>
` (2 preceding siblings ...)
2025-11-09 19:45 ` [PATCH v4 1/4] array_size.h: Add ARRAY_END() Alejandro Colomar
@ 2025-11-09 19:45 ` 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
5 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 19:45 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton, Maciej W. Rozycki
We were wasting a byte due to an off-by-one bug. s[c]nprintf()
doesn't write more than $2 bytes including the null byte, so trying to
pass 'size-1' there is wasting one byte.
Acked-by: Marco Elver <elver@google.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Christopher Bazley <chris.bazley.wg14@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Marco Elver <elver@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Message-ID: <515445ae064d4b8599899bf0d8b480dadd2ff843.1752182685.git.alx@kernel.org>
---
mm/kfence/kfence_test.c | 4 ++--
mm/kmsan/kmsan_test.c | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c
index 00034e37bc9f..5725a367246d 100644
--- a/mm/kfence/kfence_test.c
+++ b/mm/kfence/kfence_test.c
@@ -110,7 +110,7 @@ static bool report_matches(const struct expect_report *r)
/* Title */
cur = expect[0];
- end = &expect[0][sizeof(expect[0]) - 1];
+ end = ARRAY_END(expect[0]);
switch (r->type) {
case KFENCE_ERROR_OOB:
cur += scnprintf(cur, end - cur, "BUG: KFENCE: out-of-bounds %s",
@@ -140,7 +140,7 @@ static bool report_matches(const struct expect_report *r)
/* Access information */
cur = expect[1];
- end = &expect[1][sizeof(expect[1]) - 1];
+ end = ARRAY_END(expect[1]);
switch (r->type) {
case KFENCE_ERROR_OOB:
diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c
index 902ec48b1e3e..b5ad5dfb2c00 100644
--- a/mm/kmsan/kmsan_test.c
+++ b/mm/kmsan/kmsan_test.c
@@ -105,7 +105,7 @@ static bool report_matches(const struct expect_report *r)
/* Title */
cur = expected_header;
- end = &expected_header[sizeof(expected_header) - 1];
+ end = ARRAY_END(expected_header);
cur += scnprintf(cur, end - cur, "BUG: KMSAN: %s", r->error_type);
--
2.51.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v4 3/4] kernel: Fix off-by-one benign bugs
[not found] <cover.1758806023.git.alx@kernel.org>
` (3 preceding siblings ...)
2025-11-09 19:45 ` [PATCH v4 2/4] mm: Fix benign off-by-one bugs Alejandro Colomar
@ 2025-11-09 19:45 ` Alejandro Colomar
2025-11-09 19:45 ` [PATCH v4 4/4] mm: Use ARRAY_END() instead of open-coding it Alejandro Colomar
5 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 19:45 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton, Maciej W. Rozycki
We were wasting a byte due to an off-by-one bug. s[c]nprintf()
doesn't write more than $2 bytes including the null byte, so trying to
pass 'size-1' there is wasting one byte.
This is essentially the same as the previous commit, in a different
file.
Cc: Marco Elver <elver@google.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Christopher Bazley <chris.bazley.wg14@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Marco Elver <elver@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
kernel/kcsan/kcsan_test.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c
index 219d22857c98..8ef8167be745 100644
--- a/kernel/kcsan/kcsan_test.c
+++ b/kernel/kcsan/kcsan_test.c
@@ -176,7 +176,7 @@ static bool __report_matches(const struct expect_report *r)
/* Title */
cur = expect[0];
- end = &expect[0][sizeof(expect[0]) - 1];
+ end = ARRAY_END(expect[0]);
cur += scnprintf(cur, end - cur, "BUG: KCSAN: %s in ",
is_assert ? "assert: race" : "data-race");
if (r->access[1].fn) {
@@ -200,7 +200,7 @@ static bool __report_matches(const struct expect_report *r)
/* Access 1 */
cur = expect[1];
- end = &expect[1][sizeof(expect[1]) - 1];
+ end = ARRAY_END(expect[1]);
if (!r->access[1].fn)
cur += scnprintf(cur, end - cur, "race at unknown origin, with ");
--
2.51.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v4 4/4] mm: Use ARRAY_END() instead of open-coding it
[not found] <cover.1758806023.git.alx@kernel.org>
` (4 preceding siblings ...)
2025-11-09 19:45 ` [PATCH v4 3/4] kernel: Fix off-by-one benign bugs Alejandro Colomar
@ 2025-11-09 19:45 ` Alejandro Colomar
5 siblings, 0 replies; 12+ messages in thread
From: Alejandro Colomar @ 2025-11-09 19:45 UTC (permalink / raw)
To: linux-kernel, linux-mm
Cc: Alejandro Colomar, Kees Cook, Christopher Bazley,
Rasmus Villemoes, Marco Elver, Michal Hocko, Linus Torvalds,
Al Viro, Alexander Potapenko, Dmitry Vyukov, Jann Horn,
Andrew Morton, Maciej W. Rozycki
Cc: Kees Cook <kees@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
mm/kmemleak.c | 2 +-
mm/memcontrol-v1.c | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 1ac56ceb29b6..fe33f2edfe07 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -510,7 +510,7 @@ static void mem_pool_free(struct kmemleak_object *object)
{
unsigned long flags;
- if (object < mem_pool || object >= mem_pool + ARRAY_SIZE(mem_pool)) {
+ if (object < mem_pool || object >= ARRAY_END(mem_pool)) {
kmem_cache_free(object_cache, object);
return;
}
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 6eed14bff742..b2f37bd939fa 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1794,7 +1794,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v)
mem_cgroup_flush_stats(memcg);
- for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
+ for (stat = stats; stat < ARRAY_END(stats); stat++) {
seq_printf(m, "%s=%lu", stat->name,
mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
false));
@@ -1805,7 +1805,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v)
seq_putc(m, '\n');
}
- for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
+ for (stat = stats; stat < ARRAY_END(stats); stat++) {
seq_printf(m, "hierarchical_%s=%lu", stat->name,
mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
--
2.51.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
end of thread, other threads:[~2025-11-09 19:49 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <cover.1758806023.git.alx@kernel.org>
2025-11-09 18:06 ` [PATCH v3 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs Alejandro Colomar
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
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).