exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / xsize.h
blob71c9c07322e5b7e10dcb6e536e02d7f8a85a173c
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/>. */
18 #ifndef _XSIZE_H
19 #define _XSIZE_H
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."
24 #endif
26 /* Get size_t. */
27 #include <stddef.h>
29 /* Get SIZE_MAX. */
30 #include <limits.h>
31 #if HAVE_STDINT_H
32 # include <stdint.h>
33 #endif
35 /* Get ATTRIBUTE_PURE. */
36 #include "attribute.h"
38 _GL_INLINE_HEADER_BEGIN
39 #ifndef XSIZE_INLINE
40 # define XSIZE_INLINE _GL_INLINE
41 #endif
43 /* The size of memory objects is often computed through expressions of
44 type size_t. Example:
45 void* p = malloc (header_size + n * element_size).
46 These computations can lead to overflow. When this happens, malloc()
47 returns a piece of memory that is way too small, and the program then
48 crashes while attempting to fill the memory.
49 To avoid this, the functions and macros in this file check for overflow.
50 The convention is that SIZE_MAX represents overflow.
51 malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
52 implementation that uses mmap --, it's recommended to use size_overflow_p()
53 or size_in_bounds_p() before invoking malloc().
54 The example thus becomes:
55 size_t size = xsum (header_size, xtimes (n, element_size));
56 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
59 /* Convert an arbitrary value >= 0 to type size_t. */
60 #define xcast_size_t(N) \
61 ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
63 /* Sum of two sizes, with overflow check. */
64 XSIZE_INLINE size_t ATTRIBUTE_PURE
65 xsum (size_t size1, size_t size2)
67 size_t sum = size1 + size2;
68 return (sum >= size1 ? sum : SIZE_MAX);
71 /* Sum of three sizes, with overflow check. */
72 XSIZE_INLINE size_t ATTRIBUTE_PURE
73 xsum3 (size_t size1, size_t size2, size_t size3)
75 return xsum (xsum (size1, size2), size3);
78 /* Sum of four sizes, with overflow check. */
79 XSIZE_INLINE size_t ATTRIBUTE_PURE
80 xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
82 return xsum (xsum (xsum (size1, size2), size3), size4);
85 /* Maximum of two sizes, with overflow check. */
86 XSIZE_INLINE size_t ATTRIBUTE_PURE
87 xmax (size_t size1, size_t size2)
89 /* No explicit check is needed here, because for any n:
90 max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
91 return (size1 >= size2 ? size1 : size2);
94 /* Multiplication of a count with an element size, with overflow check.
95 The count must be >= 0 and the element size must be > 0.
96 This is a macro, not a function, so that it works correctly even
97 when N is of a wider type and N > SIZE_MAX. */
98 #define xtimes(N, ELSIZE) \
99 ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
101 /* Check for overflow. */
102 #define size_overflow_p(SIZE) \
103 ((SIZE) == SIZE_MAX)
104 /* Check against overflow. */
105 #define size_in_bounds_p(SIZE) \
106 ((SIZE) != SIZE_MAX)
108 _GL_INLINE_HEADER_END
110 #endif /* _XSIZE_H */