strdup.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #ifdef _WIN32
  27. #include <wchar.h>
  28. #endif
  29. #include "strdup.h"
  30. #include "curl_memory.h"
  31. /* The last #include file should be: */
  32. #include "memdebug.h"
  33. #ifndef HAVE_STRDUP
  34. char *Curl_strdup(const char *str)
  35. {
  36. size_t len;
  37. char *newstr;
  38. if(!str)
  39. return (char *)NULL;
  40. len = strlen(str) + 1;
  41. newstr = malloc(len);
  42. if(!newstr)
  43. return (char *)NULL;
  44. memcpy(newstr, str, len);
  45. return newstr;
  46. }
  47. #endif
  48. #ifdef _WIN32
  49. /***************************************************************************
  50. *
  51. * Curl_wcsdup(source)
  52. *
  53. * Copies the 'source' wchar string to a newly allocated buffer (that is
  54. * returned).
  55. *
  56. * Returns the new pointer or NULL on failure.
  57. *
  58. ***************************************************************************/
  59. wchar_t *Curl_wcsdup(const wchar_t *src)
  60. {
  61. size_t length = wcslen(src);
  62. if(length > (SIZE_T_MAX / sizeof(wchar_t)) - 1)
  63. return (wchar_t *)NULL; /* integer overflow */
  64. return (wchar_t *)Curl_memdup(src, (length + 1) * sizeof(wchar_t));
  65. }
  66. #endif
  67. /***************************************************************************
  68. *
  69. * Curl_memdup(source, length)
  70. *
  71. * Copies the 'source' data to a newly allocated buffer (that is
  72. * returned). Copies 'length' bytes.
  73. *
  74. * Returns the new pointer or NULL on failure.
  75. *
  76. ***************************************************************************/
  77. void *Curl_memdup(const void *src, size_t length)
  78. {
  79. void *buffer = malloc(length);
  80. if(!buffer)
  81. return NULL; /* fail */
  82. memcpy(buffer, src, length);
  83. return buffer;
  84. }
  85. /***************************************************************************
  86. *
  87. * Curl_memdup0(source, length)
  88. *
  89. * Copies the 'source' string to a newly allocated buffer (that is returned).
  90. * Copies 'length' bytes then adds a null terminator.
  91. *
  92. * Returns the new pointer or NULL on failure.
  93. *
  94. ***************************************************************************/
  95. void *Curl_memdup0(const char *src, size_t length)
  96. {
  97. char *buf = malloc(length + 1);
  98. if(!buf)
  99. return NULL;
  100. memcpy(buf, src, length);
  101. buf[length] = 0;
  102. return buf;
  103. }
  104. /***************************************************************************
  105. *
  106. * Curl_saferealloc(ptr, size)
  107. *
  108. * Does a normal realloc(), but will free the data pointer if the realloc
  109. * fails. If 'size' is non-zero, it will free the data and return a failure.
  110. *
  111. * This convenience function is provided and used to help us avoid a common
  112. * mistake pattern when we could pass in a zero, catch the NULL return and end
  113. * up free'ing the memory twice.
  114. *
  115. * Returns the new pointer or NULL on failure.
  116. *
  117. ***************************************************************************/
  118. void *Curl_saferealloc(void *ptr, size_t size)
  119. {
  120. void *datap = realloc(ptr, size);
  121. if(size && !datap)
  122. /* only free 'ptr' if size was non-zero */
  123. free(ptr);
  124. return datap;
  125. }