1 /* xsize.h -- Checked size_t computations.
3 Copyright (C) 2003, 2008-2018 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
30 #ifndef _GL_INLINE_HEADER_BEGIN
31 #error "Please include config.h first."
33 _GL_INLINE_HEADER_BEGIN
35 # define XSIZE_INLINE _GL_INLINE
38 /* The size of memory objects is often computed through expressions of
40 void* p = malloc (header_size + n * element_size).
41 These computations can lead to overflow. When this happens, malloc()
42 returns a piece of memory that is way too small, and the program then
43 crashes while attempting to fill the memory.
44 To avoid this, the functions and macros in this file check for overflow.
45 The convention is that SIZE_MAX represents overflow.
46 malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
47 implementation that uses mmap --, it's recommended to use size_overflow_p()
48 or size_in_bounds_p() before invoking malloc().
49 The example thus becomes:
50 size_t size = xsum (header_size, xtimes (n, element_size));
51 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
54 /* Convert an arbitrary value >= 0 to type size_t. */
55 #define xcast_size_t(N) \
56 ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
58 /* Sum of two sizes, with overflow check. */
61 __attribute__ ((__pure__
))
63 xsum (size_t size1
, size_t size2
)
65 size_t sum
= size1
+ size2
;
66 return (sum
>= size1
? sum
: SIZE_MAX
);
69 /* Sum of three sizes, with overflow check. */
72 __attribute__ ((__pure__
))
74 xsum3 (size_t size1
, size_t size2
, size_t size3
)
76 return xsum (xsum (size1
, size2
), size3
);
79 /* Sum of four sizes, with overflow check. */
82 __attribute__ ((__pure__
))
84 xsum4 (size_t size1
, size_t size2
, size_t size3
, size_t size4
)
86 return xsum (xsum (xsum (size1
, size2
), size3
), size4
);
89 /* Maximum of two sizes, with overflow check. */
92 __attribute__ ((__pure__
))
94 xmax (size_t size1
, size_t size2
)
96 /* No explicit check is needed here, because for any n:
97 max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
98 return (size1
>= size2
? size1
: size2
);
101 /* Multiplication of a count with an element size, with overflow check.
102 The count must be >= 0 and the element size must be > 0.
103 This is a macro, not a function, so that it works correctly even
104 when N is of a wider type and N > SIZE_MAX. */
105 #define xtimes(N, ELSIZE) \
106 ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
108 /* Check for overflow. */
109 #define size_overflow_p(SIZE) \
111 /* Check against overflow. */
112 #define size_in_bounds_p(SIZE) \
115 _GL_INLINE_HEADER_END
117 #endif /* _XSIZE_H */