Blame src/gl/xalloc-oversized.h

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