careadlinkat.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Read symbolic links into a buffer without size limitation, relative to fd.
  2. Copyright (C) 2001, 2003-2004, 2007, 2009-2020 Free Software Foundation,
  3. Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
  15. #include <config.h>
  16. #include "careadlinkat.h"
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. /* Define this independently so that stdint.h is not a prerequisite. */
  22. #ifndef SIZE_MAX
  23. # define SIZE_MAX ((size_t) -1)
  24. #endif
  25. #ifndef SSIZE_MAX
  26. # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
  27. #endif
  28. #include "allocator.h"
  29. enum { STACK_BUF_SIZE = 1024 };
  30. /* Act like careadlinkat (see below), with an additional argument
  31. STACK_BUF that can be used as temporary storage.
  32. If GCC_LINT is defined, do not inline this function with GCC 10.1
  33. and later, to avoid creating a pointer to the stack that GCC
  34. -Wreturn-local-addr incorrectly complains about. See:
  35. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93644
  36. Although the noinline attribute can hurt performance a bit, no better way
  37. to pacify GCC is known; even an explicit #pragma does not pacify GCC.
  38. When the GCC bug is fixed this workaround should be limited to the
  39. broken GCC versions. */
  40. #if (defined GCC_LINT || defined lint) && _GL_GNUC_PREREQ (10, 1)
  41. __attribute__ ((__noinline__))
  42. #endif
  43. static char *
  44. readlink_stk (int fd, char const *filename,
  45. char *buffer, size_t buffer_size,
  46. struct allocator const *alloc,
  47. ssize_t (*preadlinkat) (int, char const *, char *, size_t),
  48. char stack_buf[STACK_BUF_SIZE])
  49. {
  50. char *buf;
  51. size_t buf_size;
  52. size_t buf_size_max =
  53. SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
  54. if (! alloc)
  55. alloc = &stdlib_allocator;
  56. if (!buffer)
  57. {
  58. buffer = stack_buf;
  59. buffer_size = STACK_BUF_SIZE;
  60. }
  61. buf = buffer;
  62. buf_size = buffer_size;
  63. while (buf)
  64. {
  65. /* Attempt to read the link into the current buffer. */
  66. ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
  67. size_t link_size;
  68. if (link_length < 0)
  69. {
  70. /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
  71. with errno == ERANGE if the buffer is too small. */
  72. int readlinkat_errno = errno;
  73. if (readlinkat_errno != ERANGE)
  74. {
  75. if (buf != buffer)
  76. {
  77. alloc->free (buf);
  78. errno = readlinkat_errno;
  79. }
  80. return NULL;
  81. }
  82. }
  83. link_size = link_length;
  84. if (link_size < buf_size)
  85. {
  86. buf[link_size++] = '\0';
  87. if (buf == stack_buf)
  88. {
  89. char *b = alloc->allocate (link_size);
  90. buf_size = link_size;
  91. if (! b)
  92. break;
  93. return memcpy (b, buf, link_size);
  94. }
  95. if (link_size < buf_size && buf != buffer && alloc->reallocate)
  96. {
  97. /* Shrink BUF before returning it. */
  98. char *b = alloc->reallocate (buf, link_size);
  99. if (b)
  100. return b;
  101. }
  102. return buf;
  103. }
  104. if (buf != buffer)
  105. alloc->free (buf);
  106. if (buf_size < buf_size_max / 2)
  107. buf_size = 2 * buf_size + 1;
  108. else if (buf_size < buf_size_max)
  109. buf_size = buf_size_max;
  110. else if (buf_size_max < SIZE_MAX)
  111. {
  112. errno = ENAMETOOLONG;
  113. return NULL;
  114. }
  115. else
  116. break;
  117. buf = alloc->allocate (buf_size);
  118. }
  119. if (alloc->die)
  120. alloc->die (buf_size);
  121. errno = ENOMEM;
  122. return NULL;
  123. }
  124. /* Assuming the current directory is FD, get the symbolic link value
  125. of FILENAME as a null-terminated string and put it into a buffer.
  126. If FD is AT_FDCWD, FILENAME is interpreted relative to the current
  127. working directory, as in openat.
  128. If the link is small enough to fit into BUFFER put it there.
  129. BUFFER's size is BUFFER_SIZE, and BUFFER can be null
  130. if BUFFER_SIZE is zero.
  131. If the link is not small, put it into a dynamically allocated
  132. buffer managed by ALLOC. It is the caller's responsibility to free
  133. the returned value if it is nonnull and is not BUFFER. A null
  134. ALLOC stands for the standard allocator.
  135. The PREADLINKAT function specifies how to read links. It operates
  136. like POSIX readlinkat()
  137. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
  138. but can assume that its first argument is the same as FD.
  139. If successful, return the buffer address; otherwise return NULL and
  140. set errno. */
  141. char *
  142. careadlinkat (int fd, char const *filename,
  143. char *buffer, size_t buffer_size,
  144. struct allocator const *alloc,
  145. ssize_t (*preadlinkat) (int, char const *, char *, size_t))
  146. {
  147. /* Allocate the initial buffer on the stack. This way, in the
  148. common case of a symlink of small size, we get away with a
  149. single small malloc instead of a big malloc followed by a
  150. shrinking realloc.
  151. If GCC -Wreturn-local-addr warns about this buffer, the warning
  152. is bogus; see readlink_stk. */
  153. char stack_buf[STACK_BUF_SIZE];
  154. return readlink_stk (fd, filename, buffer, buffer_size, alloc,
  155. preadlinkat, stack_buf);
  156. }