LCOV - code coverage report
Current view: top level - lib - xsize.h (source / functions) Hit Total Coverage
Test: coreutils.info Lines: 7 7 100.0 %
Date: 2018-01-30 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* -*- buffer-read-only: t -*- vi: set ro: */
       2             : /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
       3             : #line 1
       4             : /* xsize.h -- Checked size_t computations.
       5             : 
       6             :    Copyright (C) 2003, 2008 Free Software Foundation, Inc.
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3, or (at your option)
      11             :    any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program; if not, write to the Free Software Foundation,
      20             :    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
      21             : 
      22             : #ifndef _XSIZE_H
      23             : #define _XSIZE_H
      24             : 
      25             : /* Get size_t.  */
      26             : #include <stddef.h>
      27             : 
      28             : /* Get SIZE_MAX.  */
      29             : #include <limits.h>
      30             : #if HAVE_STDINT_H
      31             : # include <stdint.h>
      32             : #endif
      33             : 
      34             : /* The size of memory objects is often computed through expressions of
      35             :    type size_t. Example:
      36             :       void* p = malloc (header_size + n * element_size).
      37             :    These computations can lead to overflow.  When this happens, malloc()
      38             :    returns a piece of memory that is way too small, and the program then
      39             :    crashes while attempting to fill the memory.
      40             :    To avoid this, the functions and macros in this file check for overflow.
      41             :    The convention is that SIZE_MAX represents overflow.
      42             :    malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
      43             :    implementation that uses mmap --, it's recommended to use size_overflow_p()
      44             :    or size_in_bounds_p() before invoking malloc().
      45             :    The example thus becomes:
      46             :       size_t size = xsum (header_size, xtimes (n, element_size));
      47             :       void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
      48             : */
      49             : 
      50             : /* Convert an arbitrary value >= 0 to type size_t.  */
      51             : #define xcast_size_t(N) \
      52             :   ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
      53             : 
      54             : /* Sum of two sizes, with overflow check.  */
      55             : static inline size_t
      56        5797 : #if __GNUC__ >= 3
      57             : __attribute__ ((__pure__))
      58        5797 : #endif
      59        5797 : xsum (size_t size1, size_t size2)
      60             : {
      61             :   size_t sum = size1 + size2;
      62             :   return (sum >= size1 ? sum : SIZE_MAX);
      63             : }
      64             : 
      65             : /* Sum of three sizes, with overflow check.  */
      66             : static inline size_t
      67             : #if __GNUC__ >= 3
      68             : __attribute__ ((__pure__))
      69             : #endif
      70             : xsum3 (size_t size1, size_t size2, size_t size3)
      71             : {
      72             :   return xsum (xsum (size1, size2), size3);
      73             : }
      74             : 
      75             : /* Sum of four sizes, with overflow check.  */
      76             : static inline size_t
      77         731 : #if __GNUC__ >= 3
      78             : __attribute__ ((__pure__))
      79         731 : #endif
      80             : xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
      81             : {
      82             :   return xsum (xsum (xsum (size1, size2), size3), size4);
      83             : }
      84             : 
      85             : /* Maximum of two sizes, with overflow check.  */
      86             : static inline size_t
      87          52 : #if __GNUC__ >= 3
      88             : __attribute__ ((__pure__))
      89             : #endif
      90             : xmax (size_t size1, size_t size2)
      91          52 : {
      92             :   /* No explicit check is needed here, because for any n:
      93             :      max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX.  */
      94             :   return (size1 >= size2 ? size1 : size2);
      95             : }
      96             : 
      97             : /* Multiplication of a count with an element size, with overflow check.
      98             :    The count must be >= 0 and the element size must be > 0.
      99             :    This is a macro, not an inline function, so that it works correctly even
     100             :    when N is of a wider type and N > SIZE_MAX.  */
     101             : #define xtimes(N, ELSIZE) \
     102             :   ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
     103             : 
     104             : /* Check for overflow.  */
     105             : #define size_overflow_p(SIZE) \
     106             :   ((SIZE) == SIZE_MAX)
     107             : /* Check against overflow.  */
     108             : #define size_in_bounds_p(SIZE) \
     109             :   ((SIZE) != SIZE_MAX)
     110             : 
     111             : #endif /* _XSIZE_H */

Generated by: LCOV version 1.10