Detect if the static keyword is supported in array type derivations in function parameters
Antonin Décimo <[email protected]> Thu, 20 Feb 2025 23:37:30 +0100
| Newsgroups | gmane.comp.sysutils.autoconf.general |
|---|---|
| Message-ID | <CACCWBSBQVft035SWWvGesQD03+QJJ_DpcABNRm8pSzFE+cpTJQ@mail.gmail.com> |
This is a C99 feature that helps with static analysis. > 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. See _Array declarators_ and _Function declarators_ from the C standard. It is not supported by MSVC: [C11: static inside array parameter square brackets](https://developercommunity.visualstudio.com/t/C11:-static-inside-array-parameter-squar/1475168). Both gcc and clang warn if `NULL` or if an array too small is passed to the function. Interestingly gcc also warns in the non-static case if an array too small is passed. cppcheck considers in both cases that the array has a fixed size and warns for out-of-bounds accesses. I'm suggesting adding an AC_C_STATIC_ARRAY_DECLARATOR macro, defined to static, or empty. I've tentatively written a macro modelled after AC_C_FLEXARRAY, attached to this mail. The name of the macro can, as it should, be bikeshedded. I'm not too sure how m4 processes the [ and ] in the description. Cheers, -- Antonin
0001-AC_C_STATIC_ARRAY_DECLARATOR.patch
(application/octet-stream, 3.4 KB)
From 65a80ffdff888675da8f051a0050501ba0cb854d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= <[email protected]> Date: Thu, 20 Feb 2025 23:31:04 +0100 Subject: [PATCH] AC_C_STATIC_ARRAY_DECLARATOR --- 0001-AC_C_STATIC_ARRAY_DECLARATOR.patch | 48 +++++++++++++++++++++++++ lib/autoconf/c.m4 | 25 +++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 0001-AC_C_STATIC_ARRAY_DECLARATOR.patch diff --git a/0001-AC_C_STATIC_ARRAY_DECLARATOR.patch b/0001-AC_C_STATIC_ARRAY_DECLARATOR.patch new file mode 100644 index 00000000..c2cdb470 --- /dev/null +++ b/0001-AC_C_STATIC_ARRAY_DECLARATOR.patch @@ -0,0 +1,48 @@ +From e940aa777bbc657e592529843736093c32adde47 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= <[email protected]> +Date: Thu, 20 Feb 2025 23:31:04 +0100 +Subject: [PATCH] AC_C_STATIC_ARRAY_DECLARATOR + +--- + lib/autoconf/c.m4 | 25 +++++++++++++++++++++++++ + 1 file changed, 25 insertions(+) + +diff --git a/lib/autoconf/c.m4 b/lib/autoconf/c.m4 +index bafa72c1..a4596d16 100644 +--- a/lib/autoconf/c.m4 ++++ b/lib/autoconf/c.m4 +@@ -2379,6 +2379,31 @@ AC_DEFUN([AC_C_VARARRAYS], + ]) + + ++# AC_C_STATIC_ARRAY_DECLARATOR ++# ---------------------------- ++# Check whether the C compiler supports the C99 static keyword keyword ++# appearing within the [ and ] of array type derivations in function ++# parameters. ++AC_DEFUN([AC_C_STATIC_ARRAY_DECLARATOR], ++[ ++ AC_CACHE_CHECK([if $CC supports the static keyword in array declarators], ++ ac_cv_c_static_array_declarator, ++ [AC_COMPILE_IFELSE( ++ [AC_LANG_PROGRAM( ++ [[static void f(int array[static 16]) { }]], ++ [[]])], ++ [ac_cv_c_static_array_declarator=yes], ++ [ac_cv_c_static_array_declarator=no])]) ++ if test $ac_cv_c_static_array_declarator = yes; then ++ AC_DEFINE([STATIC_ARRAY_DECLARATOR], [static], ++ [Define to nothing if the C compiler supports the 'static' keyword ++ appearing within the [ and ] of array type derivations in function ++ parameters.]) ++ else ++ AC_DEFINE([STATIC_ARRAY_DECLARATOR], []) ++ fi ++]) ++ + # AC_C_TYPEOF + # ----------- + # Check if the C compiler supports GCC's typeof syntax. +-- +2.45.1 + diff --git a/lib/autoconf/c.m4 b/lib/autoconf/c.m4 index bafa72c1..e9c93736 100644 --- a/lib/autoconf/c.m4 +++ b/lib/autoconf/c.m4 @@ -2379,6 +2379,31 @@ AC_DEFUN([AC_C_VARARRAYS], ]) +# AC_C_STATIC_ARRAY_DECLARATOR +# ---------------------------- +# Check whether the C compiler supports the C99 static keyword keyword +# appearing within the [ and ] of array type derivations in function +# parameters. +AC_DEFUN([AC_C_STATIC_ARRAY_DECLARATOR], +[ + AC_CACHE_CHECK([if $CC supports the static keyword in array declarators], + ac_cv_c_static_array_declarator, + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[static void f(int array[static 16]) { }]], + [[]])], + [ac_cv_c_static_array_declarator=yes], + [ac_cv_c_static_array_declarator=no])]) + if test $ac_cv_c_static_array_declarator = yes; then + AC_DEFINE([STATIC_ARRAY_DECLARATOR], [static], + [Define to empty if the C compiler doesn't support the 'static' + keyword appearing within the [ and ] of array type derivations in + function parameters.]) + else + AC_DEFINE([STATIC_ARRAY_DECLARATOR], []) + fi +]) + # AC_C_TYPEOF # ----------- # Check if the C compiler supports GCC's typeof syntax. -- 2.45.1