Bump to m4 1.4.19
[platform/upstream/m4.git] / lib / eloop-threshold.h
1 /* Threshold at which to diagnose ELOOP.  Generic version.
2    Copyright (C) 2012-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public
7    License as published by the Free Software Foundation; either
8    version 3 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 #ifndef _ELOOP_THRESHOLD_H
20 #define _ELOOP_THRESHOLD_H      1
21
22 #include <limits.h>
23 #ifdef _LIBC
24 # include <sys/param.h>
25 # define _GL_ATTRIBUTE_CONST __attribute__ ((const))
26 #else
27 # include <unistd.h>
28 # include "minmax.h"
29 # define __sysconf sysconf
30 # if (!defined SYMLOOP_MAX \
31       && ! (defined _SC_SYMLOOP_MAX && defined _POSIX_SYMLOOP_MAX))
32 #  define SYMLOOP_MAX 8
33 # endif
34 #endif
35
36 /* POSIX specifies SYMLOOP_MAX as the "Maximum number of symbolic
37    links that can be reliably traversed in the resolution of a
38    pathname in the absence of a loop."  This makes it a minimum that
39    we should certainly accept.  But it leaves open the possibility
40    that more might sometimes work--just not "reliably".
41
42    For example, Linux implements a complex policy whereby there is a
43    small limit on the number of direct symlink traversals (a symlink
44    to a symlink to a symlink), but larger limit on the total number of
45    symlink traversals overall.  Hence the SYMLOOP_MAX number should be
46    the small one, but the limit library functions enforce on users
47    should be the larger one.
48
49    So, we use the larger of the reported SYMLOOP_MAX (if any) and our
50    own constant MIN_ELOOP_THRESHOLD, below.  This constant should be
51    large enough that it never rules out a file name and directory tree
52    that the underlying system (i.e. calls to 'open' et al) would
53    resolve successfully.  It should be small enough that actual loops
54    are detected without a huge number of iterations.  */
55
56 #ifndef MIN_ELOOP_THRESHOLD
57 # define MIN_ELOOP_THRESHOLD    40
58 #endif
59
60 /* Return the maximum number of symlink traversals to permit
61    before diagnosing ELOOP.  */
62 static inline unsigned int _GL_ATTRIBUTE_CONST
63 __eloop_threshold (void)
64 {
65 #ifdef SYMLOOP_MAX
66   const int symloop_max = SYMLOOP_MAX;
67 #else
68   /* The function is marked 'const' even though we use memory and
69      call a function, because sysconf is required to return the
70      same value in every call and so it must always be safe to
71      call __eloop_threshold exactly once and reuse the value.  */
72   static long int sysconf_symloop_max;
73   if (sysconf_symloop_max == 0)
74     sysconf_symloop_max = __sysconf (_SC_SYMLOOP_MAX);
75   const unsigned int symloop_max = (sysconf_symloop_max <= 0
76                                     ? _POSIX_SYMLOOP_MAX
77                                     : sysconf_symloop_max);
78 #endif
79
80   return MAX (symloop_max, MIN_ELOOP_THRESHOLD);
81 }
82
83 #endif  /* eloop-threshold.h */