Blame lib/xalloc-oversized.h

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