Navit SVN

/work/compile/navit/src/navit/intl_cmake/xsize.h

00001 /* xsize.h -- Checked size_t computations.
00002 
00003    Copyright (C) 2003 Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify it
00006    under the terms of the GNU Library General Public License as published
00007    by the Free Software Foundation; either version 2, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this program; if not, write to the Free Software
00017    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00018    USA.  */
00019 
00020 #ifndef _XSIZE_H
00021 #define _XSIZE_H
00022 
00023 /* Get size_t.  */
00024 #include <stddef.h>
00025 
00026 /* Get SIZE_MAX.  */
00027 #include <limits.h>
00028 #if HAVE_STDINT_H
00029 # include <stdint.h>
00030 #endif
00031 
00032 /* The size of memory objects is often computed through expressions of
00033    type size_t. Example:
00034       void* p = malloc (header_size + n * element_size).
00035    These computations can lead to overflow.  When this happens, malloc()
00036    returns a piece of memory that is way too small, and the program then
00037    crashes while attempting to fill the memory.
00038    To avoid this, the functions and macros in this file check for overflow.
00039    The convention is that SIZE_MAX represents overflow.
00040    malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
00041    implementation that uses mmap --, it's recommended to use size_overflow_p()
00042    or size_in_bounds_p() before invoking malloc().
00043    The example thus becomes:
00044       size_t size = xsum (header_size, xtimes (n, element_size));
00045       void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
00046 */
00047 
00048 /* Convert an arbitrary value >= 0 to type size_t.  */
00049 #define xcast_size_t(N) \
00050   ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
00051 
00052 /* Sum of two sizes, with overflow check.  */
00053 static size_t
00054 #if __GNUC__ >= 3
00055 __attribute__ ((__pure__))
00056 #endif
00057 xsum (size_t size1, size_t size2)
00058 {
00059   size_t sum = size1 + size2;
00060   return (sum >= size1 ? sum : SIZE_MAX);
00061 }
00062 
00063 /* Sum of three sizes, with overflow check.  */
00064 static size_t
00065 #if __GNUC__ >= 3
00066 __attribute__ ((__pure__))
00067 #endif
00068 xsum3 (size_t size1, size_t size2, size_t size3)
00069 {
00070   return xsum (xsum (size1, size2), size3);
00071 }
00072 
00073 /* Sum of four sizes, with overflow check.  */
00074 static size_t
00075 #if __GNUC__ >= 3
00076 __attribute__ ((__pure__))
00077 #endif
00078 xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
00079 {
00080   return xsum (xsum (xsum (size1, size2), size3), size4);
00081 }
00082 
00083 /* Maximum of two sizes, with overflow check.  */
00084 static size_t
00085 #if __GNUC__ >= 3
00086 __attribute__ ((__pure__))
00087 #endif
00088 xmax (size_t size1, size_t size2)
00089 {
00090   /* No explicit check is needed here, because for any n:
00091      max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX.  */
00092   return (size1 >= size2 ? size1 : size2);
00093 }
00094 
00095 /* Multiplication of a count with an element size, with overflow check.
00096    The count must be >= 0 and the element size must be > 0.
00097    This is a macro, not an inline function, so that it works correctly even
00098    when N is of a wider tupe and N > SIZE_MAX.  */
00099 #define xtimes(N, ELSIZE) \
00100   ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
00101 
00102 /* Check for overflow.  */
00103 #define size_overflow_p(SIZE) \
00104   ((SIZE) == SIZE_MAX)
00105 /* Check against overflow.  */
00106 #define size_in_bounds_p(SIZE) \
00107   ((SIZE) != SIZE_MAX)
00108 
00109 #endif /* _XSIZE_H */