Import upstream version 1.28
[debian/tar] / gnu / xalloc-oversized.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* xalloc-oversized.h -- memory allocation size checking
4
5    Copyright (C) 1990-2000, 2003-2004, 2006-2014 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifndef XALLOC_OVERSIZED_H_
21 # define XALLOC_OVERSIZED_H_
22
23 # include <stddef.h>
24
25 /* Return 1 if an array of N objects, each of size S, cannot exist due
26    to size arithmetic overflow.  S must be positive and N must be
27    nonnegative.  This is a macro, not a function, so that it
28    works correctly even when SIZE_MAX < N.
29
30    By gnulib convention, SIZE_MAX represents overflow in size
31    calculations, so the conservative dividend to use here is
32    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
33    However, malloc (SIZE_MAX) fails on all known hosts where
34    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
35    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
36    branch when S is known to be 1.  */
37 # define xalloc_oversized(n, s) \
38     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
39
40 #endif /* !XALLOC_OVERSIZED_H_ */