xmalloc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* $OpenBSD: xmalloc.c,v 1.36 2019/11/12 22:32:48 djm Exp $ */
  2. /*
  3. * Author: Tatu Ylonen <ylo@cs.hut.fi>
  4. * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  5. * All rights reserved
  6. * Versions of malloc and friends that check their results, and never return
  7. * failure (they call fatal if they encounter an error).
  8. *
  9. * As far as I am concerned, the code I have written for this software
  10. * can be used freely for any purpose. Any derived versions of this
  11. * software must be clearly marked as such, and if the derived work is
  12. * incompatible with the protocol description in the RFC file, it must be
  13. * called by a name other than "ssh" or "Secure Shell".
  14. */
  15. #include "includes.h"
  16. #include <stdarg.h>
  17. #ifdef HAVE_STDINT_H
  18. # include <stdint.h>
  19. #endif
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "xmalloc.h"
  24. #include "log.h"
  25. #if defined(__OpenBSD__)
  26. char *malloc_options = "S";
  27. #endif /* __OpenBSD__ */
  28. void *
  29. xmalloc(size_t size)
  30. {
  31. void *ptr;
  32. if (size == 0)
  33. fatal("xmalloc: zero size");
  34. ptr = malloc(size);
  35. if (ptr == NULL)
  36. fatal("xmalloc: out of memory (allocating %zu bytes)", size);
  37. return ptr;
  38. }
  39. void *
  40. xcalloc(size_t nmemb, size_t size)
  41. {
  42. void *ptr;
  43. if (size == 0 || nmemb == 0)
  44. fatal("xcalloc: zero size");
  45. if (SIZE_MAX / nmemb < size)
  46. fatal("xcalloc: nmemb * size > SIZE_MAX");
  47. ptr = calloc(nmemb, size);
  48. if (ptr == NULL)
  49. fatal("xcalloc: out of memory (allocating %zu bytes)",
  50. size * nmemb);
  51. return ptr;
  52. }
  53. void *
  54. xreallocarray(void *ptr, size_t nmemb, size_t size)
  55. {
  56. void *new_ptr;
  57. new_ptr = reallocarray(ptr, nmemb, size);
  58. if (new_ptr == NULL)
  59. fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
  60. nmemb, size);
  61. return new_ptr;
  62. }
  63. void *
  64. xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size)
  65. {
  66. void *new_ptr;
  67. new_ptr = recallocarray(ptr, onmemb, nmemb, size);
  68. if (new_ptr == NULL)
  69. fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
  70. nmemb, size);
  71. return new_ptr;
  72. }
  73. char *
  74. xstrdup(const char *str)
  75. {
  76. size_t len;
  77. char *cp;
  78. len = strlen(str) + 1;
  79. cp = xmalloc(len);
  80. strlcpy(cp, str, len);
  81. return cp;
  82. }
  83. int
  84. xvasprintf(char **ret, const char *fmt, va_list ap)
  85. {
  86. int i;
  87. i = vasprintf(ret, fmt, ap);
  88. if (i < 0 || *ret == NULL)
  89. fatal("xvasprintf: could not allocate memory");
  90. return i;
  91. }
  92. int
  93. xasprintf(char **ret, const char *fmt, ...)
  94. {
  95. va_list ap;
  96. int i;
  97. va_start(ap, fmt);
  98. i = xvasprintf(ret, fmt, ap);
  99. va_end(ap);
  100. return i;
  101. }