annotate lib/xsize.h @ 273:88bbffa404ea 2.5 2.5

Updated version for release
author lost
date Sun, 16 Aug 2009 17:18:11 +0000
parents d5392bb5da3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
272
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1 /* xsize.h -- Checked size_t computations.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
2
d5392bb5da3c Added generated files
lost
parents:
diff changeset
3 Copyright (C) 2003, 2008 Free Software Foundation, Inc.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
4
d5392bb5da3c Added generated files
lost
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
d5392bb5da3c Added generated files
lost
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
d5392bb5da3c Added generated files
lost
parents:
diff changeset
7 the Free Software Foundation; either version 3, or (at your option)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
8 any later version.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
9
d5392bb5da3c Added generated files
lost
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
13 GNU General Public License for more details.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
14
d5392bb5da3c Added generated files
lost
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
d5392bb5da3c Added generated files
lost
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
18
d5392bb5da3c Added generated files
lost
parents:
diff changeset
19 #ifndef _XSIZE_H
d5392bb5da3c Added generated files
lost
parents:
diff changeset
20 #define _XSIZE_H
d5392bb5da3c Added generated files
lost
parents:
diff changeset
21
d5392bb5da3c Added generated files
lost
parents:
diff changeset
22 /* Get size_t. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
23 #include <stddef.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
24
d5392bb5da3c Added generated files
lost
parents:
diff changeset
25 /* Get SIZE_MAX. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
26 #include <limits.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
27 #if HAVE_STDINT_H
d5392bb5da3c Added generated files
lost
parents:
diff changeset
28 # include <stdint.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
29 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
30
d5392bb5da3c Added generated files
lost
parents:
diff changeset
31 /* The size of memory objects is often computed through expressions of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
32 type size_t. Example:
d5392bb5da3c Added generated files
lost
parents:
diff changeset
33 void* p = malloc (header_size + n * element_size).
d5392bb5da3c Added generated files
lost
parents:
diff changeset
34 These computations can lead to overflow. When this happens, malloc()
d5392bb5da3c Added generated files
lost
parents:
diff changeset
35 returns a piece of memory that is way too small, and the program then
d5392bb5da3c Added generated files
lost
parents:
diff changeset
36 crashes while attempting to fill the memory.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
37 To avoid this, the functions and macros in this file check for overflow.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
38 The convention is that SIZE_MAX represents overflow.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
39 malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
d5392bb5da3c Added generated files
lost
parents:
diff changeset
40 implementation that uses mmap --, it's recommended to use size_overflow_p()
d5392bb5da3c Added generated files
lost
parents:
diff changeset
41 or size_in_bounds_p() before invoking malloc().
d5392bb5da3c Added generated files
lost
parents:
diff changeset
42 The example thus becomes:
d5392bb5da3c Added generated files
lost
parents:
diff changeset
43 size_t size = xsum (header_size, xtimes (n, element_size));
d5392bb5da3c Added generated files
lost
parents:
diff changeset
44 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
45 */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
46
d5392bb5da3c Added generated files
lost
parents:
diff changeset
47 /* Convert an arbitrary value >= 0 to type size_t. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
48 #define xcast_size_t(N) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
49 ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
50
d5392bb5da3c Added generated files
lost
parents:
diff changeset
51 /* Sum of two sizes, with overflow check. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
52 static inline size_t
d5392bb5da3c Added generated files
lost
parents:
diff changeset
53 #if __GNUC__ >= 3
d5392bb5da3c Added generated files
lost
parents:
diff changeset
54 __attribute__ ((__pure__))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
55 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
56 xsum (size_t size1, size_t size2)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
57 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
58 size_t sum = size1 + size2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
59 return (sum >= size1 ? sum : SIZE_MAX);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
60 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
61
d5392bb5da3c Added generated files
lost
parents:
diff changeset
62 /* Sum of three sizes, with overflow check. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
63 static inline size_t
d5392bb5da3c Added generated files
lost
parents:
diff changeset
64 #if __GNUC__ >= 3
d5392bb5da3c Added generated files
lost
parents:
diff changeset
65 __attribute__ ((__pure__))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
66 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
67 xsum3 (size_t size1, size_t size2, size_t size3)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
68 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
69 return xsum (xsum (size1, size2), size3);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
70 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
71
d5392bb5da3c Added generated files
lost
parents:
diff changeset
72 /* Sum of four sizes, with overflow check. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
73 static inline size_t
d5392bb5da3c Added generated files
lost
parents:
diff changeset
74 #if __GNUC__ >= 3
d5392bb5da3c Added generated files
lost
parents:
diff changeset
75 __attribute__ ((__pure__))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
76 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
77 xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
78 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
79 return xsum (xsum (xsum (size1, size2), size3), size4);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
80 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
81
d5392bb5da3c Added generated files
lost
parents:
diff changeset
82 /* Maximum of two sizes, with overflow check. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
83 static inline size_t
d5392bb5da3c Added generated files
lost
parents:
diff changeset
84 #if __GNUC__ >= 3
d5392bb5da3c Added generated files
lost
parents:
diff changeset
85 __attribute__ ((__pure__))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
86 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
87 xmax (size_t size1, size_t size2)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
88 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
89 /* No explicit check is needed here, because for any n:
d5392bb5da3c Added generated files
lost
parents:
diff changeset
90 max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
91 return (size1 >= size2 ? size1 : size2);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
92 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
93
d5392bb5da3c Added generated files
lost
parents:
diff changeset
94 /* Multiplication of a count with an element size, with overflow check.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
95 The count must be >= 0 and the element size must be > 0.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
96 This is a macro, not an inline function, so that it works correctly even
d5392bb5da3c Added generated files
lost
parents:
diff changeset
97 when N is of a wider type and N > SIZE_MAX. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
98 #define xtimes(N, ELSIZE) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
99 ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
100
d5392bb5da3c Added generated files
lost
parents:
diff changeset
101 /* Check for overflow. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
102 #define size_overflow_p(SIZE) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
103 ((SIZE) == SIZE_MAX)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
104 /* Check against overflow. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
105 #define size_in_bounds_p(SIZE) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
106 ((SIZE) != SIZE_MAX)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
107
d5392bb5da3c Added generated files
lost
parents:
diff changeset
108 #endif /* _XSIZE_H */