Import upstream version 1.28
[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-2014 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 #ifndef _GL_INLINE_HEADER_BEGIN
33  #error "Please include config.h first."
34 #endif
35 _GL_INLINE_HEADER_BEGIN
36 #ifndef XSIZE_INLINE
37 # define XSIZE_INLINE _GL_INLINE
38 #endif
39
40 /* The size of memory objects is often computed through expressions of
41    type size_t. Example:
42       void* p = malloc (header_size + n * element_size).
43    These computations can lead to overflow.  When this happens, malloc()
44    returns a piece of memory that is way too small, and the program then
45    crashes while attempting to fill the memory.
46    To avoid this, the functions and macros in this file check for overflow.
47    The convention is that SIZE_MAX represents overflow.
48    malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
49    implementation that uses mmap --, it's recommended to use size_overflow_p()
50    or size_in_bounds_p() before invoking malloc().
51    The example thus becomes:
52       size_t size = xsum (header_size, xtimes (n, element_size));
53       void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
54 */
55
56 /* Convert an arbitrary value >= 0 to type size_t.  */
57 #define xcast_size_t(N) \
58   ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
59
60 /* Sum of two sizes, with overflow check.  */
61 XSIZE_INLINE size_t
62 #if __GNUC__ >= 3
63 __attribute__ ((__pure__))
64 #endif
65 xsum (size_t size1, size_t size2)
66 {
67   size_t sum = size1 + size2;
68   return (sum >= size1 ? sum : SIZE_MAX);
69 }
70
71 /* Sum of three sizes, with overflow check.  */
72 XSIZE_INLINE size_t
73 #if __GNUC__ >= 3
74 __attribute__ ((__pure__))
75 #endif
76 xsum3 (size_t size1, size_t size2, size_t size3)
77 {
78   return xsum (xsum (size1, size2), size3);
79 }
80
81 /* Sum of four sizes, with overflow check.  */
82 XSIZE_INLINE size_t
83 #if __GNUC__ >= 3
84 __attribute__ ((__pure__))
85 #endif
86 xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
87 {
88   return xsum (xsum (xsum (size1, size2), size3), size4);
89 }
90
91 /* Maximum of two sizes, with overflow check.  */
92 XSIZE_INLINE size_t
93 #if __GNUC__ >= 3
94 __attribute__ ((__pure__))
95 #endif
96 xmax (size_t size1, size_t size2)
97 {
98   /* No explicit check is needed here, because for any n:
99      max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX.  */
100   return (size1 >= size2 ? size1 : size2);
101 }
102
103 /* Multiplication of a count with an element size, with overflow check.
104    The count must be >= 0 and the element size must be > 0.
105    This is a macro, not a function, so that it works correctly even
106    when N is of a wider type and N > SIZE_MAX.  */
107 #define xtimes(N, ELSIZE) \
108   ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
109
110 /* Check for overflow.  */
111 #define size_overflow_p(SIZE) \
112   ((SIZE) == SIZE_MAX)
113 /* Check against overflow.  */
114 #define size_in_bounds_p(SIZE) \
115   ((SIZE) != SIZE_MAX)
116
117 _GL_INLINE_HEADER_END
118
119 #endif /* _XSIZE_H */