1 /* Read symbolic links into a buffer without size limitation, relative to fd.
3 Copyright (C) 2001, 2003-2004, 2007, 2009-2021 Free Software Foundation,
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
23 #include "careadlinkat.h"
33 /* Define this independently so that stdint.h is not a prerequisite. */
35 # define SIZE_MAX ((size_t) -1)
39 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
42 #include "allocator.h"
44 enum { STACK_BUF_SIZE = 1024 };
46 /* Act like careadlinkat (see below), with an additional argument
47 STACK_BUF that can be used as temporary storage.
49 If GCC_LINT is defined, do not inline this function with GCC 10.1
50 and later, to avoid creating a pointer to the stack that GCC
51 -Wreturn-local-addr incorrectly complains about. See:
52 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93644
53 Although the noinline attribute can hurt performance a bit, no better way
54 to pacify GCC is known; even an explicit #pragma does not pacify GCC.
55 When the GCC bug is fixed this workaround should be limited to the
56 broken GCC versions. */
57 #if _GL_GNUC_PREREQ (10, 1)
58 # if defined GCC_LINT || defined lint
59 __attribute__ ((__noinline__))
60 # elif __OPTIMIZE__ && !__NO_INLINE__
61 # define GCC_BOGUS_WRETURN_LOCAL_ADDR
65 readlink_stk (int fd, char const *filename,
66 char *buffer, size_t buffer_size,
67 struct allocator const *alloc,
68 ssize_t (*preadlinkat) (int, char const *, char *, size_t),
69 char stack_buf[STACK_BUF_SIZE])
72 alloc = &stdlib_allocator;
77 buffer_size = STACK_BUF_SIZE;
81 idx_t buf_size_max = MIN (IDX_MAX, MIN (SSIZE_MAX, SIZE_MAX));
82 idx_t buf_size = MIN (buffer_size, buf_size_max);
86 /* Attempt to read the link into the current buffer. */
87 idx_t link_length = preadlinkat (fd, filename, buf, buf_size);
92 int readlinkat_errno = errno;
94 errno = readlinkat_errno;
99 idx_t link_size = link_length;
101 if (link_size < buf_size)
103 buf[link_size++] = '\0';
105 if (buf == stack_buf)
107 char *b = alloc->allocate (link_size);
108 buf_size = link_size;
111 return memcpy (b, buf, link_size);
114 if (link_size < buf_size && buf != buffer && alloc->reallocate)
116 /* Shrink BUF before returning it. */
117 char *b = alloc->reallocate (buf, link_size);
128 if (buf_size_max / 2 <= buf_size)
130 errno = ENAMETOOLONG;
134 buf_size = 2 * buf_size + 1;
135 buf = alloc->allocate (buf_size);
139 alloc->die (buf_size);
145 /* Assuming the current directory is FD, get the symbolic link value
146 of FILENAME as a null-terminated string and put it into a buffer.
147 If FD is AT_FDCWD, FILENAME is interpreted relative to the current
148 working directory, as in openat.
150 If the link is small enough to fit into BUFFER put it there.
151 BUFFER's size is BUFFER_SIZE, and BUFFER can be null
152 if BUFFER_SIZE is zero.
154 If the link is not small, put it into a dynamically allocated
155 buffer managed by ALLOC. It is the caller's responsibility to free
156 the returned value if it is nonnull and is not BUFFER. A null
157 ALLOC stands for the standard allocator.
159 The PREADLINKAT function specifies how to read links. It operates
160 like POSIX readlinkat()
161 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
162 but can assume that its first argument is the same as FD.
164 If successful, return the buffer address; otherwise return NULL and
168 careadlinkat (int fd, char const *filename,
169 char *buffer, size_t buffer_size,
170 struct allocator const *alloc,
171 ssize_t (*preadlinkat) (int, char const *, char *, size_t))
173 /* Allocate the initial buffer on the stack. This way, in the
174 common case of a symlink of small size, we get away with a
175 single small malloc instead of a big malloc followed by a
176 shrinking realloc. */
177 #ifdef GCC_BOGUS_WRETURN_LOCAL_ADDR
178 #warning "GCC might issue a bogus -Wreturn-local-addr warning here."
179 #warning "See <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93644>."
181 char stack_buf[STACK_BUF_SIZE];
182 return readlink_stk (fd, filename, buffer, buffer_size, alloc,
183 preadlinkat, stack_buf);