Blame gl/xalloc-oversized.h

Packit a4aae4
/* xalloc-oversized.h -- memory allocation size checking
Packit a4aae4
Packit a4aae4
   Copyright (C) 1990-2000, 2003-2004, 2006-2017 Free Software Foundation, Inc.
Packit a4aae4
Packit a4aae4
   This program is free software: you can redistribute it and/or modify
Packit a4aae4
   it under the terms of the GNU Lesser General Public License as published by
Packit a4aae4
   the Free Software Foundation; either version 3 of the License, or
Packit a4aae4
   (at your option) any later version.
Packit a4aae4
Packit a4aae4
   This program is distributed in the hope that it will be useful,
Packit a4aae4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4aae4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit a4aae4
   GNU Lesser General Public License for more details.
Packit a4aae4
Packit a4aae4
   You should have received a copy of the GNU Lesser General Public License
Packit a4aae4
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit a4aae4
Packit a4aae4
#ifndef XALLOC_OVERSIZED_H_
Packit a4aae4
#define XALLOC_OVERSIZED_H_
Packit a4aae4
Packit a4aae4
#include <stddef.h>
Packit a4aae4
#include <stdint.h>
Packit a4aae4
Packit a4aae4
/* True if N * S would overflow in a size_t calculation,
Packit a4aae4
   or would generate a value larger than PTRDIFF_MAX.
Packit a4aae4
   This expands to a constant expression if N and S are both constants.
Packit a4aae4
   By gnulib convention, SIZE_MAX represents overflow in size
Packit a4aae4
   calculations, so the conservative size_t-based dividend to use here
Packit a4aae4
   is SIZE_MAX - 1.  */
Packit a4aae4
#define __xalloc_oversized(n, s) \
Packit a4aae4
  ((size_t) (PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX - 1) / (s) < (n))
Packit a4aae4
Packit a4aae4
#if PTRDIFF_MAX < SIZE_MAX
Packit a4aae4
typedef ptrdiff_t __xalloc_count_type;
Packit a4aae4
#else
Packit a4aae4
typedef size_t __xalloc_count_type;
Packit a4aae4
#endif
Packit a4aae4
Packit a4aae4
/* Return 1 if an array of N objects, each of size S, cannot exist
Packit a4aae4
   reliably due to size or ptrdiff_t arithmetic overflow.  S must be
Packit a4aae4
   positive and N must be nonnegative.  This is a macro, not a
Packit a4aae4
   function, so that it works correctly even when SIZE_MAX < N.  */
Packit a4aae4
Packit a4aae4
#if 7 <= __GNUC__
Packit a4aae4
# define xalloc_oversized(n, s) \
Packit a4aae4
   __builtin_mul_overflow_p (n, s, (__xalloc_count_type) 1)
Packit a4aae4
#elif 5 <= __GNUC__ && !defined __ICC && !__STRICT_ANSI__
Packit a4aae4
# define xalloc_oversized(n, s) \
Packit a4aae4
   (__builtin_constant_p (n) && __builtin_constant_p (s) \
Packit a4aae4
    ? __xalloc_oversized (n, s) \
Packit a4aae4
    : ({ __xalloc_count_type __xalloc_count; \
Packit a4aae4
         __builtin_mul_overflow (n, s, &__xalloc_count); }))
Packit a4aae4
Packit a4aae4
/* Other compilers use integer division; this may be slower but is
Packit a4aae4
   more portable.  */
Packit a4aae4
#else
Packit a4aae4
# define xalloc_oversized(n, s) __xalloc_oversized (n, s)
Packit a4aae4
#endif
Packit a4aae4
Packit a4aae4
#endif /* !XALLOC_OVERSIZED_H_ */