You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xsize.h 3.5KB

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