From: Daniel Thompson <daniel@riscstar.com>
To: Alejandro Colomar <alx@kernel.org>
Cc: Martin Uecker <uecker@tugraz.at>,
Christopher Bazley <chris.bazley.wg14@gmail.com>,
Alex Celeste <alexg.nvfp@gmail.com>,
Joseph Myers <josmyers@redhat.com>,
Aaron Ballman <aaron@aaronballman.com>,
Douglas McIlroy <douglas.mcilroy@dartmouth.edu>,
Bruno Haible <bruno@clisp.org>, Paul Eggert <eggert@cs.ucla.edu>,
Florian Weimer <fweimer@redhat.com>,
Jonathan Corbet <corbet@lwn.net>, Kees Cook <kees@kernel.org>,
Eric Biggers <ebiggers@kernel.org>,
Ard Biesheuvel <ardb@kernel.org>,
Daniel Thompson <danielt@kernel.org>,
Daniel Lundin <daniel.lundin.mail@gmail.com>,
"Valentin V. Bartenev" <vbartenev@gmail.com>,
Andrew Clayton <andrew@digital-domain.net>,
"Brian W. Kernighan" <bwk@cs.princeton.edu>,
"G. Branden Robinson" <branden@debian.org>,
"Basil L. Contovounesios" <basil@contovou.net>,
"Jason A. Donenfeld" <jason@zx2c4.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
onf <onf@disroot.org>, Rich Felker <dalias@libc.org>,
linux-hardening@vger.kernel.org
Subject: Re: [RFC v3 3/6] alx-0078r2 - [static n] shouldn't access more than n elements
Date: Wed, 28 Jan 2026 09:54:37 +0000 [thread overview]
Message-ID: <aXnc3aeRq045FWZy@aspen.lan> (raw)
In-Reply-To: <aXdirEZVgj8PKPxM@devuan>
On Mon, Jan 26, 2026 at 01:48:49PM +0100, Alejandro Colomar wrote:
> Name
> alx-0078r2 - [static n] shouldn't access more than n elements
>
> Principles
> - Uphold the character of the language.
> - Codify existing practice to address evident deficiencies.
> - Enable secure programming.
>
> And from previous charters:
>
> C23:
> - APIs should be self-documenting when possible.
>
> Category
> Language; array parameters.
>
> Authors
> Alejandro Colomar <alx@kernel.org>
> Martin Uecker <uecker@tugraz.at>
>
> Acked-by: Doug McIlroy
> Acked-by: Andrew Clayton <ac@sigsegv.uk>
>
> History
> <https://www.alejandro-colomar.es/src/alx/alx/std/wg14/alx-0078.git/>
>
> r0 (2026-01-25):
> - Initial draft.
>
> r1 (2026-01-25):
> - wfix.
> - Co-authored by Martin.
>
> r2 (2026-01-26):
> - Acked-by.
> - tfix
>
> Abstract
> The following function prototype requires an input with at least
> 2 elements:
>
> void f(int a[static 2]);
>
> It should not use more than 2 elements, as those are not
> guaranteed to be available. That is, the following function
> definition should be unacceptable:
>
> void f(int a[static 2])
> {
> a[7] = 0;
> }
>
> Discussion
> It is a de-facto standard that functions declaring a [static n]
> parameter require at least n elements, and don't access more
> than n elements. Most programmers that don't know the fine
> letter of the standard would assume that. This has its roots in
> the older syntax, [n], which is not acknowledged by the
> standard, but has been historically used to document this as
> part of the API.
>
> Without this, [static n] is only useful for optimizations, but
> not for writing safe code, as the specification of [static n]
> doesn't provide the compiler with enough information to know
> whether array bounds will be violated. This makes it a terrible
> UB foot-gun.
>
> Let's change the specification to make it safe.
I don't see how the proposed wording change makes C safer, see below.
> <snip>
> 6.7.7.4 Function declarators
> @@ Semantics, p7
> A declaration of a parameter as "array of type"
> shall be adjusted to "qualified pointer to type",
> where the type qualifiers (if any)
> are those specified
> within the [ and ]
> of the array type derivation.
> 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.
> +as specified by the array length expression,
> +and the function definition
> +shall not access an element
> +beyond the specified number of elements.
By limiting what the function definition may do, we are reducing the set
of valid programs. Won't that *increase* the level of undefined
behaviour in the language?
That will allow compilers to perform more aggressive local optimizations
(which is probably good) but I don't see how it will make things safer.
To make things safe would mean compilers changing their diagnostics, and
the prior art suggests compilers already warn on this.
Daniel.
next prev parent reply other threads:[~2026-01-28 9:54 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <aXZM5O2mU2e3LJBJ@devuan>
2026-01-26 12:48 ` [RFC v3 0/6] Improve function parameters in ISO C2y Alejandro Colomar
2026-01-26 12:48 ` [RFC v3 1/6] alx-0077r3 - disallow function parameters of function type Alejandro Colomar
2026-01-26 12:48 ` [RFC v3 2/6] alx-0076r3 - incompatible array parameters Alejandro Colomar
2026-01-26 12:48 ` [RFC v3 3/6] alx-0078r2 - [static n] shouldn't access more than n elements Alejandro Colomar
2026-01-28 9:54 ` Daniel Thompson [this message]
2026-01-28 15:14 ` Alejandro Colomar
2026-01-26 12:49 ` [RFC v3 4/6] alx-0079r2 - [static n] == non-null [n] Alejandro Colomar
2026-01-26 12:49 ` [RFC v3 5/6] alx-0081r2 - array parameters of 0 elements Alejandro Colomar
2026-01-28 10:14 ` Daniel Thompson
2026-01-28 15:21 ` Alejandro Colomar
2026-01-26 12:49 ` [RFC v3 6/6] alx-0080r1 - [static] without array length expression Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aXnc3aeRq045FWZy@aspen.lan \
--to=daniel@riscstar.com \
--cc=aaron@aaronballman.com \
--cc=alexg.nvfp@gmail.com \
--cc=alx@kernel.org \
--cc=andrew@digital-domain.net \
--cc=ardb@kernel.org \
--cc=basil@contovou.net \
--cc=branden@debian.org \
--cc=bruno@clisp.org \
--cc=bwk@cs.princeton.edu \
--cc=chris.bazley.wg14@gmail.com \
--cc=corbet@lwn.net \
--cc=dalias@libc.org \
--cc=daniel.lundin.mail@gmail.com \
--cc=danielt@kernel.org \
--cc=douglas.mcilroy@dartmouth.edu \
--cc=ebiggers@kernel.org \
--cc=eggert@cs.ucla.edu \
--cc=fweimer@redhat.com \
--cc=jason@zx2c4.com \
--cc=josmyers@redhat.com \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=onf@disroot.org \
--cc=torvalds@linux-foundation.org \
--cc=uecker@tugraz.at \
--cc=vbartenev@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox