public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: linux-kernel@vger.kernel.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 v2 0/4] Add ARRAY_END(), and use it to fix off-by-one bugs
Date: Sat, 8 Nov 2025 23:20:19 +0100	[thread overview]
Message-ID: <cover.1762637046.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

	a + ARRAY_SIZE(a) - 1

instead, with <= instead of <.  This is bogus, as it doesn't scale down
to arrays of 0 elements.  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).

---

Hi!

This v2 has the following changes compared to v1:

-  Rebase on top of v6.18-rc4.
-  Rename ENDOF() => ARRAY_END(), for consistency with ARRAY_SIZE().
   [Reported-by: Andrew Morton]
-  Add a useful cover letter.  [Reported-by: Andrew Morton]
-  Add a fourth commit, replacing all cases of a+ARRAY_SIZE(a) in mm/ by
   ARRAY_END(a).

See the range-diff below.


Have a lovely day!
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

 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 ++--
 6 files changed, 14 insertions(+), 8 deletions(-)

Range-diff against v1:
1:  90ab26558138 ! 1:  35255c1ceb54 array_size.h: Add ENDOF()
    @@ Metadata
     Author: Alejandro Colomar <alx@kernel.org>
     
      ## Commit message ##
    -    array_size.h: Add ENDOF()
    +    array_size.h: Add ARRAY_END()
     
    -    This macro is useful to calculate the second argument to
    -    sprintf_trunc_end(), avoiding off-by-one bugs.
    +    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 < ENDOF(a); p++)
    +                    ...
     
         Cc: Kees Cook <kees@kernel.org>
         Cc: Christopher Bazley <chris.bazley.wg14@gmail.com>
    @@ include/linux/array_size.h
      #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
      
     +/**
    -+ * ENDOF - get a pointer to one past the last element in array @a
    ++ * ARRAY_END - get a pointer to one past the last element in array @a
     + * @a: array
     + */
    -+#define ENDOF(a)  (a + ARRAY_SIZE(a))
    ++#define ARRAY_END(a)  (a + ARRAY_SIZE(a))
     +
      #endif  /* _LINUX_ARRAY_SIZE_H */
2:  ec7553aa4747 ! 2:  acd8bcbb05d3 mm: Fix benign off-by-one bugs
    @@ mm/kfence/kfence_test.c: static bool report_matches(const struct expect_report *
        /* Title */
        cur = expect[0];
     -  end = &expect[0][sizeof(expect[0]) - 1];
    -+  end = ENDOF(expect[0]);
    ++  end = ARRAY_END(expect[0]);
        switch (r->type) {
        case KFENCE_ERROR_OOB:
                cur += scnprintf(cur, end - cur, "BUG: KFENCE: out-of-bounds %s",
    @@ mm/kfence/kfence_test.c: static bool report_matches(const struct expect_report *
        /* Access information */
        cur = expect[1];
     -  end = &expect[1][sizeof(expect[1]) - 1];
    -+  end = ENDOF(expect[1]);
    ++  end = ARRAY_END(expect[1]);
      
        switch (r->type) {
        case KFENCE_ERROR_OOB:
    @@ mm/kmsan/kmsan_test.c: static bool report_matches(const struct expect_report *r)
        /* Title */
        cur = expected_header;
     -  end = &expected_header[sizeof(expected_header) - 1];
    -+  end = ENDOF(expected_header);
    ++  end = ARRAY_END(expected_header);
      
        cur += scnprintf(cur, end - cur, "BUG: KMSAN: %s", r->error_type);
      
3:  c94e42e85c13 ! 3:  781cce547eb2 kernel: Fix off-by-one benign bugs
    @@ kernel/kcsan/kcsan_test.c: static bool __report_matches(const struct expect_repo
        /* Title */
        cur = expect[0];
     -  end = &expect[0][sizeof(expect[0]) - 1];
    -+  end = ENDOF(expect[0]);
    ++  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) {
    @@ kernel/kcsan/kcsan_test.c: static bool __report_matches(const struct expect_repo
        /* Access 1 */
        cur = expect[1];
     -  end = &expect[1][sizeof(expect[1]) - 1];
    -+  end = ENDOF(expect[1]);
    ++  end = ARRAY_END(expect[1]);
        if (!r->access[1].fn)
                cur += scnprintf(cur, end - cur, "race at unknown origin, with ");
      
-:  ------------ > 4:  094878542457 mm: Use ARRAY_END() instead of open-coding it

base-commit: 6146a0f1dfae5d37442a9ddcba012add260bceb0
-- 
2.51.0


  parent reply	other threads:[~2025-11-08 22:20 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 ` Alejandro Colomar [this message]
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 ` [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
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.1762637046.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@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