xsize.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* xsize.h -- Checked size_t computations.
  2. Copyright (C) 2003, 2008-2012 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 Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #ifndef _XSIZE_H
  15. #define _XSIZE_H
  16. /* Get size_t. */
  17. #include <stddef.h>
  18. /* Get SIZE_MAX. */
  19. #include <limits.h>
  20. #if HAVE_STDINT_H
  21. # include <stdint.h>
  22. #endif
  23. /* The size of memory objects is often computed through expressions of
  24. type size_t. Example:
  25. void* p = malloc (header_size + n * element_size).
  26. These computations can lead to overflow. When this happens, malloc()
  27. returns a piece of memory that is way too small, and the program then
  28. crashes while attempting to fill the memory.
  29. To avoid this, the functions and macros in this file check for overflow.
  30. The convention is that SIZE_MAX represents overflow.
  31. malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
  32. implementation that uses mmap --, it's recommended to use size_overflow_p()
  33. or size_in_bounds_p() before invoking malloc().
  34. The example thus becomes:
  35. size_t size = xsum (header_size, xtimes (n, element_size));
  36. void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
  37. */
  38. /* Convert an arbitrary value >= 0 to type size_t. */
  39. #define xcast_size_t(N) \
  40. ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
  41. /* Sum of two sizes, with overflow check. */
  42. static inline size_t
  43. #if __GNUC__ >= 3
  44. __attribute__ ((__pure__))
  45. #endif
  46. xsum (size_t size1, size_t size2)
  47. {
  48. size_t sum = size1 + size2;
  49. return (sum >= size1 ? sum : SIZE_MAX);
  50. }
  51. /* Sum of three sizes, with overflow check. */
  52. static inline size_t
  53. #if __GNUC__ >= 3
  54. __attribute__ ((__pure__))
  55. #endif
  56. xsum3 (size_t size1, size_t size2, size_t size3)
  57. {
  58. return xsum (xsum (size1, size2), size3);
  59. }
  60. /* Sum of four sizes, with overflow check. */
  61. static inline size_t
  62. #if __GNUC__ >= 3
  63. __attribute__ ((__pure__))
  64. #endif
  65. xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
  66. {
  67. return xsum (xsum (xsum (size1, size2), size3), size4);
  68. }
  69. /* Maximum of two sizes, with overflow check. */
  70. static inline size_t
  71. #if __GNUC__ >= 3
  72. __attribute__ ((__pure__))
  73. #endif
  74. xmax (size_t size1, size_t size2)
  75. {
  76. /* No explicit check is needed here, because for any n:
  77. max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
  78. return (size1 >= size2 ? size1 : size2);
  79. }
  80. /* Multiplication of a count with an element size, with overflow check.
  81. The count must be >= 0 and the element size must be > 0.
  82. This is a macro, not an inline function, so that it works correctly even
  83. when N is of a wider type and N > SIZE_MAX. */
  84. #define xtimes(N, ELSIZE) \
  85. ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
  86. /* Check for overflow. */
  87. #define size_overflow_p(SIZE) \
  88. ((SIZE) == SIZE_MAX)
  89. /* Check against overflow. */
  90. #define size_in_bounds_p(SIZE) \
  91. ((SIZE) != SIZE_MAX)
  92. #endif /* _XSIZE_H */