diff options
| author | Kenichi Handa | 2011-05-19 09:37:36 +0900 |
|---|---|---|
| committer | Kenichi Handa | 2011-05-19 09:37:36 +0900 |
| commit | 23373930daa192623bfda56960ccb04b2703fbe5 (patch) | |
| tree | 4350ad7774b3d4dc6f2ccf0e6343b505582e0112 /lib | |
| parent | a28d4396018e48479916fd624e2371aa1f75e6e7 (diff) | |
| parent | 627abcddd1c45a07d58b9c0cbfd6bb62caf256a9 (diff) | |
| download | emacs-23373930daa192623bfda56960ccb04b2703fbe5.tar.gz emacs-23373930daa192623bfda56960ccb04b2703fbe5.zip | |
merge trunk
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/gnulib.mk | 8 | ||||
| -rw-r--r-- | lib/intprops.h | 298 | ||||
| -rw-r--r-- | lib/unistd.in.h | 16 |
3 files changed, 285 insertions, 37 deletions
diff --git a/lib/gnulib.mk b/lib/gnulib.mk index 1466e430a4c..495e6918a45 100644 --- a/lib/gnulib.mk +++ b/lib/gnulib.mk | |||
| @@ -167,7 +167,8 @@ endif | |||
| 167 | 167 | ||
| 168 | ## begin gnulib module ignore-value | 168 | ## begin gnulib module ignore-value |
| 169 | 169 | ||
| 170 | libgnu_a_SOURCES += ignore-value.h | 170 | |
| 171 | EXTRA_DIST += ignore-value.h | ||
| 171 | 172 | ||
| 172 | ## end gnulib module ignore-value | 173 | ## end gnulib module ignore-value |
| 173 | 174 | ||
| @@ -779,6 +780,7 @@ unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H | |||
| 779 | -e 's|@''GNULIB_GETLOGIN_R''@|$(GNULIB_GETLOGIN_R)|g' \ | 780 | -e 's|@''GNULIB_GETLOGIN_R''@|$(GNULIB_GETLOGIN_R)|g' \ |
| 780 | -e 's|@''GNULIB_GETPAGESIZE''@|$(GNULIB_GETPAGESIZE)|g' \ | 781 | -e 's|@''GNULIB_GETPAGESIZE''@|$(GNULIB_GETPAGESIZE)|g' \ |
| 781 | -e 's|@''GNULIB_GETUSERSHELL''@|$(GNULIB_GETUSERSHELL)|g' \ | 782 | -e 's|@''GNULIB_GETUSERSHELL''@|$(GNULIB_GETUSERSHELL)|g' \ |
| 783 | -e 's|@''GNULIB_GROUP_MEMBER''@|$(GNULIB_GROUP_MEMBER)|g' \ | ||
| 782 | -e 's|@''GNULIB_LCHOWN''@|$(GNULIB_LCHOWN)|g' \ | 784 | -e 's|@''GNULIB_LCHOWN''@|$(GNULIB_LCHOWN)|g' \ |
| 783 | -e 's|@''GNULIB_LINK''@|$(GNULIB_LINK)|g' \ | 785 | -e 's|@''GNULIB_LINK''@|$(GNULIB_LINK)|g' \ |
| 784 | -e 's|@''GNULIB_LINKAT''@|$(GNULIB_LINKAT)|g' \ | 786 | -e 's|@''GNULIB_LINKAT''@|$(GNULIB_LINKAT)|g' \ |
| @@ -817,6 +819,7 @@ unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H | |||
| 817 | -e 's|@''HAVE_GETHOSTNAME''@|$(HAVE_GETHOSTNAME)|g' \ | 819 | -e 's|@''HAVE_GETHOSTNAME''@|$(HAVE_GETHOSTNAME)|g' \ |
| 818 | -e 's|@''HAVE_GETLOGIN''@|$(HAVE_GETLOGIN)|g' \ | 820 | -e 's|@''HAVE_GETLOGIN''@|$(HAVE_GETLOGIN)|g' \ |
| 819 | -e 's|@''HAVE_GETPAGESIZE''@|$(HAVE_GETPAGESIZE)|g' \ | 821 | -e 's|@''HAVE_GETPAGESIZE''@|$(HAVE_GETPAGESIZE)|g' \ |
| 822 | -e 's|@''HAVE_GROUP_MEMBER''@|$(HAVE_GROUP_MEMBER)|g' \ | ||
| 820 | -e 's|@''HAVE_LCHOWN''@|$(HAVE_LCHOWN)|g' \ | 823 | -e 's|@''HAVE_LCHOWN''@|$(HAVE_LCHOWN)|g' \ |
| 821 | -e 's|@''HAVE_LINK''@|$(HAVE_LINK)|g' \ | 824 | -e 's|@''HAVE_LINK''@|$(HAVE_LINK)|g' \ |
| 822 | -e 's|@''HAVE_LINKAT''@|$(HAVE_LINKAT)|g' \ | 825 | -e 's|@''HAVE_LINKAT''@|$(HAVE_LINKAT)|g' \ |
| @@ -883,9 +886,10 @@ EXTRA_DIST += unistd.in.h | |||
| 883 | ## begin gnulib module verify | 886 | ## begin gnulib module verify |
| 884 | 887 | ||
| 885 | if gl_GNULIB_ENABLED_verify | 888 | if gl_GNULIB_ENABLED_verify |
| 886 | libgnu_a_SOURCES += verify.h | ||
| 887 | 889 | ||
| 888 | endif | 890 | endif |
| 891 | EXTRA_DIST += verify.h | ||
| 892 | |||
| 889 | ## end gnulib module verify | 893 | ## end gnulib module verify |
| 890 | 894 | ||
| 891 | ## begin gnulib module warn-on-use | 895 | ## begin gnulib module warn-on-use |
diff --git a/lib/intprops.h b/lib/intprops.h index 58b1b3fbf44..a84bd6af531 100644 --- a/lib/intprops.h +++ b/lib/intprops.h | |||
| @@ -17,70 +17,298 @@ | |||
| 17 | 17 | ||
| 18 | /* Written by Paul Eggert. */ | 18 | /* Written by Paul Eggert. */ |
| 19 | 19 | ||
| 20 | #ifndef GL_INTPROPS_H | 20 | #ifndef _GL_INTPROPS_H |
| 21 | # define GL_INTPROPS_H | 21 | #define _GL_INTPROPS_H |
| 22 | 22 | ||
| 23 | # include <limits.h> | 23 | #include <limits.h> |
| 24 | |||
| 25 | /* Return a integer value, converted to the same type as the integer | ||
| 26 | expression E after integer type promotion. V is the unconverted value. | ||
| 27 | E should not have side effects. */ | ||
| 28 | #define _GL_INT_CONVERT(e, v) ((e) - (e) + (v)) | ||
| 24 | 29 | ||
| 25 | /* The extra casts in the following macros work around compiler bugs, | 30 | /* The extra casts in the following macros work around compiler bugs, |
| 26 | e.g., in Cray C 5.0.3.0. */ | 31 | e.g., in Cray C 5.0.3.0. */ |
| 27 | 32 | ||
| 28 | /* True if the arithmetic type T is an integer type. bool counts as | 33 | /* True if the arithmetic type T is an integer type. bool counts as |
| 29 | an integer. */ | 34 | an integer. */ |
| 30 | # define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) | 35 | #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) |
| 31 | 36 | ||
| 32 | /* True if negative values of the signed integer type T use two's | 37 | /* True if negative values of the signed integer type T use two's |
| 33 | complement, ones' complement, or signed magnitude representation, | 38 | complement, ones' complement, or signed magnitude representation, |
| 34 | respectively. Much GNU code assumes two's complement, but some | 39 | respectively. Much GNU code assumes two's complement, but some |
| 35 | people like to be portable to all possible C hosts. */ | 40 | people like to be portable to all possible C hosts. */ |
| 36 | # define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1) | 41 | #define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1) |
| 37 | # define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0) | 42 | #define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0) |
| 38 | # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1) | 43 | #define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1) |
| 44 | |||
| 45 | /* True if the signed integer expression E uses two's complement. */ | ||
| 46 | #define _GL_INT_TWOS_COMPLEMENT(e) (~ _GL_INT_CONVERT (e, 0) == -1) | ||
| 39 | 47 | ||
| 40 | /* True if the arithmetic type T is signed. */ | 48 | /* True if the arithmetic type T is signed. */ |
| 41 | # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) | 49 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) |
| 50 | |||
| 51 | /* Return 1 if the integer expression E, after integer promotion, has | ||
| 52 | a signed type. E should not have side effects. */ | ||
| 53 | #define _GL_INT_SIGNED(e) (_GL_INT_CONVERT (e, -1) < 0) | ||
| 42 | 54 | ||
| 43 | /* The maximum and minimum values for the integer type T. These | 55 | |
| 56 | /* Minimum and maximum values for integer types and expressions. These | ||
| 44 | macros have undefined behavior if T is signed and has padding bits. | 57 | macros have undefined behavior if T is signed and has padding bits. |
| 45 | If this is a problem for you, please let us know how to fix it for | 58 | If this is a problem for you, please let us know how to fix it for |
| 46 | your host. */ | 59 | your host. */ |
| 47 | # define TYPE_MINIMUM(t) \ | 60 | |
| 48 | ((t) (! TYPE_SIGNED (t) \ | 61 | /* The maximum and minimum values for the integer type T. */ |
| 49 | ? (t) 0 \ | 62 | #define TYPE_MINIMUM(t) \ |
| 50 | : TYPE_SIGNED_MAGNITUDE (t) \ | 63 | ((t) (! TYPE_SIGNED (t) \ |
| 51 | ? ~ (t) 0 \ | 64 | ? (t) 0 \ |
| 65 | : TYPE_SIGNED_MAGNITUDE (t) \ | ||
| 66 | ? ~ (t) 0 \ | ||
| 52 | : ~ TYPE_MAXIMUM (t))) | 67 | : ~ TYPE_MAXIMUM (t))) |
| 53 | # define TYPE_MAXIMUM(t) \ | 68 | #define TYPE_MAXIMUM(t) \ |
| 54 | ((t) (! TYPE_SIGNED (t) \ | 69 | ((t) (! TYPE_SIGNED (t) \ |
| 55 | ? (t) -1 \ | 70 | ? (t) -1 \ |
| 56 | : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1))) | 71 | : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1))) |
| 57 | 72 | ||
| 58 | /* Return zero if T can be determined to be an unsigned type. | 73 | /* The maximum and minimum values for the type of the expression E, |
| 59 | Otherwise, return 1. | 74 | after integer promotion. E should not have side effects. */ |
| 60 | When compiling with GCC, INT_STRLEN_BOUND uses this macro to obtain a | 75 | #define _GL_INT_MINIMUM(e) \ |
| 61 | tighter bound. Otherwise, it overestimates the true bound by one byte | 76 | (_GL_INT_SIGNED (e) \ |
| 62 | when applied to unsigned types of size 2, 4, 16, ... bytes. | 77 | ? - _GL_INT_TWOS_COMPLEMENT (e) - _GL_SIGNED_INT_MAXIMUM (e) \ |
| 63 | The symbol signed_type_or_expr__ is private to this header file. */ | 78 | : _GL_INT_CONVERT (e, 0)) |
| 64 | # if __GNUC__ >= 2 | 79 | #define _GL_INT_MAXIMUM(e) \ |
| 65 | # define signed_type_or_expr__(t) TYPE_SIGNED (__typeof__ (t)) | 80 | (_GL_INT_SIGNED (e) \ |
| 66 | # else | 81 | ? _GL_SIGNED_INT_MAXIMUM (e) \ |
| 67 | # define signed_type_or_expr__(t) 1 | 82 | : _GL_INT_CONVERT (e, -1)) |
| 68 | # endif | 83 | #define _GL_SIGNED_INT_MAXIMUM(e) \ |
| 84 | (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1) | ||
| 85 | |||
| 86 | |||
| 87 | /* Return 1 if the __typeof__ keyword works. This could be done by | ||
| 88 | 'configure', but for now it's easier to do it by hand. */ | ||
| 89 | #if 2 <= __GNUC__ || 0x5110 <= __SUNPRO_C | ||
| 90 | # define _GL_HAVE___TYPEOF__ 1 | ||
| 91 | #else | ||
| 92 | # define _GL_HAVE___TYPEOF__ 0 | ||
| 93 | #endif | ||
| 94 | |||
| 95 | /* Return 1 if the integer type or expression T might be signed. Return 0 | ||
| 96 | if it is definitely unsigned. This macro does not evaluate its argument, | ||
| 97 | and expands to an integer constant expression. */ | ||
| 98 | #if _GL_HAVE___TYPEOF__ | ||
| 99 | # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t)) | ||
| 100 | #else | ||
| 101 | # define _GL_SIGNED_TYPE_OR_EXPR(t) 1 | ||
| 102 | #endif | ||
| 69 | 103 | ||
| 70 | /* Bound on length of the string representing an unsigned integer | 104 | /* Bound on length of the string representing an unsigned integer |
| 71 | value representable in B bits. log10 (2.0) < 146/485. The | 105 | value representable in B bits. log10 (2.0) < 146/485. The |
| 72 | smallest value of B where this bound is not tight is 2621. */ | 106 | smallest value of B where this bound is not tight is 2621. */ |
| 73 | # define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) | 107 | #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) |
| 74 | 108 | ||
| 75 | /* Bound on length of the string representing an integer type or expression T. | 109 | /* Bound on length of the string representing an integer type or expression T. |
| 76 | Subtract 1 for the sign bit if T is signed, and then add 1 more for | 110 | Subtract 1 for the sign bit if T is signed, and then add 1 more for |
| 77 | a minus sign if needed. */ | 111 | a minus sign if needed. |
| 78 | # define INT_STRLEN_BOUND(t) \ | 112 | |
| 79 | (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT - signed_type_or_expr__ (t)) \ | 113 | Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is |
| 80 | + signed_type_or_expr__ (t)) | 114 | signed, this macro may overestimate the true bound by one byte when |
| 115 | applied to unsigned types of size 2, 4, 16, ... bytes. */ | ||
| 116 | #define INT_STRLEN_BOUND(t) \ | ||
| 117 | (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \ | ||
| 118 | - _GL_SIGNED_TYPE_OR_EXPR (t)) \ | ||
| 119 | + _GL_SIGNED_TYPE_OR_EXPR (t)) | ||
| 81 | 120 | ||
| 82 | /* Bound on buffer size needed to represent an integer type or expression T, | 121 | /* Bound on buffer size needed to represent an integer type or expression T, |
| 83 | including the terminating null. */ | 122 | including the terminating null. */ |
| 84 | # define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) | 123 | #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) |
| 124 | |||
| 125 | |||
| 126 | /* Range overflow checks. | ||
| 127 | |||
| 128 | The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C | ||
| 129 | operators might not yield numerically correct answers due to | ||
| 130 | arithmetic overflow. They do not rely on undefined or | ||
| 131 | implementation-defined behavior. Their implementations are simple | ||
| 132 | and straightforward, but they are a bit harder to use than the | ||
| 133 | INT_<op>_OVERFLOW macros described below. | ||
| 134 | |||
| 135 | Example usage: | ||
| 136 | |||
| 137 | long int i = ...; | ||
| 138 | long int j = ...; | ||
| 139 | if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX)) | ||
| 140 | printf ("multiply would overflow"); | ||
| 141 | else | ||
| 142 | printf ("product is %ld", i * j); | ||
| 143 | |||
| 144 | Restrictions on *_RANGE_OVERFLOW macros: | ||
| 145 | |||
| 146 | These macros do not check for all possible numerical problems or | ||
| 147 | undefined or unspecified behavior: they do not check for division | ||
| 148 | by zero, for bad shift counts, or for shifting negative numbers. | ||
| 149 | |||
| 150 | These macros may evaluate their arguments zero or multiple times, | ||
| 151 | so the arguments should not have side effects. The arithmetic | ||
| 152 | arguments (including the MIN and MAX arguments) must be of the same | ||
| 153 | integer type after the usual arithmetic conversions, and the type | ||
| 154 | must have minimum value MIN and maximum MAX. Unsigned types should | ||
| 155 | use a zero MIN of the proper type. | ||
| 156 | |||
| 157 | These macros are tuned for constant MIN and MAX. For commutative | ||
| 158 | operations such as A + B, they are also tuned for constant B. */ | ||
| 159 | |||
| 160 | /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic. | ||
| 161 | See above for restrictions. */ | ||
| 162 | #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \ | ||
| 163 | ((b) < 0 \ | ||
| 164 | ? (a) < (min) - (b) \ | ||
| 165 | : (max) - (b) < (a)) | ||
| 166 | |||
| 167 | /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic. | ||
| 168 | See above for restrictions. */ | ||
| 169 | #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \ | ||
| 170 | ((b) < 0 \ | ||
| 171 | ? (max) + (b) < (a) \ | ||
| 172 | : (a) < (min) + (b)) | ||
| 173 | |||
| 174 | /* Return 1 if - A would overflow in [MIN,MAX] arithmetic. | ||
| 175 | See above for restrictions. */ | ||
| 176 | #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ | ||
| 177 | ((min) < 0 \ | ||
| 178 | ? (a) < - (max) \ | ||
| 179 | : 0 < (a)) | ||
| 180 | |||
| 181 | /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. | ||
| 182 | See above for restrictions. */ | ||
| 183 | #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ | ||
| 184 | ((b) < 0 \ | ||
| 185 | ? ((a) < 0 \ | ||
| 186 | ? (a) < (max) / (b) \ | ||
| 187 | : (b) < -1 && (min) / (b) < (a)) \ | ||
| 188 | : (0 < (b) \ | ||
| 189 | && ((a) < 0 \ | ||
| 190 | ? (a) < (min) / (b) \ | ||
| 191 | : (max) / (b) < (a)))) | ||
| 192 | |||
| 193 | /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic. | ||
| 194 | See above for restrictions. Do not check for division by zero. */ | ||
| 195 | #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \ | ||
| 196 | ((min) < 0 && (b) == -1 && (a) < - (max)) | ||
| 197 | |||
| 198 | /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic. | ||
| 199 | See above for restrictions. Do not check for division by zero. | ||
| 200 | Mathematically, % should never overflow, but on x86-like hosts | ||
| 201 | INT_MIN % -1 traps, and the C standard permits this, so treat this | ||
| 202 | as an overflow too. */ | ||
| 203 | #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \ | ||
| 204 | INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max) | ||
| 205 | |||
| 206 | /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic. | ||
| 207 | See above for restrictions. Here, MIN and MAX are for A only, and B need | ||
| 208 | not be of the same type as the other arguments. The C standard says that | ||
| 209 | behavior is undefined for shifts unless 0 <= B < wordwidth, and that when | ||
| 210 | A is negative then A << B has undefined behavior and A >> B has | ||
| 211 | implementation-defined behavior, but do not check these other | ||
| 212 | restrictions. */ | ||
| 213 | #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ | ||
| 214 | ((a) < 0 \ | ||
| 215 | ? (a) < (min) >> (b) \ | ||
| 216 | : (max) >> (b) < (a)) | ||
| 217 | |||
| 218 | |||
| 219 | /* The _GL*_OVERFLOW macros have the same restrictions as the | ||
| 220 | *_RANGE_OVERFLOW macros, except that they do not assume that operands | ||
| 221 | (e.g., A and B) have the same type as MIN and MAX. Instead, they assume | ||
| 222 | that the result (e.g., A + B) has that type. */ | ||
| 223 | #define _GL_ADD_OVERFLOW(a, b, min, max) \ | ||
| 224 | ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ | ||
| 225 | : (a) < 0 ? (b) <= (a) + (b) \ | ||
| 226 | : (b) < 0 ? (a) <= (a) + (b) \ | ||
| 227 | : (a) + (b) < (b)) | ||
| 228 | #define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ | ||
| 229 | ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ | ||
| 230 | : (a) < 0 ? 1 \ | ||
| 231 | : (b) < 0 ? (a) - (b) <= (a) \ | ||
| 232 | : (a) < (b)) | ||
| 233 | #define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ | ||
| 234 | (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ | ||
| 235 | || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) | ||
| 236 | #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ | ||
| 237 | ((min) < 0 ? (b) == _GL_INT_CONVERT (min, -1) && (a) < - (max) \ | ||
| 238 | : (a) < 0 ? (b) <= (a) + (b) - 1 \ | ||
| 239 | : (b) < 0 && (a) + (b) <= (a)) | ||
| 240 | #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \ | ||
| 241 | ((min) < 0 ? (b) == _GL_INT_CONVERT (min, -1) && (a) < - (max) \ | ||
| 242 | : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \ | ||
| 243 | : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max)) | ||
| 244 | |||
| 245 | /* Return a nonzero value if A is a mathematical multiple of B, where | ||
| 246 | A is unsigned, B is negative, and MAX is the maximum value of A's | ||
| 247 | type. A's type must be the same as (A % B)'s type. Normally (A % | ||
| 248 | -B == 0) suffices, but things get tricky if -B would overflow. */ | ||
| 249 | #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \ | ||
| 250 | (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \ | ||
| 251 | ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \ | ||
| 252 | ? (a) \ | ||
| 253 | : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \ | ||
| 254 | : (a) % - (b)) \ | ||
| 255 | == 0) | ||
| 256 | |||
| 257 | |||
| 258 | /* Integer overflow checks. | ||
| 259 | |||
| 260 | The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators | ||
| 261 | might not yield numerically correct answers due to arithmetic overflow. | ||
| 262 | They work correctly on all known practical hosts, and do not rely | ||
| 263 | on undefined behavior due to signed arithmetic overflow. | ||
| 264 | |||
| 265 | Example usage: | ||
| 266 | |||
| 267 | long int i = ...; | ||
| 268 | long int j = ...; | ||
| 269 | if (INT_MULTIPLY_OVERFLOW (i, j)) | ||
| 270 | printf ("multiply would overflow"); | ||
| 271 | else | ||
| 272 | printf ("product is %ld", i * j); | ||
| 273 | |||
| 274 | These macros do not check for all possible numerical problems or | ||
| 275 | undefined or unspecified behavior: they do not check for division | ||
| 276 | by zero, for bad shift counts, or for shifting negative numbers. | ||
| 277 | |||
| 278 | These macros may evaluate their arguments zero or multiple times, so the | ||
| 279 | arguments should not have side effects. | ||
| 280 | |||
| 281 | These macros are tuned for their last argument being a constant. | ||
| 282 | |||
| 283 | Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, | ||
| 284 | A % B, and A << B would overflow, respectively. */ | ||
| 285 | |||
| 286 | #define INT_ADD_OVERFLOW(a, b) \ | ||
| 287 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) | ||
| 288 | #define INT_SUBTRACT_OVERFLOW(a, b) \ | ||
| 289 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) | ||
| 290 | #define INT_NEGATE_OVERFLOW(a) \ | ||
| 291 | INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) | ||
| 292 | #define INT_MULTIPLY_OVERFLOW(a, b) \ | ||
| 293 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) | ||
| 294 | #define INT_DIVIDE_OVERFLOW(a, b) \ | ||
| 295 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW) | ||
| 296 | #define INT_REMAINDER_OVERFLOW(a, b) \ | ||
| 297 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW) | ||
| 298 | #define INT_LEFT_SHIFT_OVERFLOW(a, b) \ | ||
| 299 | INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ | ||
| 300 | _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) | ||
| 301 | |||
| 302 | /* Return 1 if the expression A <op> B would overflow, | ||
| 303 | where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test, | ||
| 304 | assuming MIN and MAX are the minimum and maximum for the result type. | ||
| 305 | |||
| 306 | This macro assumes that A | B is a valid integer if both A and B are, | ||
| 307 | which is true of all known practical hosts. If this is a problem | ||
| 308 | for you, please let us know how to fix it for your host. */ | ||
| 309 | #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ | ||
| 310 | op_result_overflow (a, b, \ | ||
| 311 | _GL_INT_MINIMUM ((a) | (b)), \ | ||
| 312 | _GL_INT_MAXIMUM ((a) | (b))) | ||
| 85 | 313 | ||
| 86 | #endif /* GL_INTPROPS_H */ | 314 | #endif /* _GL_INTPROPS_H */ |
diff --git a/lib/unistd.in.h b/lib/unistd.in.h index 3b58d0f42a5..59dd5709877 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h | |||
| @@ -871,6 +871,22 @@ _GL_WARN_ON_USE (endusershell, "endusershell is unportable - " | |||
| 871 | #endif | 871 | #endif |
| 872 | 872 | ||
| 873 | 873 | ||
| 874 | #if @GNULIB_GROUP_MEMBER@ | ||
| 875 | /* Determine whether group id is in calling user's group list. */ | ||
| 876 | # if !@HAVE_GROUP_MEMBER@ | ||
| 877 | _GL_FUNCDECL_SYS (group_member, int, (gid_t gid)); | ||
| 878 | # endif | ||
| 879 | _GL_CXXALIAS_SYS (group_member, int, (gid_t gid)); | ||
| 880 | _GL_CXXALIASWARN (group_member); | ||
| 881 | #elif defined GNULIB_POSIXCHECK | ||
| 882 | # undef group_member | ||
| 883 | # if HAVE_RAW_DECL_GROUP_MEMBER | ||
| 884 | _GL_WARN_ON_USE (group_member, "group_member is unportable - " | ||
| 885 | "use gnulib module group-member for portability"); | ||
| 886 | # endif | ||
| 887 | #endif | ||
| 888 | |||
| 889 | |||
| 874 | #if @GNULIB_LCHOWN@ | 890 | #if @GNULIB_LCHOWN@ |
| 875 | /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE | 891 | /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE |
| 876 | to GID (if GID is not -1). Do not follow symbolic links. | 892 | to GID (if GID is not -1). Do not follow symbolic links. |