2
0

strdup.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_saferealloc(ptr, size)
  88. *
  89. * Does a normal realloc(), but will free the data pointer if the realloc
  90. * fails. If 'size' is non-zero, it will free the data and return a failure.
  91. *
  92. * This convenience function is provided and used to help us avoid a common
  93. * mistake pattern when we could pass in a zero, catch the NULL return and end
  94. * up free'ing the memory twice.
  95. *
  96. * Returns the new pointer or NULL on failure.
  97. *
  98. ***************************************************************************/
  99. void *Curl_saferealloc(void *ptr, size_t size)
  100. {
  101. void *datap = realloc(ptr, size);
  102. if(size && !datap)
  103. /* only free 'ptr' if size was non-zero */
  104. free(ptr);
  105. return datap;
  106. }