linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH libcrypto 1/2] array_size: introduce min_array_size() function decoration
@ 2025-11-18 17:02 Jason A. Donenfeld
  2025-11-18 17:02 ` [PATCH libcrypto 2/2] crypto: chacha20poly1305: statically check fixed array lengths Jason A. Donenfeld
  2025-11-18 23:24 ` [PATCH libcrypto 1/2] array_size: introduce min_array_size() function decoration Eric Biggers
  0 siblings, 2 replies; 15+ messages in thread
From: Jason A. Donenfeld @ 2025-11-18 17:02 UTC (permalink / raw)
  To: Linus Torvalds, Eric Biggers, Ard Biesheuvel, Kees Cook,
	linux-crypto, linux-kernel
  Cc: Jason A. Donenfeld

Clang and recent gcc support warning if they are able to prove that the
user is passing to a function an array that is too short in size. For
example:

    void blah(unsigned char herp[static 7]);
    static void schma(void)
    {
        unsigned char good[] = { 1, 2, 3, 4, 5, 6, 7 };
        unsigned char bad[] = { 1, 2, 3, 4, 5, 6 };
        blah(good);
        blah(bad);
    }

The notation here, `static 7`, means that it's incorrect to pass
anything less than 7 elements. This is section 6.7.5.3 of C99:

    If the keyword static also appears within the [ and ] of the array
    type derivation, then for each call to the function, the value of
    the corresponding actual argument shall provide access to the first
    element of an array with at least as many elements as specified by
    the size expression.

Here is the output from gcc 15:

    zx2c4@thinkpad /tmp $ gcc -c a.c
    a.c: In function ‘schma’:
    a.c:9:9: warning: ‘blah’ accessing 7 bytes in a region of size 6 [-Wstringop-overflow=]
        9 |         blah(bad);
          |         ^~~~~~~~~
    a.c:9:9: note: referencing argument 1 of type ‘unsigned char[7]’
    a.c:2:6: note: in a call to function ‘blah’
        2 | void blah(unsigned char herp[static 7]);
          |      ^~~~

And from clang 21:

    zx2c4@thinkpad /tmp $ clang -c a.c
    a.c:9:2: warning: array argument is too small; contains 6 elements, callee requires at least 7
          [-Warray-bounds]
        9 |         blah(bad);
          |         ^    ~~~
    a.c:2:25: note: callee declares array parameter as static here
        2 | void blah(unsigned char herp[static 7]);
          |                         ^   ~~~~~~~~~~
    1 warning generated.

So these are covered by, variously, -Wstringop-overflow and
-Warray-bounds.

Introduce min_array_size(), so that the above code becomes slightly less
ugly:

    void blah(unsigned char herp[min_array_size(7)]);

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 include/linux/array_size.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/include/linux/array_size.h b/include/linux/array_size.h
index 06d7d83196ca..8671aee11479 100644
--- a/include/linux/array_size.h
+++ b/include/linux/array_size.h
@@ -10,4 +10,11 @@
  */
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
 
+/**
+ * min_array_size - parameter decoration to hint to the compiler that the
+ *                  passed array should have at least @n elements
+ * @n: minimum number of elements, after which the compiler may warn
+ */
+#define min_array_size(n) static n
+
 #endif  /* _LINUX_ARRAY_SIZE_H */
-- 
2.51.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2025-11-22  2:37 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-18 17:02 [PATCH libcrypto 1/2] array_size: introduce min_array_size() function decoration Jason A. Donenfeld
2025-11-18 17:02 ` [PATCH libcrypto 2/2] crypto: chacha20poly1305: statically check fixed array lengths Jason A. Donenfeld
2025-11-19 10:36   ` kernel test robot
2025-11-19 12:45   ` kernel test robot
2025-11-19 16:22     ` Linus Torvalds
2025-11-19 16:46       ` Jason A. Donenfeld
2025-11-19 16:57         ` Linus Torvalds
2025-11-19 18:45         ` Nathan Chancellor
2025-11-18 23:24 ` [PATCH libcrypto 1/2] array_size: introduce min_array_size() function decoration Eric Biggers
2025-11-18 23:31   ` Jason A. Donenfeld
2025-11-19 19:04     ` Jason A. Donenfeld
2025-11-19 19:10       ` Ard Biesheuvel
2025-11-19 23:02       ` david laight
2025-11-18 23:32   ` Linus Torvalds
2025-11-22  2:37     ` Herbert Xu

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).