xsize.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef _GL_INLINE_HEADER_BEGIN
  23. #error "Please include config.h first."
  24. #endif
  25. _GL_INLINE_HEADER_BEGIN
  26. #ifndef XSIZE_INLINE
  27. # define XSIZE_INLINE _GL_INLINE
  28. #endif
  29. /* The size of memory objects is often computed through expressions of
  30. type size_t. Example:
  31. void* p = malloc (header_size + n * element_size).
  32. These computations can lead to overflow. When this happens, malloc()
  33. returns a piece of memory that is way too small, and the program then
  34. crashes while attempting to fill the memory.
  35. To avoid this, the functions and macros in this file check for overflow.
  36. The convention is that SIZE_MAX represents overflow.
  37. malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
  38. implementation that uses mmap --, it's recommended to use size_overflow_p()
  39. or size_in_bounds_p() before invoking malloc().
  40. The example thus becomes:
  41. size_t size = xsum (header_size, xtimes (n, element_size));
  42. void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
  43. */
  44. /* Convert an arbitrary value >= 0 to type size_t. */
  45. #define xcast_size_t(N) \
  46. ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
  47. /* Sum of two sizes, with overflow check. */
  48. XSIZE_INLINE size_t
  49. #if __GNUC__ >= 3
  50. __attribute__ ((__pure__))
  51. #endif
  52. xsum (size_t size1, size_t size2)
  53. {
  54. size_t sum = size1 + size2;
  55. return (sum >= size1 ? sum : SIZE_MAX);
  56. }
  57. /* Sum of three sizes, with overflow check. */
  58. XSIZE_INLINE size_t
  59. #if __GNUC__ >= 3
  60. __attribute__ ((__pure__))
  61. #endif
  62. xsum3 (size_t size1, size_t size2, size_t size3)
  63. {
  64. return xsum (xsum (size1, size2), size3);
  65. }
  66. /* Sum of four sizes, with overflow check. */
  67. XSIZE_INLINE size_t
  68. #if __GNUC__ >= 3
  69. __attribute__ ((__pure__))
  70. #endif
  71. xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
  72. {
  73. return xsum (xsum (xsum (size1, size2), size3), size4);
  74. }
  75. /* Maximum of two sizes, with overflow check. */
  76. XSIZE_INLINE size_t
  77. #if __GNUC__ >= 3
  78. __attribute__ ((__pure__))
  79. #endif
  80. xmax (size_t size1, size_t size2)
  81. {
  82. /* No explicit check is needed here, because for any n:
  83. max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
  84. return (size1 >= size2 ? size1 : size2);
  85. }
  86. /* Multiplication of a count with an element size, with overflow check.
  87. The count must be >= 0 and the element size must be > 0.
  88. This is a macro, not a function, so that it works correctly even
  89. when N is of a wider type and N > SIZE_MAX. */
  90. #define xtimes(N, ELSIZE) \
  91. ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
  92. /* Check for overflow. */
  93. #define size_overflow_p(SIZE) \
  94. ((SIZE) == SIZE_MAX)
  95. /* Check against overflow. */
  96. #define size_in_bounds_p(SIZE) \
  97. ((SIZE) != SIZE_MAX)
  98. _GL_INLINE_HEADER_END
  99. #endif /* _XSIZE_H */