1 /* xsize.h -- Checked size_t computations.
3 Copyright (C) 2003, 2008-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, HAVE_STDINT_H. */
22 #if !_GL_CONFIG_H_INCLUDED
23 #error "Please include config.h first."
35 /* Get ATTRIBUTE_PURE. */
36 #include "attribute.h"
38 _GL_INLINE_HEADER_BEGIN
40 # define XSIZE_INLINE _GL_INLINE
48 /* The size of memory objects is often computed through expressions of
50 void* p = malloc (header_size + n * element_size).
51 These computations can lead to overflow. When this happens, malloc()
52 returns a piece of memory that is way too small, and the program then
53 crashes while attempting to fill the memory.
54 To avoid this, the functions and macros in this file check for overflow.
55 The convention is that SIZE_MAX represents overflow.
56 malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
57 implementation that uses mmap --, it's recommended to use size_overflow_p()
58 or size_in_bounds_p() before invoking malloc().
59 The example thus becomes:
60 size_t size = xsum (header_size, xtimes (n, element_size));
61 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
64 /* Convert an arbitrary value >= 0 to type size_t. */
65 #define xcast_size_t(N) \
66 ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
68 /* Sum of two sizes, with overflow check. */
69 XSIZE_INLINE
size_t ATTRIBUTE_PURE
70 xsum (size_t size1
, size_t size2
)
72 size_t sum
= size1
+ size2
;
73 return (sum
>= size1
? sum
: SIZE_MAX
);
76 /* Sum of three sizes, with overflow check. */
77 XSIZE_INLINE
size_t ATTRIBUTE_PURE
78 xsum3 (size_t size1
, size_t size2
, size_t size3
)
80 return xsum (xsum (size1
, size2
), size3
);
83 /* Sum of four sizes, with overflow check. */
84 XSIZE_INLINE
size_t ATTRIBUTE_PURE
85 xsum4 (size_t size1
, size_t size2
, size_t size3
, size_t size4
)
87 return xsum (xsum (xsum (size1
, size2
), size3
), size4
);
90 /* Maximum of two sizes, with overflow check. */
91 XSIZE_INLINE
size_t ATTRIBUTE_PURE
92 xmax (size_t size1
, size_t size2
)
94 /* No explicit check is needed here, because for any n:
95 max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
96 return (size1
>= size2
? size1
: size2
);
99 /* Multiplication of a count with an element size, with overflow check.
100 The count must be >= 0 and the element size must be > 0.
101 This is a macro, not a function, so that it works correctly even
102 when N is of a wider type and N > SIZE_MAX. */
103 #define xtimes(N, ELSIZE) \
104 ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
106 /* Check for overflow. */
107 #define size_overflow_p(SIZE) \
109 /* Check against overflow. */
110 #define size_in_bounds_p(SIZE) \
118 _GL_INLINE_HEADER_END
120 #endif /* _XSIZE_H */