README-release: make it easier to execute commands
[gnulib/ericb.git] / doc / intprops.texi
blob8780886beed3e001eb612cf77fd3005c5b0e0680
1 @node Integer Properties
2 @section Integer Properties
4 @c Copyright (C) 2011-2012 Free Software Foundation, Inc.
6 @c Permission is granted to copy, distribute and/or modify this document
7 @c under the terms of the GNU Free Documentation License, Version 1.3 or
8 @c any later version published by the Free Software Foundation; with no
9 @c Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
10 @c Texts.  A copy of the license is included in the ``GNU Free
11 @c Documentation License'' file as part of this distribution.
13 @c Written by Paul Eggert.
15 @cindex integer properties
17 The @code{intprops} module consists of an include file @code{<intprops.h>}
18 that defines several macros useful for testing properties of integer
19 types.
21 @cindex integer overflow
22 @cindex overflow, integer
24 Integer overflow is a common source of problems in programs written in
25 C and other languages.  In some cases, such as signed integer
26 arithmetic in C programs, the resulting behavior is undefined, and
27 practical platforms do not always behave as if integers wrap around
28 reliably.  In other cases, such as unsigned integer arithmetic in C,
29 the resulting behavior is well-defined, but programs may still
30 misbehave badly after overflow occurs.
32 Many techniques have been proposed to attack these problems.  These
33 include precondition testing, GCC's @option{-ftrapv} option, GCC's
34 no-undefined-overflow branch, the As-if Infinitely Ranged (AIR) model
35 implemented in Clang, saturation semantics where overflow reliably
36 yields an extreme value, the RICH static transformer to an
37 overflow-checking variant, and special testing methods.  For more
38 information about these techniques, see: Dannenberg R, Dormann W,
39 Keaton D @emph{et al.},
40 @url{http://www.sei.cmu.edu/library/abstracts/reports/10tn008.cfm,
41 As-if Infinitely Ranged integer model -- 2nd ed.}, Software Engineering
42 Institute Technical Note CMU/SEI-2010-TN-008, April 2010.
44 Gnulib supports the precondition testing technique, as this is easy to
45 support portably.  There are two families of precondition tests: the
46 first, for integer ranges, has a simple and straightforward implementation,
47 while the second, for integer types, is easier to use.
49 @menu
50 * Integer Type Determination::  Whether a type has integer properties.
51 * Integer Bounds::              Bounds on integer values and representations.
52 * Integer Range Overflow::      Integer overflow checking if bounds are known.
53 * Integer Type Overflow::       General integer overflow checking.
54 @end menu
56 @node Integer Type Determination
57 @subsection Integer Type Determination
59 @findex TYPE_IS_INTEGER
60 @code{TYPE_IS_INTEGER (@var{t})} expands to a constant
61 expression that is 1 if the arithmetic type @var{t} is an integer type.
62 @code{_Bool} counts as an integer type.
64 @findex TYPE_SIGNED
65 @code{TYPE_SIGNED (@var{t})} expands to a constant expression
66 that is 1 if the arithmetic type @var{t} is a signed integer type or a
67 floating type.  If @var{t} is an integer type, @code{TYPE_SIGNED (@var{t})}
68 expands to an integer constant expression.
70 Example usage:
72 @example
73 #include <intprops.h>
74 #include <time.h>
75 enum
77   time_t_is_signed_integer =
78     TYPE_IS_INTEGER (time_t) && TYPE_SIGNED (time_t)
79 @};
80 @end example
82 @node Integer Bounds
83 @subsection Integer Bounds
85 @cindex integer bounds
87 @findex INT_BUFSIZE_BOUND
88 @code{INT_BUFSIZE_BOUND (@var{t})} expands to an integer constant
89 expression that is a bound on the size of the string representing an
90 integer type or expression @var{t} in decimal notation, including the
91 terminating null character and any leading @code{-} character.  For
92 example, if @code{INT_STRLEN_BOUND (int)} is 12, any value of type
93 @code{int} can be represented in 12 bytes or less, including the
94 terminating null.  The bound is not necessarily tight.
96 Example usage:
98 @example
99 #include <intprops.h>
100 #include <stdio.h>
102 int_strlen (int i)
104   char buf[INT_BUFSIZE_BOUND (int)];
105   return sprintf (buf, "%d", i);
107 @end example
109 @findex INT_STRLEN_BOUND
110 @code{INT_STRLEN_BOUND (@var{t})} expands to an integer constant
111 expression that is a bound on the length of the string representing an
112 integer type or expression @var{t} in decimal notation, including any
113 leading @code{-} character.  This is one less than
114 @code{INT_BUFSIZE_BOUND (@var{t})}.
116 @findex TYPE_MINIMUM
117 @findex TYPE_MAXIMUM
118 @code{TYPE_MINIMUM (@var{t})} and @code{TYPE_MAXIMUM (@var{t})} expand
119 to integer constant expressions equal to the minimum and maximum
120 values of the integer type @var{t}.  These expressions are of the type
121 @var{t} (or more precisely, the type @var{t} after integer
122 promotions).
124 Example usage:
126 @example
127 #include <stdint.h>
128 #include <sys/types.h>
129 #include <intprops.h>
131 in_off_t_range (intmax_t a)
133   return TYPE_MINIMUM (off_t) <= a && a <= TYPE_MAXIMUM (off_t);
135 @end example
137 @node Integer Range Overflow
138 @subsection Integer Range Overflow
140 @cindex integer range overflow
141 @cindex overflow, integer range
143 These macros yield 1 if the corresponding C operators might not yield
144 numerically correct answers due to arithmetic overflow.  They do not
145 rely on undefined or implementation-defined behavior.  They expand to
146 integer constant expressions if their arguments are.  Their
147 implementations are simple and straightforward, but they are typically
148 harder to use than the integer type overflow macros.  @xref{Integer
149 Type Overflow}.
151 Although the implementation of these macros is similar to that
152 suggested in Seacord R, The CERT C Secure Coding Standard (2009,
153 revised 2011), in its two sections
154 ``@url{https://www.securecoding.cert.org/confluence/display/seccode/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap,
155 INT30-C. Ensure that unsigned integer operations do not wrap}'' and
156 ``@url{https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow,
157 INT32-C. Ensure that operations on signed integers do not result in
158 overflow}'', Gnulib's implementation was derived independently of
159 CERT's suggestions.
161 Example usage:
163 @example
164 #include <intprops.h>
165 void
166 print_product (long int a, long int b)
168   if (INT_MULTIPLY_RANGE_OVERFLOW (a, b, LONG_MIN, LONG_MAX))
169     printf ("multiply would overflow");
170   else
171     printf ("product is %ld", a * b);
173 @end example
175 @noindent
176 These macros have the following restrictions:
178 @itemize @bullet
179 @item
180 Their arguments must be integer expressions.
182 @item
183 They may evaluate their arguments zero or multiple times, so
184 the arguments should not have side effects.
186 @item
187 The arithmetic arguments (including the @var{min} and @var{max}
188 arguments) must be of the same integer type after the usual arithmetic
189 conversions, and the type must have minimum value @var{min} and
190 maximum @var{max}.  Unsigned values should use a zero @var{min} of the
191 proper type, for example, @code{(unsigned int) 0}.
192 @end itemize
194 These macros are tuned for constant @var{min} and @var{max}.  For
195 commutative operations such as @code{@var{a} + @var{b}}, they are also
196 tuned for constant @var{b}.
198 @table @code
199 @item INT_ADD_RANGE_OVERFLOW (@var{a}, @var{b}, @var{min}, @var{max})
200 @findex INT_ADD_RANGE_OVERFLOW
201 Yield 1 if @code{@var{a} + @var{b}} would overflow in
202 [@var{min},@var{max}] integer arithmetic.  See above for restrictions.
204 @item INT_SUBTRACT_RANGE_OVERFLOW (@var{a}, @var{b}, @var{min}, @var{max})
205 @findex INT_SUBTRACT_RANGE_OVERFLOW
206 Yield 1 if @code{@var{a} - @var{b}} would overflow in
207 [@var{min},@var{max}] integer arithmetic.  See above for restrictions.
209 @item INT_NEGATE_RANGE_OVERFLOW (@var{a}, @var{min}, @var{max})
210 @findex INT_NEGATE_RANGE_OVERFLOW
211 Yield 1 if @code{-@var{a}} would overflow in [@var{min},@var{max}]
212 integer arithmetic.  See above for restrictions.
214 @item INT_MULTIPLY_RANGE_OVERFLOW (@var{a}, @var{b}, @var{min}, @var{max})
215 @findex INT_MULTIPLY_RANGE_OVERFLOW
216 Yield 1 if @code{@var{a} * @var{b}} would overflow in
217 [@var{min},@var{max}] integer arithmetic.  See above for restrictions.
219 @item INT_DIVIDE_RANGE_OVERFLOW (@var{a}, @var{b}, @var{min}, @var{max})
220 @findex INT_DIVIDE_RANGE_OVERFLOW
221 Yield 1 if @code{@var{a} / @var{b}} would overflow in
222 [@var{min},@var{max}] integer arithmetic.  See above for restrictions.
223 Division overflow can happen on two's complement hosts when dividing
224 the most negative integer by @minus{}1.  This macro does not check for
225 division by zero.
227 @item INT_REMAINDER_RANGE_OVERFLOW (@var{a}, @var{b}, @var{min}, @var{max})
228 @findex INT_REMAINDER_RANGE_OVERFLOW
229 Yield 1 if @code{@var{a} % @var{b}} would overflow in
230 [@var{min},@var{max}] integer arithmetic.  See above for restrictions.
231 Remainder overflow can happen on two's complement hosts when dividing
232 the most negative integer by @minus{}1; although the mathematical
233 result is always 0, in practice some implementations trap, so this
234 counts as an overflow.  This macro does not check for division by
235 zero.
237 @item INT_LEFT_SHIFT_RANGE_OVERFLOW (@var{a}, @var{b}, @var{min}, @var{max})
238 @findex INT_LEFT_SHIFT_RANGE_OVERFLOW
239 Yield 1 if @code{@var{a} << @var{b}} would overflow in
240 [@var{min},@var{max}] integer arithmetic.  See above for restrictions.
241 Here, @var{min} and @var{max} are for @var{a} only, and @var{b} need
242 not be of the same type as the other arguments.  The C standard says
243 that behavior is undefined for shifts unless 0@leq{}@var{b}<@var{w}
244 where @var{w} is @var{a}'s word width, and that when @var{a} is negative
245 then @code{@var{a} << @var{b}} has undefined behavior and
246 @code{@var{a} >> @var{b}} has implementation-defined behavior, but
247 this macro does not check these other restrictions.
248 @end table
250 @node Integer Type Overflow
251 @subsection Integer Type Overflow
253 @cindex integer type overflow
254 @cindex overflow, integer type
256 These macros yield 1 if the corresponding C operators might not yield
257 numerically correct answers due to arithmetic overflow of an integer
258 type.  They work correctly on all known practical hosts, and do not
259 rely on undefined behavior due to signed arithmetic overflow.  They
260 expand to integer constant expressions if their arguments are.  They
261 are easier to use than the integer range overflow macros
262 (@pxref{Integer Range Overflow}).
264 Example usage:
266 @example
267 #include <intprops.h>
268 void
269 print_product (long int a, long int b)
271   if (INT_MULTIPLY_OVERFLOW (a, b))
272     printf ("multiply would overflow");
273   else
274     printf ("product is %ld", a * b);
276 @end example
278 @noindent
279 These macros have the following restrictions:
281 @itemize @bullet
282 @item
283 Their arguments must be integer expressions.
285 @item
286 They may evaluate their arguments zero or multiple times, so the
287 arguments should not have side effects.
288 @end itemize
290 These macros are tuned for their last argument being a constant.
292 @table @code
293 @item INT_ADD_OVERFLOW (@var{a}, @var{b})
294 @findex INT_ADD_OVERFLOW
295 Yield 1 if @code{@var{a} + @var{b}} would overflow.  See above for
296 restrictions.
298 @item INT_SUBTRACT_OVERFLOW (@var{a}, @var{b})
299 @findex INT_SUBTRACT_OVERFLOW
300 Yield 1 if @code{@var{a} - @var{b}} would overflow.  See above for
301 restrictions.
303 @item INT_NEGATE_OVERFLOW (@var{a})
304 @findex INT_NEGATE_OVERFLOW
305 Yields 1 if @code{-@var{a}} would overflow.  See above for restrictions.
307 @item INT_MULTIPLY_OVERFLOW (@var{a}, @var{b})
308 @findex INT_MULTIPLY_OVERFLOW
309 Yield 1 if @code{@var{a} * @var{b}} would overflow.  See above for
310 restrictions.
312 @item INT_DIVIDE_OVERFLOW (@var{a}, @var{b})
313 @findex INT_DIVIDE_OVERFLOW
314 Yields 1 if @code{@var{a} / @var{b}} would overflow.  See above for
315 restrictions.  Division overflow can happen on two's complement hosts
316 when dividing the most negative integer by @minus{}1.  This macro does
317 not check for division by zero.
319 @item INT_REMAINDER_OVERFLOW (@var{a}, @var{b})
320 @findex INT_REMAINDER_OVERFLOW
321 Yield 1 if @code{@var{a} % @var{b}} would overflow.  See above for
322 restrictions.  Remainder overflow can happen on two's complement hosts
323 when dividing the most negative integer by @minus{}1; although the
324 mathematical result is always 0, in practice some implementations
325 trap, so this counts as an overflow.  This macro does not check for
326 division by zero.
328 @item INT_LEFT_SHIFT_OVERFLOW (@var{a}, @var{b})
329 @findex INT_LEFT_SHIFT_OVERFLOW
330 Yield 1 if @code{@var{a} << @var{b}} would overflow.  See above for
331 restrictions.  The C standard says that behavior is undefined for
332 shifts unless 0@leq{}@var{b}<@var{w} where @var{w} is @var{a}'s word
333 width, and that when @var{a} is negative then @code{@var{a} <<
334 @var{b}} has undefined behavior and @code{@var{a} >> @var{b}} has
335 implementation-defined behavior, but this macro does not check these
336 other restrictions.
337 @end table