a0bd76650e104dd4df02056db3bcc14ac40ddce4
[debian/tar] / gnu / xsize.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* xsize.h -- Checked size_t computations.
4
5    Copyright (C) 2003, 2008-2013 Free Software Foundation, Inc.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifndef _XSIZE_H
21 #define _XSIZE_H
22
23 /* Get size_t.  */
24 #include <stddef.h>
25
26 /* Get SIZE_MAX.  */
27 #include <limits.h>
28 #if HAVE_STDINT_H
29 # include <stdint.h>
30 #endif
31
32 _GL_INLINE_HEADER_BEGIN
33 #ifndef XSIZE_INLINE
34 # define XSIZE_INLINE _GL_INLINE
35 #endif
36
37 /* The size of memory objects is often computed through expressions of
38    type size_t. Example:
39       void* p = malloc (header_size + n * element_size).
40    These computations can lead to overflow.  When this happens, malloc()
41    returns a piece of memory that is way too small, and the program then
42    crashes while attempting to fill the memory.
43    To avoid this, the functions and macros in this file check for overflow.
44    The convention is that SIZE_MAX represents overflow.
45    malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
46    implementation that uses mmap --, it's recommended to use size_overflow_p()
47    or size_in_bounds_p() before invoking malloc().
48    The example thus becomes:
49       size_t size = xsum (header_size, xtimes (n, element_size));
50       void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
51 */
52
53 /* Convert an arbitrary value >= 0 to type size_t.  */
54 #define xcast_size_t(N) \
55   ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
56
57 /* Sum of two sizes, with overflow check.  */
58 XSIZE_INLINE size_t
59 #if __GNUC__ >= 3
60 __attribute__ ((__pure__))
61 #endif
62 xsum (size_t size1, size_t size2)
63 {
64   size_t sum = size1 + size2;
65   return (sum >= size1 ? sum : SIZE_MAX);
66 }
67
68 /* Sum of three sizes, with overflow check.  */
69 XSIZE_INLINE size_t
70 #if __GNUC__ >= 3
71 __attribute__ ((__pure__))
72 #endif
73 xsum3 (size_t size1, size_t size2, size_t size3)
74 {
75   return xsum (xsum (size1, size2), size3);
76 }
77
78 /* Sum of four sizes, with overflow check.  */
79 XSIZE_INLINE size_t
80 #if __GNUC__ >= 3
81 __attribute__ ((__pure__))
82 #endif
83 xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
84 {
85   return xsum (xsum (xsum (size1, size2), size3), size4);
86 }
87
88 /* Maximum of two sizes, with overflow check.  */
89 XSIZE_INLINE size_t
90 #if __GNUC__ >= 3
91 __attribute__ ((__pure__))
92 #endif
93 xmax (size_t size1, size_t size2)
94 {
95   /* No explicit check is needed here, because for any n:
96      max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX.  */
97   return (size1 >= size2 ? size1 : size2);
98 }
99
100 /* Multiplication of a count with an element size, with overflow check.
101    The count must be >= 0 and the element size must be > 0.
102    This is a macro, not a function, so that it works correctly even
103    when N is of a wider type and N > SIZE_MAX.  */
104 #define xtimes(N, ELSIZE) \
105   ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
106
107 /* Check for overflow.  */
108 #define size_overflow_p(SIZE) \
109   ((SIZE) == SIZE_MAX)
110 /* Check against overflow.  */
111 #define size_in_bounds_p(SIZE) \
112   ((SIZE) != SIZE_MAX)
113
114 _GL_INLINE_HEADER_END
115
116 #endif /* _XSIZE_H */