Made error code consistent with the other text parsing functions.
[gnutls.git] / gl / xsize.h
bloba2942b95558b7299104795b9d319ae6e53a60f39
1 /* xsize.h -- Checked size_t computations.
3 Copyright (C) 2003, 2008-2012 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)
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 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 <http://www.gnu.org/licenses/>. */
18 #ifndef _XSIZE_H
19 #define _XSIZE_H
21 /* Get size_t. */
22 #include <stddef.h>
24 /* Get SIZE_MAX. */
25 #include <limits.h>
26 #if HAVE_STDINT_H
27 # include <stdint.h>
28 #endif
30 _GL_INLINE_HEADER_BEGIN
31 #ifndef XSIZE_INLINE
32 # define XSIZE_INLINE _GL_INLINE
33 #endif
35 /* The size of memory objects is often computed through expressions of
36 type size_t. Example:
37 void* p = malloc (header_size + n * element_size).
38 These computations can lead to overflow. When this happens, malloc()
39 returns a piece of memory that is way too small, and the program then
40 crashes while attempting to fill the memory.
41 To avoid this, the functions and macros in this file check for overflow.
42 The convention is that SIZE_MAX represents overflow.
43 malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
44 implementation that uses mmap --, it's recommended to use size_overflow_p()
45 or size_in_bounds_p() before invoking malloc().
46 The example thus becomes:
47 size_t size = xsum (header_size, xtimes (n, element_size));
48 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
51 /* Convert an arbitrary value >= 0 to type size_t. */
52 #define xcast_size_t(N) \
53 ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
55 /* Sum of two sizes, with overflow check. */
56 XSIZE_INLINE size_t
57 #if __GNUC__ >= 3
58 __attribute__ ((__pure__))
59 #endif
60 xsum (size_t size1, size_t size2)
62 size_t sum = size1 + size2;
63 return (sum >= size1 ? sum : SIZE_MAX);
66 /* Sum of three sizes, with overflow check. */
67 XSIZE_INLINE size_t
68 #if __GNUC__ >= 3
69 __attribute__ ((__pure__))
70 #endif
71 xsum3 (size_t size1, size_t size2, size_t size3)
73 return xsum (xsum (size1, size2), size3);
76 /* Sum of four sizes, with overflow check. */
77 XSIZE_INLINE size_t
78 #if __GNUC__ >= 3
79 __attribute__ ((__pure__))
80 #endif
81 xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
83 return xsum (xsum (xsum (size1, size2), size3), size4);
86 /* Maximum of two sizes, with overflow check. */
87 XSIZE_INLINE size_t
88 #if __GNUC__ >= 3
89 __attribute__ ((__pure__))
90 #endif
91 xmax (size_t size1, size_t size2)
93 /* No explicit check is needed here, because for any n:
94 max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
95 return (size1 >= size2 ? size1 : size2);
98 /* Multiplication of a count with an element size, with overflow check.
99 The count must be >= 0 and the element size must be > 0.
100 This is a macro, not a function, so that it works correctly even
101 when N is of a wider type and N > SIZE_MAX. */
102 #define xtimes(N, ELSIZE) \
103 ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
105 /* Check for overflow. */
106 #define size_overflow_p(SIZE) \
107 ((SIZE) == SIZE_MAX)
108 /* Check against overflow. */
109 #define size_in_bounds_p(SIZE) \
110 ((SIZE) != SIZE_MAX)
112 _GL_INLINE_HEADER_END
114 #endif /* _XSIZE_H */