aboutsummaryrefslogtreecommitdiffstats
path: root/lib/canonicalize-lgpl.c
diff options
context:
space:
mode:
authorPaul Eggert2021-01-01 01:51:18 -0800
committerPaul Eggert2021-01-01 01:52:03 -0800
commit1b59478f4cf442f5201500b0a9c66f4332fce640 (patch)
tree02787d99a0476a28836390ee9e2ff332445381a1 /lib/canonicalize-lgpl.c
parent50f3949119cd5bb2f058b90d14b2940a3a8a7a0e (diff)
downloademacs-1b59478f4cf442f5201500b0a9c66f4332fce640.tar.gz
emacs-1b59478f4cf442f5201500b0a9c66f4332fce640.zip
Update from Gnulib by running admin/merge-gnulib.
Diffstat (limited to 'lib/canonicalize-lgpl.c')
-rw-r--r--lib/canonicalize-lgpl.c51
1 files changed, 19 insertions, 32 deletions
diff --git a/lib/canonicalize-lgpl.c b/lib/canonicalize-lgpl.c
index dcc6e12d9c5..b6dc3a447ab 100644
--- a/lib/canonicalize-lgpl.c
+++ b/lib/canonicalize-lgpl.c
@@ -32,7 +32,6 @@
32#include <fcntl.h> 32#include <fcntl.h>
33#include <limits.h> 33#include <limits.h>
34#include <stdbool.h> 34#include <stdbool.h>
35#include <stddef.h>
36#include <string.h> 35#include <string.h>
37#include <sys/stat.h> 36#include <sys/stat.h>
38#include <unistd.h> 37#include <unistd.h>
@@ -40,16 +39,11 @@
40#include <eloop-threshold.h> 39#include <eloop-threshold.h>
41#include <filename.h> 40#include <filename.h>
42#include <idx.h> 41#include <idx.h>
42#include <intprops.h>
43#include <scratch_buffer.h> 43#include <scratch_buffer.h>
44 44
45#ifdef _LIBC 45#ifdef _LIBC
46# include <shlib-compat.h> 46# include <shlib-compat.h>
47# include <sysdep.h>
48# ifdef __ASSUME_FACCESSAT2
49# define FACCESSAT_NEVER_EOVERFLOWS __ASSUME_FACCESSAT2
50# else
51# define FACCESSAT_NEVER_EOVERFLOWS true
52# endif
53# define GCC_LINT 1 47# define GCC_LINT 1
54# define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__)) 48# define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
55#else 49#else
@@ -91,14 +85,15 @@
91# define IF_LINT(Code) /* empty */ 85# define IF_LINT(Code) /* empty */
92#endif 86#endif
93 87
88/* True if adding two valid object sizes might overflow idx_t.
89 As a practical matter, this cannot happen on 64-bit machines. */
90enum { NARROW_ADDRESSES = IDX_MAX >> 31 >> 31 == 0 };
91
94#ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT 92#ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
95# define DOUBLE_SLASH_IS_DISTINCT_ROOT false 93# define DOUBLE_SLASH_IS_DISTINCT_ROOT false
96#endif 94#endif
97#ifndef FACCESSAT_NEVER_EOVERFLOWS
98# define FACCESSAT_NEVER_EOVERFLOWS false
99#endif
100 95
101#if !FUNC_REALPATH_WORKS || defined _LIBC 96#if defined _LIBC || !FUNC_REALPATH_WORKS
102 97
103/* Return true if FILE's existence can be shown, false (setting errno) 98/* Return true if FILE's existence can be shown, false (setting errno)
104 otherwise. Follow symbolic links. */ 99 otherwise. Follow symbolic links. */
@@ -106,14 +101,11 @@ static bool
106file_accessible (char const *file) 101file_accessible (char const *file)
107{ 102{
108# if defined _LIBC || HAVE_FACCESSAT 103# if defined _LIBC || HAVE_FACCESSAT
109 int r = __faccessat (AT_FDCWD, file, F_OK, AT_EACCESS); 104 return __faccessat (AT_FDCWD, file, F_OK, AT_EACCESS) == 0;
110# else 105# else
111 struct stat st; 106 struct stat st;
112 int r = __stat (file, &st); 107 return __stat (file, &st) == 0 || errno == EOVERFLOW;
113# endif 108# endif
114
115 return ((!FACCESSAT_NEVER_EOVERFLOWS && r < 0 && errno == EOVERFLOW)
116 || r == 0);
117} 109}
118 110
119/* True if concatenating END as a suffix to a file name means that the 111/* True if concatenating END as a suffix to a file name means that the
@@ -350,7 +342,12 @@ realpath_stk (const char *name, char *resolved,
350 idx_t end_idx IF_LINT (= 0); 342 idx_t end_idx IF_LINT (= 0);
351 if (end_in_extra_buffer) 343 if (end_in_extra_buffer)
352 end_idx = end - extra_buf; 344 end_idx = end - extra_buf;
353 idx_t len = strlen (end); 345 size_t len = strlen (end);
346 if (NARROW_ADDRESSES && INT_ADD_OVERFLOW (len, n))
347 {
348 __set_errno (ENOMEM);
349 goto error_nomem;
350 }
354 while (extra_buffer.length <= len + n) 351 while (extra_buffer.length <= len + n)
355 { 352 {
356 if (!scratch_buffer_grow_preserve (&extra_buffer)) 353 if (!scratch_buffer_grow_preserve (&extra_buffer))
@@ -413,24 +410,14 @@ error:
413error_nomem: 410error_nomem:
414 scratch_buffer_free (&extra_buffer); 411 scratch_buffer_free (&extra_buffer);
415 scratch_buffer_free (&link_buffer); 412 scratch_buffer_free (&link_buffer);
416 if (failed || rname == resolved)
417 scratch_buffer_free (rname_buf);
418
419 if (failed)
420 return NULL;
421 413
422 if (rname == resolved) 414 if (failed || rname == resolved)
423 return rname;
424 idx_t rname_size = dest - rname;
425 if (rname == rname_on_stack)
426 { 415 {
427 rname = malloc (rname_size); 416 scratch_buffer_free (rname_buf);
428 if (rname == NULL) 417 return failed ? NULL : resolved;
429 return NULL;
430 return memcpy (rname, rname_on_stack, rname_size);
431 } 418 }
432 char *result = realloc (rname, rname_size); 419
433 return result != NULL ? result : rname; 420 return scratch_buffer_dupfree (rname_buf, dest - rname);
434} 421}
435 422
436/* Return the canonical absolute name of file NAME. A canonical name 423/* Return the canonical absolute name of file NAME. A canonical name