public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/maskray/relr] linux: Fix a possibly non-constant expression in _Static_assert
@ 2021-10-29 22:19 Fangrui Song
  0 siblings, 0 replies; only message in thread
From: Fangrui Song @ 2021-10-29 22:19 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=aa783f9a7b774d67487daa9376095738aef5cf88

commit aa783f9a7b774d67487daa9376095738aef5cf88
Author: Fangrui Song <maskray@google.com>
Date:   Wed Oct 20 14:22:43 2021 -0700

    linux: Fix a possibly non-constant expression in _Static_assert
    
    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")

Diff:
---
 sysdeps/unix/sysv/linux/opendir.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index 48f254d169..6874f796d3 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -103,14 +103,14 @@ __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)");
 
   /* 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);
+  size_t allocation = MIN (MAX ((size_t) statp->st_blksize, (size_t)
+                                allocation_size), (size_t) max_buffer_size);
 
   DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
   if (dirp == NULL)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-29 22:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-29 22:19 [glibc/maskray/relr] linux: Fix a possibly non-constant expression in _Static_assert Fangrui Song

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