Re: [cocci] [PATCH v3] scripts/coccinelle: Add script for using ARRAY_END()
Alejandro Colomar <[email protected]> Sun, 15 Mar 2026 23:12:14 +0100
| Newsgroups | fr.inria.cocci |
|---|---|
| Message-ID | <abcusyeK0YEyYcOa@devuan> |
On 2026-03-15T23:11:03+0100, Alejandro Colomar wrote:
> 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:
> 436debc9cad8 ("array_size.h: add ARRAY_END()")
> 8118f197b7b7 ("mm: fix benign off-by-one bugs")
> a9e5620c9a9e ("kernel: fix off-by-one benign bugs")
> 61e9210e2392 ("mm: use ARRAY_END() instead of open-coding it")
>
> Cc: Kees Cook <[email protected]>
> Cc: Markus Elfring <[email protected]>
> Cc: Julia Lawall <[email protected]>
> Cc: Nicolas Palix <[email protected]>
> Signed-off-by: Alejandro Colomar <[email protected]>
> ---
>
> Hi!
>
> This v3 only has one change: I've removed the comment about the false
> negatives. They were due to spatch(1) not being able to correctly parse
> unrelated code elsewhere.
>
> See range-diff and interdiff at the bottom.
Oops, actually, only the range-diff.
>
>
> Have a lovely night!
> Alex
>
> scripts/coccinelle/misc/array_end.cocci | 73 +++++++++++++++++++++++++
> 1 file changed, 73 insertions(+)
> create mode 100644 scripts/coccinelle/misc/array_end.cocci
>
> diff --git a/scripts/coccinelle/misc/array_end.cocci b/scripts/coccinelle/misc/array_end.cocci
> new file mode 100644
> index 00000000..88c664de
> --- /dev/null
> +++ b/scripts/coccinelle/misc/array_end.cocci
> @@ -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 <[email protected]>
> +// 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")
>
> Range-diff:
> 1: 9fd8d3d1 ! 1: 2ae22f8b scripts/coccinelle: Add script for using ARRAY_END()
> @@ scripts/coccinelle/misc/array_end.cocci (new)
> +//
> +// Confidence: High
> +// Copyright: 2026, Alejandro Colomar <[email protected]>
> -+// Comments: No known false positives, but has a few false negatives
> +// Options: --no-includes --include-headers
> +
> +virtual patch
> --
> 2.53.0
>
--
<https://www.alejandro-colomar.es>
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmm3Lr4ACgkQ64mZXMKQ wqnrsw/9G3wnBpashhburlsSNbUzzhZcn9yDOedHmUQ2qcQ/OlG4RcpISopKWV5E pZnUawavXdMLGp+0In+2yf4nEK9+bo0Ib4G5W6U79zoY1gt4xus3QTRr1FiBT5Lg dZI9UZDBhzvyUmZluTjUHxNttMSN5dsX4TSM2HOyOoSSd4r13qkFSoaMAH9A9rUX 81D76k3yxIrVvHGDMEq6flYY0cFeohigym20LH2A0v5T8I1ygCgbhyTVRhUOewPo Uvy25Bhd2Q1MAnkClDTAOi/7Z0ofVe/osBUoV056Vk0p5VueIDhN3U2BHktqrI/P cpW6regft6NO8Fmef662hcANBrPEGdXv44ViuhGWCFr//oR6Cfrvg247b/ZCEGeB 2uyvwihZUKBen6m2XspC9XDagHItPvQiGP54YNmAlv6Sjf7s2YMyuOn+ApRipSir NNS6/nRrF9bReGzstOCYHf3yiN6l6dY+bZ12OF4/9swmixScpTiA7UusucShUWd1 aimc9uVZgCxfqjALbAnqQyV8hLDHJmhUfROORz1RZ0+TGk5knCjFMmOTMwx6g2rN QvP58nGvCS4TaVAy3NuhRdwCqkXCjwJHvBm8QtEFfOdsWKbNFuLFU6vVNHCR1Rsg e18WZWfYneNwR7IEDkKSjxn0csdbOSUeo35yPmxtUH6VmrEw800= =V6hG -----END PGP SIGNATURE-----