Blame lib/xsize.h

Packit Service fdd496
/* xsize.h -- Checked size_t computations.
Packit Service fdd496
Packit Service fdd496
   Copyright (C) 2003, 2008-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software; you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3, or (at your option)
Packit Service fdd496
   any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
#ifndef _XSIZE_H
Packit Service fdd496
#define _XSIZE_H
Packit Service fdd496
Packit Service fdd496
/* Get size_t.  */
Packit Service fdd496
#include <stddef.h>
Packit Service fdd496
Packit Service fdd496
/* Get SIZE_MAX.  */
Packit Service fdd496
#include <limits.h>
Packit Service fdd496
#if HAVE_STDINT_H
Packit Service fdd496
# include <stdint.h>
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#ifndef _GL_INLINE_HEADER_BEGIN
Packit Service fdd496
 #error "Please include config.h first."
Packit Service fdd496
#endif
Packit Service fdd496
_GL_INLINE_HEADER_BEGIN
Packit Service fdd496
#ifndef XSIZE_INLINE
Packit Service fdd496
# define XSIZE_INLINE _GL_INLINE
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* The size of memory objects is often computed through expressions of
Packit Service fdd496
   type size_t. Example:
Packit Service fdd496
      void* p = malloc (header_size + n * element_size).
Packit Service fdd496
   These computations can lead to overflow.  When this happens, malloc()
Packit Service fdd496
   returns a piece of memory that is way too small, and the program then
Packit Service fdd496
   crashes while attempting to fill the memory.
Packit Service fdd496
   To avoid this, the functions and macros in this file check for overflow.
Packit Service fdd496
   The convention is that SIZE_MAX represents overflow.
Packit Service fdd496
   malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
Packit Service fdd496
   implementation that uses mmap --, it's recommended to use size_overflow_p()
Packit Service fdd496
   or size_in_bounds_p() before invoking malloc().
Packit Service fdd496
   The example thus becomes:
Packit Service fdd496
      size_t size = xsum (header_size, xtimes (n, element_size));
Packit Service fdd496
      void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
Packit Service fdd496
*/
Packit Service fdd496
Packit Service fdd496
/* Convert an arbitrary value >= 0 to type size_t.  */
Packit Service fdd496
#define xcast_size_t(N) \
Packit Service fdd496
  ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
Packit Service fdd496
Packit Service fdd496
/* Sum of two sizes, with overflow check.  */
Packit Service fdd496
XSIZE_INLINE size_t
Packit Service fdd496
#if __GNUC__ >= 3
Packit Service fdd496
__attribute__ ((__pure__))
Packit Service fdd496
#endif
Packit Service fdd496
xsum (size_t size1, size_t size2)
Packit Service fdd496
{
Packit Service fdd496
  size_t sum = size1 + size2;
Packit Service fdd496
  return (sum >= size1 ? sum : SIZE_MAX);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Sum of three sizes, with overflow check.  */
Packit Service fdd496
XSIZE_INLINE size_t
Packit Service fdd496
#if __GNUC__ >= 3
Packit Service fdd496
__attribute__ ((__pure__))
Packit Service fdd496
#endif
Packit Service fdd496
xsum3 (size_t size1, size_t size2, size_t size3)
Packit Service fdd496
{
Packit Service fdd496
  return xsum (xsum (size1, size2), size3);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Sum of four sizes, with overflow check.  */
Packit Service fdd496
XSIZE_INLINE size_t
Packit Service fdd496
#if __GNUC__ >= 3
Packit Service fdd496
__attribute__ ((__pure__))
Packit Service fdd496
#endif
Packit Service fdd496
xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
Packit Service fdd496
{
Packit Service fdd496
  return xsum (xsum (xsum (size1, size2), size3), size4);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Maximum of two sizes, with overflow check.  */
Packit Service fdd496
XSIZE_INLINE size_t
Packit Service fdd496
#if __GNUC__ >= 3
Packit Service fdd496
__attribute__ ((__pure__))
Packit Service fdd496
#endif
Packit Service fdd496
xmax (size_t size1, size_t size2)
Packit Service fdd496
{
Packit Service fdd496
  /* No explicit check is needed here, because for any n:
Packit Service fdd496
     max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX.  */
Packit Service fdd496
  return (size1 >= size2 ? size1 : size2);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Multiplication of a count with an element size, with overflow check.
Packit Service fdd496
   The count must be >= 0 and the element size must be > 0.
Packit Service fdd496
   This is a macro, not a function, so that it works correctly even
Packit Service fdd496
   when N is of a wider type and N > SIZE_MAX.  */
Packit Service fdd496
#define xtimes(N, ELSIZE) \
Packit Service fdd496
  ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
Packit Service fdd496
Packit Service fdd496
/* Check for overflow.  */
Packit Service fdd496
#define size_overflow_p(SIZE) \
Packit Service fdd496
  ((SIZE) == SIZE_MAX)
Packit Service fdd496
/* Check against overflow.  */
Packit Service fdd496
#define size_in_bounds_p(SIZE) \
Packit Service fdd496
  ((SIZE) != SIZE_MAX)
Packit Service fdd496
Packit Service fdd496
_GL_INLINE_HEADER_END
Packit Service fdd496
Packit Service fdd496
#endif /* _XSIZE_H */