scripts/coccinelle: Add script for using ARRAY_END()

Recently, we added an ARRAY_END() macro to simplify finding a pointer
one past the last element in an array.  Such a pointer is often called
the 'end', and thus the macro name.

Make it easy to find more places where that macro should be used.

See also:
436debc9ca ("array_size.h: add ARRAY_END()")
8118f197b7 ("mm: fix benign off-by-one bugs")
a9e5620c9a ("kernel: fix off-by-one benign bugs")
61e9210e23 ("mm: use ARRAY_END() instead of open-coding it")

Cc: Kees Cook <kees@kernel.org>
Cc: Markus Elfring <Markus.Elfring@web.de>
Cc: Julia Lawall <Julia.Lawall@inria.fr>
Cc: Nicolas Palix <nicolas.palix@imag.fr>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
This commit is contained in:
Alejandro Colomar 2026-09-22 21:34:09 +02:00 committed by Julia Lawall
parent 6628e7d4c6
commit 6b828a87a6

View File

@ -0,0 +1,73 @@
// SPDX-License-Identifier: GPL-2.0-only
/// Use ARRAY_END instead of an expression derived from ARRAY_SIZE
//
// Confidence: High
// Copyright: 2026, Alejandro Colomar <alx@kernel.org>
// Options: --no-includes --include-headers
virtual patch
virtual context
virtual org
virtual report
@depends on context@
type T;
T[] a;
expression b;
@@
(
* (a + ARRAY_SIZE(a))
|
* (&a[0] + ARRAY_SIZE(a))
|
* (&a[ARRAY_SIZE(a)])
|
* (&a[ARRAY_SIZE(a) - b])
)
@depends on patch@
type T;
T[] a;
expression b;
@@
(
- (a + ARRAY_SIZE(a))
+ ARRAY_END(a)
|
- (&a[0] + ARRAY_SIZE(a))
+ ARRAY_END(a)
|
- (&a[ARRAY_SIZE(a)])
+ ARRAY_END(a)
|
- (&a[ARRAY_SIZE(a) - b])
+ ARRAY_END(a) - b
)
@r depends on org || report@
type T;
T[] a;
expression b;
position p;
@@
(
(a@p + ARRAY_SIZE(a))
|
(&a[0]@p + ARRAY_SIZE(a))
|
(&a[ARRAY_SIZE(a)]@p)
|
(&a[ARRAY_SIZE(a)@p - b])
)
@script:python depends on org@
p << r.p;
@@
coccilib.org.print_todo(p[0], "WARNING should use ARRAY_END")
@script:python depends on report@
p << r.p;
@@
coccilib.report.print_report(p[0], "WARNING: opportunity for ARRAY_END")