summaryrefslogtreecommitdiffstats
path: root/gl/xsize.h
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2007-01-24 22:47:25 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2007-01-24 22:47:25 (GMT)
commitfe856aa957978504137c1d425815d4ed8a22be40 (patch)
treea5bb46ce0e64b2056f75700eadbf27aba7c39418 /gl/xsize.h
parent210f39bc84cfbb21cd72dc054e43f13815ee0616 (diff)
downloadmonitoring-plugins-fe856aa957978504137c1d425815d4ed8a22be40.tar.gz
Sync with gnulib - lots of extraneous code removed in preference to GNU code
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1580 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'gl/xsize.h')
-rw-r--r--gl/xsize.h108
1 files changed, 108 insertions, 0 deletions
diff --git a/gl/xsize.h b/gl/xsize.h
new file mode 100644
index 0000000..341fb16
--- /dev/null
+++ b/gl/xsize.h
@@ -0,0 +1,108 @@
1/* xsize.h -- Checked size_t computations.
2
3 Copyright (C) 2003 Free Software Foundation, Inc.
4
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 2, or (at your option)
8 any later version.
9
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.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#ifndef _XSIZE_H
20#define _XSIZE_H
21
22/* Get size_t. */
23#include <stddef.h>
24
25/* Get SIZE_MAX. */
26#include <limits.h>
27#if HAVE_STDINT_H
28# include <stdint.h>
29#endif
30
31/* The size of memory objects is often computed through expressions of
32 type size_t. Example:
33 void* p = malloc (header_size + n * element_size).
34 These computations can lead to overflow. When this happens, malloc()
35 returns a piece of memory that is way too small, and the program then
36 crashes while attempting to fill the memory.
37 To avoid this, the functions and macros in this file check for overflow.
38 The convention is that SIZE_MAX represents overflow.
39 malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
40 implementation that uses mmap --, it's recommended to use size_overflow_p()
41 or size_in_bounds_p() before invoking malloc().
42 The example thus becomes:
43 size_t size = xsum (header_size, xtimes (n, element_size));
44 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
45*/
46
47/* Convert an arbitrary value >= 0 to type size_t. */
48#define xcast_size_t(N) \
49 ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
50
51/* Sum of two sizes, with overflow check. */
52static inline size_t
53#if __GNUC__ >= 3
54__attribute__ ((__pure__))
55#endif
56xsum (size_t size1, size_t size2)
57{
58 size_t sum = size1 + size2;
59 return (sum >= size1 ? sum : SIZE_MAX);
60}
61
62/* Sum of three sizes, with overflow check. */
63static inline size_t
64#if __GNUC__ >= 3
65__attribute__ ((__pure__))
66#endif
67xsum3 (size_t size1, size_t size2, size_t size3)
68{
69 return xsum (xsum (size1, size2), size3);
70}
71
72/* Sum of four sizes, with overflow check. */
73static inline size_t
74#if __GNUC__ >= 3
75__attribute__ ((__pure__))
76#endif
77xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
78{
79 return xsum (xsum (xsum (size1, size2), size3), size4);
80}
81
82/* Maximum of two sizes, with overflow check. */
83static inline size_t
84#if __GNUC__ >= 3
85__attribute__ ((__pure__))
86#endif
87xmax (size_t size1, size_t size2)
88{
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);
92}
93
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 an inline function, so that it works correctly even
97 when N is of a wider tupe and N > SIZE_MAX. */
98#define xtimes(N, ELSIZE) \
99 ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
100
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)
107
108#endif /* _XSIZE_H */