]> Savannah Git Hosting - gnulib.git/commitdiff
careadlinkat: pacify -Wreturn-local-addr
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 10 May 2020 01:01:59 +0000 (18:01 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 10 May 2020 01:10:26 +0000 (18:10 -0700)
* lib/careadlinkat.c (careadlinkat) [GCC_LINT]:
Pacify gcc 10’s -Wreturn-local-addr option.
Simplify some of the later code.

ChangeLog
lib/careadlinkat.c

index c35cfee97141f1dd6a1cc2957af23e0314973dba..b6d070599c007143ebc6a5c4e8f859fce399baea 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2020-05-09  Paul Eggert  <eggert@cs.ucla.edu>
 
+       careadlinkat: pacify -Wreturn-local-addr
+       * lib/careadlinkat.c (careadlinkat) [GCC_LINT]:
+       Pacify gcc 10’s -Wreturn-local-addr option.
+       Simplify some of the later code.
+
        attribute: remove ATTRIBUTE_DEPRECATED
        * lib/attribute.h: Improve recently-added comments, mostly
        by shortening them (use active voice, etc.).
index 1effdb78451faae27c15c760a5b0106f49099b7c..e1f8305e9b34f93878cf5a665e1831c9de6a07dc 100644 (file)
@@ -70,19 +70,28 @@ careadlinkat (int fd, char const *filename,
   size_t buf_size;
   size_t buf_size_max =
     SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
-  char stack_buf[1024];
+
+#if defined GCC_LINT || defined lint
+  /* Pacify preadlinkat without creating a pointer to the stack
+     that gcc -Wreturn-local-addr would cry wolf about.  */
+  static char initial_buf[1];
+  enum { initial_buf_size = 0 }; /* 0 so that initial_buf never changes.  */
+#else
+  char initial_buf[1024];
+  enum { initial_buf_size = sizeof initial_buf };
+#endif
 
   if (! alloc)
     alloc = &stdlib_allocator;
 
   if (! buffer_size)
     {
-      /* Allocate the initial buffer on the stack.  This way, in the
-         common case of a symlink of small size, we get away with a
+      /* Allocate the initial buffer.  This way, in the common case of
+         a symlink of small size without GCC_LINT, we get away with a
          single small malloc() instead of a big malloc() followed by a
          shrinking realloc().  */
-      buffer = stack_buf;
-      buffer_size = sizeof stack_buf;
+      buffer = initial_buf;
+      buffer_size = initial_buf_size;
     }
 
   buf = buffer;
@@ -115,21 +124,21 @@ careadlinkat (int fd, char const *filename,
         {
           buf[link_size++] = '\0';
 
-          if (buf == stack_buf)
+          if (buf == initial_buf)
             {
               char *b = (char *) alloc->allocate (link_size);
               buf_size = link_size;
               if (! b)
                 break;
-              memcpy (b, buf, link_size);
-              buf = b;
+              return memcpy (b, buf, link_size);
             }
-          else if (link_size < buf_size && buf != buffer && alloc->reallocate)
+
+          if (link_size < buf_size && buf != buffer && alloc->reallocate)
             {
               /* Shrink BUF before returning it.  */
               char *b = (char *) alloc->reallocate (buf, link_size);
               if (b)
-                buf = b;
+                return b;
             }
 
           return buf;