xsize.h 3.5 KB

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