>From 26f82c14e89f71ab1c2e6a811981ecc742b044e5 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 9 May 2020 18:01:59 -0700 Subject: [PATCH 1/2] careadlinkat: pacify -Wreturn-local-addr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lib/careadlinkat.c (careadlinkat) [GCC_LINT]: Pacify gcc 10’s -Wreturn-local-addr option. Simplify some of the later code. --- ChangeLog | 5 +++++ lib/careadlinkat.c | 29 +++++++++++++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index c35cfee97..b6d070599 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2020-05-09 Paul Eggert + 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.). diff --git a/lib/careadlinkat.c b/lib/careadlinkat.c index 1effdb784..e1f8305e9 100644 --- a/lib/careadlinkat.c +++ b/lib/careadlinkat.c @@ -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; -- 2.17.1