# HG changeset patch # User Eric Blake # Date 1303941270 21600 # Node ID b3b2f9e830e98649999d1f83026fde61e460c232 # Parent 1e0bf20577a1c61bfb9280193ed911f470e13d57 xalloc-oversized: new module Due to inline functions, mere inclusion of xalloc.h can result in a link dependency on xalloc_die() on some platforms. However, there are several modules that want to use just xalloc_oversized in order to short-circuit the potential to call xalloc_die. Splitting the macro into a new header and module makes this easy. * modules/xalloc-oversized: New module. * modules/xalloc (Depends-on): Add it. * lib/xalloc.h (xalloc_oversized): Move... * lib/xalloc-oversized.h: ...into new file. Signed-off-by: Eric Blake diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2011-04-28 Eric Blake + xalloc-oversized: new module + * modules/xalloc-oversized: New module. + * modules/xalloc (Depends-on): Add it. + * lib/xalloc.h (xalloc_oversized): Move... + * lib/xalloc-oversized.h: ...into new file. + utimecmp: drop dependency on xmalloc * lib/utimecmp.c (utimecmp): Work even if hash table cache fails due to memory pressure. diff --git a/lib/xalloc-oversized.h b/lib/xalloc-oversized.h new file mode 100644 --- /dev/null +++ b/lib/xalloc-oversized.h @@ -0,0 +1,38 @@ +/* xalloc-oversized.h -- memory allocation size checking + + Copyright (C) 1990-2000, 2003-2004, 2006-2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef XALLOC_OVERSIZED_H_ +# define XALLOC_OVERSIZED_H_ + +# include + +/* Return 1 if an array of N objects, each of size S, cannot exist due + to size arithmetic overflow. S must be positive and N must be + nonnegative. This is a macro, not an inline function, so that it + works correctly even when SIZE_MAX < N. + + By gnulib convention, SIZE_MAX represents overflow in size + calculations, so the conservative dividend to use here is + SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. + However, malloc (SIZE_MAX) fails on all known hosts where + sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for + exactly-SIZE_MAX allocations on such hosts; this avoids a test and + branch when S is known to be 1. */ +# define xalloc_oversized(n, s) \ + ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) + +#endif /* !XALLOC_OVERSIZED_H_ */ diff --git a/lib/xalloc.h b/lib/xalloc.h --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -20,6 +20,7 @@ # include +# include "xalloc-oversized.h" # ifdef __cplusplus extern "C" { @@ -65,22 +66,6 @@ char *xstrdup (char const *str) _GL_ATTRIBUTE_MALLOC; -/* Return 1 if an array of N objects, each of size S, cannot exist due - to size arithmetic overflow. S must be positive and N must be - nonnegative. This is a macro, not an inline function, so that it - works correctly even when SIZE_MAX < N. - - By gnulib convention, SIZE_MAX represents overflow in size - calculations, so the conservative dividend to use here is - SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. - However, malloc (SIZE_MAX) fails on all known hosts where - sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for - exactly-SIZE_MAX allocations on such hosts; this avoids a test and - branch when S is known to be 1. */ -# define xalloc_oversized(n, s) \ - ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) - - /* In the following macros, T must be an elementary or structure/union or typedef'ed type, or a pointer to such a type. To apply one of the following macros to a function pointer or array type, you need to typedef diff --git a/modules/xalloc b/modules/xalloc --- a/modules/xalloc +++ b/modules/xalloc @@ -9,6 +9,7 @@ Depends-on: inline xalloc-die +xalloc-oversized configure.ac: gl_XALLOC diff --git a/modules/xalloc-oversized b/modules/xalloc-oversized new file mode 100644 --- /dev/null +++ b/modules/xalloc-oversized @@ -0,0 +1,20 @@ +Description: +Check for memory allocation overflow. Also see xalloc. + +Files: +lib/xalloc-oversized.h + +Depends-on: + +configure.ac: + +Makefile.am: + +Include: +"xalloc-oversized.h" + +License: +GPL + +Maintainer: +all