malloc-h: New module.
[gnulib.git] / lib / careadlinkat.c
blob6aaa712b9bef841bab92643481ef057cecd61530
1 /* Read symbolic links into a buffer without size limitation, relative to fd.
3 Copyright (C) 2001, 2003-2004, 2007, 2009-2020 Free Software Foundation,
4 Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program 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 General Public License for more details.
16 You should have received a copy of the GNU 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. */
21 #include <config.h>
23 #include "careadlinkat.h"
25 #include <errno.h>
26 #include <limits.h>
27 #include <string.h>
28 #include <unistd.h>
30 /* Define this independently so that stdint.h is not a prerequisite. */
31 #ifndef SIZE_MAX
32 # define SIZE_MAX ((size_t) -1)
33 #endif
35 #ifndef SSIZE_MAX
36 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
37 #endif
39 #include "allocator.h"
41 enum { STACK_BUF_SIZE = 1024 };
43 /* Act like careadlinkat (see below), with an additional argument
44 STACK_BUF that can be used as temporary storage.
46 If GCC_LINT is defined, do not inline this function with GCC 10.1
47 and later, to avoid creating a pointer to the stack that GCC
48 -Wreturn-local-addr incorrectly complains about. See:
49 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93644
50 Although the noinline attribute can hurt performance a bit, no better way
51 to pacify GCC is known; even an explicit #pragma does not pacify GCC.
52 When the GCC bug is fixed this workaround should be limited to the
53 broken GCC versions. */
54 #if _GL_GNUC_PREREQ (10, 1)
55 # if defined GCC_LINT || defined lint
56 __attribute__ ((__noinline__))
57 # elif __OPTIMIZE__ && !__NO_INLINE__
58 # define GCC_BOGUS_WRETURN_LOCAL_ADDR
59 # endif
60 #endif
61 static char *
62 readlink_stk (int fd, char const *filename,
63 char *buffer, size_t buffer_size,
64 struct allocator const *alloc,
65 ssize_t (*preadlinkat) (int, char const *, char *, size_t),
66 char stack_buf[STACK_BUF_SIZE])
68 char *buf;
69 size_t buf_size;
70 size_t buf_size_max =
71 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
73 if (! alloc)
74 alloc = &stdlib_allocator;
76 if (!buffer)
78 buffer = stack_buf;
79 buffer_size = STACK_BUF_SIZE;
82 buf = buffer;
83 buf_size = buffer_size;
85 while (buf)
87 /* Attempt to read the link into the current buffer. */
88 ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
89 size_t link_size;
90 if (link_length < 0)
92 if (buf != buffer)
94 int readlinkat_errno = errno;
95 alloc->free (buf);
96 errno = readlinkat_errno;
98 return NULL;
101 link_size = link_length;
103 if (link_size < buf_size)
105 buf[link_size++] = '\0';
107 if (buf == stack_buf)
109 char *b = alloc->allocate (link_size);
110 buf_size = link_size;
111 if (! b)
112 break;
113 return memcpy (b, buf, link_size);
116 if (link_size < buf_size && buf != buffer && alloc->reallocate)
118 /* Shrink BUF before returning it. */
119 char *b = alloc->reallocate (buf, link_size);
120 if (b)
121 return b;
124 return buf;
127 if (buf != buffer)
128 alloc->free (buf);
130 if (buf_size < buf_size_max / 2)
131 buf_size = 2 * buf_size + 1;
132 else if (buf_size < buf_size_max)
133 buf_size = buf_size_max;
134 else if (buf_size_max < SIZE_MAX)
136 errno = ENAMETOOLONG;
137 return NULL;
139 else
140 break;
141 buf = alloc->allocate (buf_size);
144 if (alloc->die)
145 alloc->die (buf_size);
146 errno = ENOMEM;
147 return NULL;
151 /* Assuming the current directory is FD, get the symbolic link value
152 of FILENAME as a null-terminated string and put it into a buffer.
153 If FD is AT_FDCWD, FILENAME is interpreted relative to the current
154 working directory, as in openat.
156 If the link is small enough to fit into BUFFER put it there.
157 BUFFER's size is BUFFER_SIZE, and BUFFER can be null
158 if BUFFER_SIZE is zero.
160 If the link is not small, put it into a dynamically allocated
161 buffer managed by ALLOC. It is the caller's responsibility to free
162 the returned value if it is nonnull and is not BUFFER. A null
163 ALLOC stands for the standard allocator.
165 The PREADLINKAT function specifies how to read links. It operates
166 like POSIX readlinkat()
167 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
168 but can assume that its first argument is the same as FD.
170 If successful, return the buffer address; otherwise return NULL and
171 set errno. */
173 char *
174 careadlinkat (int fd, char const *filename,
175 char *buffer, size_t buffer_size,
176 struct allocator const *alloc,
177 ssize_t (*preadlinkat) (int, char const *, char *, size_t))
179 /* Allocate the initial buffer on the stack. This way, in the
180 common case of a symlink of small size, we get away with a
181 single small malloc instead of a big malloc followed by a
182 shrinking realloc. */
183 #ifdef GCC_BOGUS_WRETURN_LOCAL_ADDR
184 #warning "GCC might issue a bogus -Wreturn-local-addr warning here."
185 #warning "See <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93644>."
186 #endif
187 char stack_buf[STACK_BUF_SIZE];
188 return readlink_stk (fd, filename, buffer, buffer_size, alloc,
189 preadlinkat, stack_buf);