memory.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * PuTTY's memory allocation wrappers.
  3. */
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #include "defs.h"
  8. #include "puttymem.h"
  9. #include "misc.h"
  10. void *safemalloc(size_t factor1, size_t factor2, size_t addend)
  11. {
  12. if (factor1 > SIZE_MAX / factor2)
  13. goto fail;
  14. { // WINSCP
  15. size_t product = factor1 * factor2;
  16. if (addend > SIZE_MAX)
  17. goto fail;
  18. if (product > SIZE_MAX - addend)
  19. goto fail;
  20. { // WINSCP
  21. size_t size = product + addend;
  22. if (size == 0)
  23. size = 1;
  24. { // WINSCP
  25. void *p;
  26. #ifdef MINEFIELD
  27. p = minefield_c_malloc(size);
  28. #else
  29. p = malloc(size);
  30. #endif
  31. if (!p)
  32. goto fail;
  33. return p;
  34. fail:
  35. out_of_memory();
  36. } // WINSCP
  37. } // WINSCP
  38. } // WINSCP
  39. }
  40. void *saferealloc(void *ptr, size_t n, size_t size)
  41. {
  42. void *p;
  43. if (n > INT_MAX / size) {
  44. p = NULL;
  45. } else {
  46. size *= n;
  47. if (!ptr) {
  48. #ifdef MINEFIELD
  49. p = minefield_c_malloc(size);
  50. #else
  51. p = malloc(size);
  52. #endif
  53. } else {
  54. #ifdef MINEFIELD
  55. p = minefield_c_realloc(ptr, size);
  56. #else
  57. p = realloc(ptr, size);
  58. #endif
  59. }
  60. }
  61. if (!p)
  62. out_of_memory();
  63. return p;
  64. }
  65. void safefree(void *ptr)
  66. {
  67. if (ptr) {
  68. #ifdef MINEFIELD
  69. minefield_c_free(ptr);
  70. #else
  71. free(ptr);
  72. #endif
  73. }
  74. }
  75. void *safegrowarray(void *ptr, size_t *allocated, size_t eltsize,
  76. size_t oldlen, size_t extralen, bool secret)
  77. {
  78. /* The largest value we can safely multiply by eltsize */
  79. pinitassert(eltsize > 0);
  80. size_t maxsize = (~(size_t)0) / eltsize;
  81. size_t oldsize = *allocated;
  82. /* Range-check the input values */
  83. assert(oldsize <= maxsize);
  84. assert(oldlen <= maxsize);
  85. assert(extralen <= maxsize - oldlen);
  86. /* If the size is already enough, don't bother doing anything! */
  87. if (oldsize > oldlen + extralen)
  88. return ptr;
  89. /* Find out how much we need to grow the array by. */
  90. { // WINSCP
  91. size_t increment = (oldlen + extralen) - oldsize;
  92. /* Invent a new size. We want to grow the array by at least
  93. * 'increment' elements; by at least a fixed number of bytes (to
  94. * get things started when sizes are small); and by some constant
  95. * factor of its old size (to avoid repeated calls to this
  96. * function taking quadratic time overall). */
  97. if (increment < 256 / eltsize)
  98. increment = 256 / eltsize;
  99. if (increment < oldsize / 16)
  100. increment = oldsize / 16;
  101. /* But we also can't grow beyond maxsize. */
  102. { // WINSCP
  103. size_t maxincr = maxsize - oldsize;
  104. if (increment > maxincr)
  105. increment = maxincr;
  106. { // WINSCP
  107. size_t newsize = oldsize + increment;
  108. void *toret;
  109. if (secret) {
  110. toret = safemalloc(newsize, eltsize, 0);
  111. memcpy(toret, ptr, oldsize * eltsize);
  112. smemclr(ptr, oldsize * eltsize);
  113. sfree(ptr);
  114. } else {
  115. toret = saferealloc(ptr, newsize, eltsize);
  116. }
  117. *allocated = newsize;
  118. return toret;
  119. } // WINSCP
  120. } // WINSCP
  121. } // WINSCP
  122. }