careadlinkat: Guard against misuse of careadlinkatcwd.
[gnulib.git] / lib / careadlinkat.c
1 /* Read symbolic links into a buffer without size limitation, relative to fd.
2
3    Copyright (C) 2001, 2003-2004, 2007, 2009-2011 Free Software Foundation,
4    Inc.
5
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.
10
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.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering.  */
20
21 #include <config.h>
22
23 #include "careadlinkat.h"
24
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 /* Define this independently so that stdint.h is not a prerequisite.  */
32 #ifndef SIZE_MAX
33 # define SIZE_MAX ((size_t) -1)
34 #endif
35
36 #ifndef SSIZE_MAX
37 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
38 #endif
39
40 #include "allocator.h"
41
42 #if ! HAVE_READLINKAT
43 /* Get the symbolic link value of FILENAME and put it into BUFFER, with
44    size BUFFER_SIZE.  This function acts like readlink  but has
45    readlinkat's signature.  */
46 ssize_t
47 careadlinkatcwd (int fd, char const *filename, char *buffer,
48                  size_t buffer_size)
49 {
50   /* FD must be AT_FDCWD here, otherwise the caller is using this
51      function in contexts for which it was not meant for.  */
52   if (fd != AT_FDCWD)
53     abort ();
54   return readlink (filename, buffer, buffer_size);
55 }
56 #endif
57
58 /* Assuming the current directory is FD, get the symbolic link value
59    of FILENAME as a null-terminated string and put it into a buffer.
60    If FD is AT_FDCWD, FILENAME is interpreted relative to the current
61    working directory, as in openat.
62
63    If the link is small enough to fit into BUFFER put it there.
64    BUFFER's size is BUFFER_SIZE, and BUFFER can be null
65    if BUFFER_SIZE is zero.
66
67    If the link is not small, put it into a dynamically allocated
68    buffer managed by ALLOC.  It is the caller's responsibility to free
69    the returned value if it is nonnull and is not BUFFER.  A null
70    ALLOC stands for the standard allocator.
71
72    The PREADLINKAT function specifies how to read links.  It operates
73    like POSIX readlinkat()
74    <http://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
75    but can assume that its first argument is the same as FD.
76
77    If successful, return the buffer address; otherwise return NULL and
78    set errno.  */
79
80 char *
81 careadlinkat (int fd, char const *filename,
82               char *buffer, size_t buffer_size,
83               struct allocator const *alloc,
84               ssize_t (*preadlinkat) (int, char const *, char *, size_t))
85 {
86   char *buf;
87   size_t buf_size;
88   size_t buf_size_max =
89     SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
90   char stack_buf[1024];
91
92   if (! alloc)
93     alloc = &stdlib_allocator;
94
95   if (! buffer_size)
96     {
97       /* Allocate the initial buffer on the stack.  This way, in the
98          common case of a symlink of small size, we get away with a
99          single small malloc() instead of a big malloc() followed by a
100          shrinking realloc().  */
101       buffer = stack_buf;
102       buffer_size = sizeof stack_buf;
103     }
104
105   buf = buffer;
106   buf_size = buffer_size;
107
108   do
109     {
110       /* Attempt to read the link into the current buffer.  */
111       ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
112       size_t link_size;
113       if (link_length < 0)
114         {
115           /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
116              with errno == ERANGE if the buffer is too small.  */
117           int readlinkat_errno = errno;
118           if (readlinkat_errno != ERANGE)
119             {
120               if (buf != buffer)
121                 {
122                   alloc->free (buf);
123                   errno = readlinkat_errno;
124                 }
125               return NULL;
126             }
127         }
128
129       link_size = link_length;
130
131       if (link_size < buf_size)
132         {
133           buf[link_size++] = '\0';
134
135           if (buf == stack_buf)
136             {
137               char *b = (char *) alloc->allocate (link_size);
138               if (! b)
139                 break;
140               memcpy (b, buf, link_size);
141               buf = b;
142             }
143           else if (link_size < buf_size && buf != buffer && alloc->reallocate)
144             {
145               /* Shrink BUF before returning it.  */
146               char *b = (char *) alloc->reallocate (buf, link_size);
147               if (b)
148                 buf = b;
149             }
150
151           return buf;
152         }
153
154       if (buf != buffer)
155         alloc->free (buf);
156
157       if (buf_size <= buf_size_max / 2)
158         buf_size *= 2;
159       else if (buf_size < buf_size_max)
160         buf_size = buf_size_max;
161       else
162         break;
163       buf = (char *) alloc->allocate (buf_size);
164     }
165   while (buf);
166
167   if (alloc->die)
168     alloc->die ();
169   errno = ENOMEM;
170   return NULL;
171 }