public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] linux: Fix a non-constant expression in _Static_assert
@ 2021-10-08  0:46 Fangrui Song
  2021-10-15  0:10 ` Fāng-ruì Sòng
  2021-10-15  5:12 ` Florian Weimer
  0 siblings, 2 replies; 4+ messages in thread
From: Fangrui Song @ 2021-10-08  0:46 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella

According to C11 6.6p6, `const int` as an operand may not make up a
constant expression. GCC -O0 errors:

../sysdeps/unix/sysv/linux/opendir.c:107:19: error: static_assert expression is not an integral constant expression
  _Static_assert (allocation_size >= sizeof (struct dirent64),

-O2 -Wpedantic has a similar warning.
See https://gcc.gnu.org/PR102502 for GCC's inconsistency.

Use enum which is guaranteed to be a constant expression.
This also makes the file compilable with Clang.

Fixes: 4b962c9e859de23b461d61f860dbd3f21311e83a ("linux: Simplify opendir buffer allocation")
---
Changes from v1:
* Clarify commit message
* Reference https://gcc.gnu.org/PR102502 
---
 sysdeps/unix/sysv/linux/opendir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index 48f254d169..88640f44ee 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -103,7 +103,7 @@ __alloc_dir (int fd, bool close_fd, int flags,
      file system provides a bogus value.  */
   enum { max_buffer_size = 1048576 };
 
-  const size_t allocation_size = 32768;
+  enum { allocation_size = 32768 };
   _Static_assert (allocation_size >= sizeof (struct dirent64),
 		  "allocation_size < sizeof (struct dirent64)");
 
-- 
2.33.0.882.g93a45727a2-goog


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

* Re: [PATCH v2] linux: Fix a non-constant expression in _Static_assert
  2021-10-08  0:46 [PATCH v2] linux: Fix a non-constant expression in _Static_assert Fangrui Song
@ 2021-10-15  0:10 ` Fāng-ruì Sòng
  2021-10-15  5:12 ` Florian Weimer
  1 sibling, 0 replies; 4+ messages in thread
From: Fāng-ruì Sòng @ 2021-10-15  0:10 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella

On Thu, Oct 7, 2021 at 5:46 PM Fangrui Song <maskray@google.com> wrote:
>
> According to C11 6.6p6, `const int` as an operand may not make up a
> constant expression. GCC -O0 errors:
>
> ../sysdeps/unix/sysv/linux/opendir.c:107:19: error: static_assert expression is not an integral constant expression
>   _Static_assert (allocation_size >= sizeof (struct dirent64),
>
> -O2 -Wpedantic has a similar warning.
> See https://gcc.gnu.org/PR102502 for GCC's inconsistency.
>
> Use enum which is guaranteed to be a constant expression.
> This also makes the file compilable with Clang.
>
> Fixes: 4b962c9e859de23b461d61f860dbd3f21311e83a ("linux: Simplify opendir buffer allocation")
> ---
> Changes from v1:
> * Clarify commit message
> * Reference https://gcc.gnu.org/PR102502
> ---
>  sysdeps/unix/sysv/linux/opendir.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
> index 48f254d169..88640f44ee 100644
> --- a/sysdeps/unix/sysv/linux/opendir.c
> +++ b/sysdeps/unix/sysv/linux/opendir.c
> @@ -103,7 +103,7 @@ __alloc_dir (int fd, bool close_fd, int flags,
>       file system provides a bogus value.  */
>    enum { max_buffer_size = 1048576 };
>
> -  const size_t allocation_size = 32768;
> +  enum { allocation_size = 32768 };
>    _Static_assert (allocation_size >= sizeof (struct dirent64),
>                   "allocation_size < sizeof (struct dirent64)");
>
> --
> 2.33.0.882.g93a45727a2-goog
>

Gentle ping:)

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

* Re: [PATCH v2] linux: Fix a non-constant expression in _Static_assert
  2021-10-08  0:46 [PATCH v2] linux: Fix a non-constant expression in _Static_assert Fangrui Song
  2021-10-15  0:10 ` Fāng-ruì Sòng
@ 2021-10-15  5:12 ` Florian Weimer
  2021-10-19  6:23   ` Fāng-ruì Sòng
  1 sibling, 1 reply; 4+ messages in thread
From: Florian Weimer @ 2021-10-15  5:12 UTC (permalink / raw)
  To: Fangrui Song via Libc-alpha

* Fangrui Song via Libc-alpha:

> diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
> index 48f254d169..88640f44ee 100644
> --- a/sysdeps/unix/sysv/linux/opendir.c
> +++ b/sysdeps/unix/sysv/linux/opendir.c
> @@ -103,7 +103,7 @@ __alloc_dir (int fd, bool close_fd, int flags,
>       file system provides a bogus value.  */
>    enum { max_buffer_size = 1048576 };
>  
> -  const size_t allocation_size = 32768;
> +  enum { allocation_size = 32768 };
>    _Static_assert (allocation_size >= sizeof (struct dirent64),
>  		  "allocation_size < sizeof (struct dirent64)");

Below we have:

  /* Increase allocation if requested, but not if the value appears to
     be bogus.  It will be between 32Kb and 1Mb.  */
  size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
			   max_buffer_size);

Mixed-type MAX is a bit iffy, but we already have mixed-type MIN here,
so it probably does not matter.

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

* Re: [PATCH v2] linux: Fix a non-constant expression in _Static_assert
  2021-10-15  5:12 ` Florian Weimer
@ 2021-10-19  6:23   ` Fāng-ruì Sòng
  0 siblings, 0 replies; 4+ messages in thread
From: Fāng-ruì Sòng @ 2021-10-19  6:23 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Fangrui Song via Libc-alpha, Adhemerval Zanella

On Thu, Oct 14, 2021 at 10:19 PM Florian Weimer <fw@deneb.enyo.de> wrote:
>
> * Fangrui Song via Libc-alpha:
>
> > diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
> > index 48f254d169..88640f44ee 100644
> > --- a/sysdeps/unix/sysv/linux/opendir.c
> > +++ b/sysdeps/unix/sysv/linux/opendir.c
> > @@ -103,7 +103,7 @@ __alloc_dir (int fd, bool close_fd, int flags,
> >       file system provides a bogus value.  */
> >    enum { max_buffer_size = 1048576 };
> >
> > -  const size_t allocation_size = 32768;
> > +  enum { allocation_size = 32768 };
> >    _Static_assert (allocation_size >= sizeof (struct dirent64),
> >                 "allocation_size < sizeof (struct dirent64)");
>
> Below we have:
>
>   /* Increase allocation if requested, but not if the value appears to
>      be bogus.  It will be between 32Kb and 1Mb.  */
>   size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
>                            max_buffer_size);
>
> Mixed-type MAX is a bit iffy, but we already have mixed-type MIN here,
> so it probably does not matter.

I'll wait few days and push this form:

-  size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
-                          max_buffer_size);
+  size_t allocation = MIN (MAX ((size_t) statp->st_blksize, (size_t)
+                                allocation_size), (size_t) max_buffer_size);

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

end of thread, other threads:[~2021-10-19  6:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-08  0:46 [PATCH v2] linux: Fix a non-constant expression in _Static_assert Fangrui Song
2021-10-15  0:10 ` Fāng-ruì Sòng
2021-10-15  5:12 ` Florian Weimer
2021-10-19  6:23   ` Fāng-ruì Sòng

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