public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: Martin Uecker <muecker@gwdg.de>
Cc: Xi Ruoyao <xry111@xry111.site>, Andrew Pinski <pinskia@gmail.com>,
	GNU libc development <libc-alpha@sourceware.org>,
	Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	Carlos O'Donell <carlos@redhat.com>,
	Andreas Schwab <schwab@suse.de>,
	Siddhesh Poyarekar <siddhesh@gotplt.org>,
	Zack Weinberg <zack@owlfolio.org>,
	"gcc@gcc.gnu.org" <gcc@gcc.gnu.org>, enh <enh@google.com>
Subject: ISO C's [static] (was: _Nullable and _Nonnull in GCC's analyzer)
Date: Wed, 9 Aug 2023 12:42:16 +0200	[thread overview]
Message-ID: <a213b674-2469-30e2-0e01-4c399fd0d47f@kernel.org> (raw)
In-Reply-To: <b6348394e323f88a8b74562d9a828a2ead6dc392.camel@gwdg.de>


[-- Attachment #1.1: Type: text/plain, Size: 2551 bytes --]

Hi Martin,

On 2023-08-09 09:26, Martin Uecker wrote:
> it is a bit cumbersome to use, but one can use [static]
> instead, which gives you the same static warnings.
> 
> [static] does not work with __builtin_dynamic_object_size,
> but maybe this could be changed (there is a bug filed.)
> 
> I am not sure whether [static] should imply [[gnu::nonnull]]

I have a gripe with ISO C's [static].  As you mention, ISO
conflated two functionalities in [static]:

-  The size of the array passed as argument must not be less
   than the size specified in the parameter's [].

-  The pointer must not be NULL.

And there are valid cases where you may want the first but
not the second.  Or the second but not the first (that's the
case for _Nonnull, of course).

In fact, it's so badly damaged, that it prompted a proposal
to ISO C of using [static 1] as an equivalent of _Nonnull in
the prototypes that accepted a pointer that should not be
NULL.  However, that proposal didn't include the functions
that actually take arrays as input (because they are taken
in the opposite order, so array syntax is not legal).  Don't
you find it ironic that ISO C could have used array syntax
for pointers and pointer syntax for arrays?  I do.

As for when one would want to mean the first (size of array)
but not _Nonnull: for a function where you may pass either
an array (which should not be smaller than the size), or a
sentinel NULL value.

Nevertheless, I floated the idea that [static] is completely
unnecessary, and nobody has yet been against it.

GCC could perfectly add a warning for the following case:

    void foo(size_t n, int a[n]);

    int
    main(void)
    {
        int a[7];

        foo(42, a);
    }

Nobody in their right mind would specify a size of an array
in a parameter and expect that passing a smaller array than
that can produce a valid program.  So, why not make that a
Wall warning?

And so [static] would be irrelevant in GNU C, because well,
what does it add?  In fact, I like that [static] is so badly
designed, because then we can repurpose plain [size] to mean
the right thing, which would produce cleaner programs
([static] just adds noise to the source).

What do you think of giving [42] a meaning, instead of just
ignoring it?

Cheers,
Alex

> which would then also trigger the optimization. I think
> clang uses it for optimization.
> 
> Martin

-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-08-09 10:42 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-10 16:13 [PATCH v5] libio: Add nonnull attribute for most FILE * arguments in stdio.h Xi Ruoyao
2023-07-10 17:12 ` Zack Weinberg
2023-07-10 17:27   ` Xi Ruoyao
2023-07-10 19:06     ` Zack Weinberg
2023-07-10 19:31       ` Xi Ruoyao
2023-07-10 17:51   ` Siddhesh Poyarekar
2023-07-10 18:41     ` Xi Ruoyao
2023-07-10 20:14       ` _Nullable and _Nonnull in GCC's analyzer (was: [PATCH v5] libio: Add nonnull attribute for most FILE * arguments in stdio.h) Alejandro Colomar
2023-07-10 20:16         ` Alejandro Colomar
2023-08-08 10:01           ` Martin Uecker
2023-08-09  0:14             ` enh
2023-08-09  1:11               ` Siddhesh Poyarekar
2023-08-09  7:26               ` Martin Uecker
2023-08-09 10:42                 ` Alejandro Colomar [this message]
2023-08-09 12:03                   ` ISO C's [static] (was: _Nullable and _Nonnull in GCC's analyzer) Martin Uecker
2023-08-09 12:37                     ` Alejandro Colomar
2023-08-09 14:24                       ` Martin Uecker
2023-08-09 13:46                   ` Xi Ruoyao
2023-08-11 23:34                 ` _Nullable and _Nonnull in GCC's analyzer (was: [PATCH v5] libio: Add nonnull attribute for most FILE * arguments in stdio.h) enh
2023-07-10 18:56     ` [PATCH v5] libio: Add nonnull attribute for most FILE * arguments in stdio.h Zack Weinberg
2023-07-10 19:31       ` Siddhesh Poyarekar
2023-07-10 19:35         ` Xi Ruoyao
2023-07-10 19:46           ` Siddhesh Poyarekar
2023-07-10 20:23             ` Xi Ruoyao
2023-07-10 20:33               ` Jeff Law
2023-07-10 20:44                 ` Xi Ruoyao
2023-07-10 20:55                 ` Zack Weinberg
2023-07-10 21:03                   ` Xi Ruoyao
2023-07-10 21:22                     ` Zack Weinberg
2023-07-10 21:33                       ` Xi Ruoyao
2023-07-11 19:12                         ` Zack Weinberg
2023-07-11 20:12                           ` Siddhesh Poyarekar
2023-07-12  8:59                             ` Xi Ruoyao
2023-07-10 22:09                       ` Paul Eggert
2023-07-11 19:18                         ` Zack Weinberg
2023-07-11 20:45                           ` Jeff Law
2023-07-11 23:59                             ` Paul Eggert
2023-07-12  2:40                               ` Jeff Law
2023-07-10 22:48                       ` Siddhesh Poyarekar
2023-07-11  0:45                         ` Sam James
2023-07-10 21:51                   ` Jeff Law
2023-07-11 13:03                     ` Cristian Rodríguez
2023-07-10 22:34                 ` Siddhesh Poyarekar
2023-07-10 22:59                   ` Jeff Law
2023-07-11  0:51         ` Sam James

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=a213b674-2469-30e2-0e01-4c399fd0d47f@kernel.org \
    --to=alx@kernel.org \
    --cc=adhemerval.zanella@linaro.org \
    --cc=carlos@redhat.com \
    --cc=enh@google.com \
    --cc=gcc@gcc.gnu.org \
    --cc=libc-alpha@sourceware.org \
    --cc=muecker@gwdg.de \
    --cc=pinskia@gmail.com \
    --cc=schwab@suse.de \
    --cc=siddhesh@gotplt.org \
    --cc=xry111@xry111.site \
    --cc=zack@owlfolio.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).