1 /* readlink wrapper to return the link name in malloc'd storage.
2    Unlike xreadlink and xreadlink_with_size, don't ever call exit.
3 
4    Copyright (C) 2001, 2003-2007, 2009-2020 Free Software Foundation, 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 <https://www.gnu.org/licenses/>.  */
18 
19 /* Written by Jim Meyering <jim@meyering.net>  */
20 
21 #include <config.h>
22 
23 #include "areadlink.h"
24 
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 #ifndef SSIZE_MAX
33 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
34 #endif
35 
36 /* SYMLINK_MAX is used only for an initial memory-allocation sanity
37    check, so it's OK to guess too small on hosts where there is no
38    arbitrary limit to symbolic link length.  */
39 #ifndef SYMLINK_MAX
40 # define SYMLINK_MAX 1024
41 #endif
42 
43 #define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
44 
45 /* Call readlink to get the symbolic link value of FILE.
46    SIZE is a hint as to how long the link is expected to be;
47    typically it is taken from st_size.  It need not be correct.
48    Return a pointer to that NUL-terminated string in malloc'd storage.
49    If readlink fails, malloc fails, or if the link value is longer
50    than SSIZE_MAX, return NULL (caller may use errno to diagnose).  */
51 
52 char *
areadlink_with_size(char const * file,size_t size)53 areadlink_with_size (char const *file, size_t size)
54 {
55   /* Some buggy file systems report garbage in st_size.  Defend
56      against them by ignoring outlandish st_size values in the initial
57      memory allocation.  */
58   size_t symlink_max = SYMLINK_MAX;
59   size_t INITIAL_LIMIT_BOUND = 8 * 1024;
60   size_t initial_limit = (symlink_max < INITIAL_LIMIT_BOUND
61                           ? symlink_max + 1
62                           : INITIAL_LIMIT_BOUND);
63 
64   enum { stackbuf_size = 128 };
65 
66   /* The initial buffer size for the link value.  */
67   size_t buf_size = (size == 0 ? stackbuf_size
68                      : size < initial_limit ? size + 1 : initial_limit);
69 
70   while (1)
71     {
72       ssize_t r;
73       size_t link_length;
74       char stackbuf[stackbuf_size];
75       char *buf = stackbuf;
76       char *buffer = NULL;
77 
78       if (! (size == 0 && buf_size == stackbuf_size))
79         {
80           buf = buffer = malloc (buf_size);
81           if (!buffer)
82             {
83               errno = ENOMEM;
84               return NULL;
85             }
86         }
87 
88       r = readlink (file, buf, buf_size);
89       link_length = r;
90 
91       /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
92          with errno == ERANGE if the buffer is too small.  */
93       if (r < 0 && errno != ERANGE)
94         {
95           int saved_errno = errno;
96           free (buffer);
97           errno = saved_errno;
98           return NULL;
99         }
100 
101       if (link_length < buf_size)
102         {
103           buf[link_length] = 0;
104           if (!buffer)
105             {
106               buffer = malloc (link_length + 1);
107               if (buffer)
108                 return memcpy (buffer, buf, link_length + 1);
109             }
110           else if (link_length + 1 < buf_size)
111             {
112               /* Shrink BUFFER before returning it.  */
113               char *shrinked_buffer = realloc (buffer, link_length + 1);
114               if (shrinked_buffer != NULL)
115                 buffer = shrinked_buffer;
116             }
117           return buffer;
118         }
119 
120       free (buffer);
121       if (buf_size <= MAXSIZE / 2)
122         buf_size *= 2;
123       else if (buf_size < MAXSIZE)
124         buf_size = MAXSIZE;
125       else
126         {
127           errno = ENOMEM;
128           return NULL;
129         }
130     }
131 }
132