Blame lib/xalloc-oversized.h

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