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