Merge branch 'g-clear-pointer-no-side-effects' into 'master'
[glib.git] / glib / gnulib / xsize.h
bloba6f65bc0d49928997b374030a237cb4db5bba0cc
1 /* xsize.h -- Checked size_t computations.
3 Copyright (C) 2003, 2008-2016 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
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 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 <http://www.gnu.org/licenses/>. */
18 #ifndef _XSIZE_H
19 #define _XSIZE_H
21 #include <glib.h>
23 /* Get size_t. */
24 #include <stddef.h>
26 /* Get G_MAXSIZE. */
27 #include <limits.h>
28 #if HAVE_STDINT_H
29 # include <stdint.h>
30 #endif
32 #ifndef XSIZE_INLINE
33 # define XSIZE_INLINE _GL_INLINE
34 #endif
36 /* The size of memory objects is often computed through expressions of
37 type size_t. Example:
38 void* p = malloc (header_size + n * element_size).
39 These computations can lead to overflow. When this happens, malloc()
40 returns a piece of memory that is way too small, and the program then
41 crashes while attempting to fill the memory.
42 To avoid this, the functions and macros in this file check for overflow.
43 The convention is that G_MAXSIZE represents overflow.
44 malloc (G_MAXSIZE) is not guaranteed to fail -- think of a malloc
45 implementation that uses mmap --, it's recommended to use size_overflow_p()
46 or size_in_bounds_p() before invoking malloc().
47 The example thus becomes:
48 size_t size = xsum (header_size, xtimes (n, element_size));
49 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
52 /* Convert an arbitrary value >= 0 to type size_t. */
53 #define xcast_size_t(N) \
54 ((N) <= G_MAXSIZE ? (size_t) (N) : G_MAXSIZE)
56 /* Sum of two sizes, with overflow check. */
57 static inline size_t
58 #if __GNUC__ >= 3
59 __attribute__ ((__pure__))
60 #endif
61 xsum (size_t size1, size_t size2)
63 size_t sum = size1 + size2;
64 return (sum >= size1 ? sum : G_MAXSIZE);
67 /* Sum of three sizes, with overflow check. */
68 static inline size_t
69 #if __GNUC__ >= 3
70 __attribute__ ((__pure__))
71 #endif
72 xsum3 (size_t size1, size_t size2, size_t size3)
74 return xsum (xsum (size1, size2), size3);
77 /* Sum of four sizes, with overflow check. */
78 static inline size_t
79 #if __GNUC__ >= 3
80 __attribute__ ((__pure__))
81 #endif
82 xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
84 return xsum (xsum (xsum (size1, size2), size3), size4);
87 /* Maximum of two sizes, with overflow check. */
88 static inline size_t
89 #if __GNUC__ >= 3
90 __attribute__ ((__pure__))
91 #endif
92 xmax (size_t size1, size_t size2)
94 /* No explicit check is needed here, because for any n:
95 max (G_MAXSIZE, n) == G_MAXSIZE and max (n, G_MAXSIZE) == G_MAXSIZE. */
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 > G_MAXSIZE. */
103 #define xtimes(N, ELSIZE) \
104 ((N) <= G_MAXSIZE / (ELSIZE) ? (size_t) (N) * (ELSIZE) : G_MAXSIZE)
106 /* Check for overflow. */
107 #define size_overflow_p(SIZE) \
108 ((SIZE) == G_MAXSIZE)
109 /* Check against overflow. */
110 #define size_in_bounds_p(SIZE) \
111 ((SIZE) != G_MAXSIZE)
113 #endif /* _XSIZE_H */