xmalloc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* $OpenBSD$ */
  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 <errno.h>
  16. #include <limits.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "tmux.h"
  22. void *
  23. xmalloc(size_t size)
  24. {
  25. void *ptr;
  26. if (size == 0)
  27. fatal("xmalloc: zero size");
  28. ptr = malloc(size);
  29. if (ptr == NULL)
  30. fatal("xmalloc: allocating %zu bytes: %s",
  31. size, strerror(errno));
  32. return ptr;
  33. }
  34. void *
  35. xcalloc(size_t nmemb, size_t size)
  36. {
  37. void *ptr;
  38. if (size == 0 || nmemb == 0)
  39. fatal("xcalloc: zero size");
  40. ptr = calloc(nmemb, size);
  41. if (ptr == NULL)
  42. fatal("xcalloc: allocating %zu * %zu bytes: %s",
  43. nmemb, size, strerror(errno));
  44. return ptr;
  45. }
  46. void *
  47. xrealloc(void *ptr, size_t size)
  48. {
  49. return xreallocarray(ptr, 1, size);
  50. }
  51. void *
  52. xreallocarray(void *ptr, size_t nmemb, size_t size)
  53. {
  54. void *new_ptr;
  55. if (nmemb == 0 || size == 0)
  56. fatal("xreallocarray: zero size");
  57. new_ptr = reallocarray(ptr, nmemb, size);
  58. if (new_ptr == NULL)
  59. fatal("xreallocarray: allocating %zu * %zu bytes: %s",
  60. nmemb, size, strerror(errno));
  61. return new_ptr;
  62. }
  63. char *
  64. xstrdup(const char *str)
  65. {
  66. char *cp;
  67. if ((cp = strdup(str)) == NULL)
  68. fatal("xstrdup: %s", strerror(errno));
  69. return cp;
  70. }
  71. int
  72. xasprintf(char **ret, const char *fmt, ...)
  73. {
  74. va_list ap;
  75. int i;
  76. va_start(ap, fmt);
  77. i = xvasprintf(ret, fmt, ap);
  78. va_end(ap);
  79. return i;
  80. }
  81. int
  82. xvasprintf(char **ret, const char *fmt, va_list ap)
  83. {
  84. int i;
  85. i = vasprintf(ret, fmt, ap);
  86. if (i < 0 || *ret == NULL)
  87. fatal("xasprintf: %s", strerror(errno));
  88. return i;
  89. }
  90. int
  91. xsnprintf(char *str, size_t len, const char *fmt, ...)
  92. {
  93. va_list ap;
  94. int i;
  95. va_start(ap, fmt);
  96. i = xvsnprintf(str, len, fmt, ap);
  97. va_end(ap);
  98. return i;
  99. }
  100. int
  101. xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
  102. {
  103. int i;
  104. if (len > INT_MAX)
  105. fatal("xsnprintf: len > INT_MAX");
  106. i = vsnprintf(str, len, fmt, ap);
  107. if (i < 0 || i >= (int)len)
  108. fatal("xsnprintf: overflow");
  109. return i;
  110. }