xsize.h 3.5 KB

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