[PATCH v1 1/3] array_size.h: Add ENDOF()

Alejandro Colomar posted 3 patches 6 days, 11 hours ago
[PATCH v1 1/3] array_size.h: Add ENDOF()
Posted by Alejandro Colomar 6 days, 11 hours ago
This macro is useful to calculate the second argument to
sprintf_trunc_end(), avoiding off-by-one bugs.

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>
---
 include/linux/array_size.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/array_size.h b/include/linux/array_size.h
index 06d7d83196ca..781bdb70d939 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))
 
+/**
+ * ENDOF - get a pointer to one past the last element in array @a
+ * @a: array
+ */
+#define ENDOF(a)  (a + ARRAY_SIZE(a))
+
 #endif  /* _LINUX_ARRAY_SIZE_H */
-- 
2.51.0
Re: [PATCH v1 1/3] array_size.h: Add ENDOF()
Posted by Alejandro Colomar 6 days, 11 hours ago
On Thu, Sep 25, 2025 at 03:20:37PM +0200, Alejandro Colomar wrote:
> This macro is useful to calculate the second argument to
> sprintf_trunc_end(), avoiding off-by-one bugs.

D'oh!  This commit message needs updating.  Please amend with:

	This macro is useful to calculate the end of an array (that is,
	a pointer to one after its last element), avoiding off-by-one
	bugs.

Cheers,
Alex

> 
> 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>
> ---
>  include/linux/array_size.h | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/include/linux/array_size.h b/include/linux/array_size.h
> index 06d7d83196ca..781bdb70d939 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))
>  
> +/**
> + * ENDOF - get a pointer to one past the last element in array @a
> + * @a: array
> + */
> +#define ENDOF(a)  (a + ARRAY_SIZE(a))
> +
>  #endif  /* _LINUX_ARRAY_SIZE_H */
> -- 
> 2.51.0
> 

-- 
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).