strdup.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #ifdef WIN32
  25. #include <wchar.h>
  26. #endif
  27. #include "strdup.h"
  28. #include "curl_memory.h"
  29. /* The last #include file should be: */
  30. #include "memdebug.h"
  31. #ifndef HAVE_STRDUP
  32. char *curlx_strdup(const char *str)
  33. {
  34. size_t len;
  35. char *newstr;
  36. if(!str)
  37. return (char *)NULL;
  38. len = strlen(str) + 1;
  39. newstr = malloc(len);
  40. if(!newstr)
  41. return (char *)NULL;
  42. memcpy(newstr, str, len);
  43. return newstr;
  44. }
  45. #endif
  46. #ifdef WIN32
  47. /***************************************************************************
  48. *
  49. * Curl_wcsdup(source)
  50. *
  51. * Copies the 'source' wchar string to a newly allocated buffer (that is
  52. * returned).
  53. *
  54. * Returns the new pointer or NULL on failure.
  55. *
  56. ***************************************************************************/
  57. wchar_t *Curl_wcsdup(const wchar_t *src)
  58. {
  59. size_t length = wcslen(src);
  60. if(length > (SIZE_T_MAX / sizeof(wchar_t)) - 1)
  61. return (wchar_t *)NULL; /* integer overflow */
  62. return (wchar_t *)Curl_memdup(src, (length + 1) * sizeof(wchar_t));
  63. }
  64. #endif
  65. /***************************************************************************
  66. *
  67. * Curl_memdup(source, length)
  68. *
  69. * Copies the 'source' data to a newly allocated buffer (that is
  70. * returned). Copies 'length' bytes.
  71. *
  72. * Returns the new pointer or NULL on failure.
  73. *
  74. ***************************************************************************/
  75. void *Curl_memdup(const void *src, size_t length)
  76. {
  77. void *buffer = malloc(length);
  78. if(!buffer)
  79. return NULL; /* fail */
  80. memcpy(buffer, src, length);
  81. return buffer;
  82. }
  83. /***************************************************************************
  84. *
  85. * Curl_saferealloc(ptr, size)
  86. *
  87. * Does a normal realloc(), but will free the data pointer if the realloc
  88. * fails. If 'size' is non-zero, it will free the data and return a failure.
  89. *
  90. * This convenience function is provided and used to help us avoid a common
  91. * mistake pattern when we could pass in a zero, catch the NULL return and end
  92. * up free'ing the memory twice.
  93. *
  94. * Returns the new pointer or NULL on failure.
  95. *
  96. ***************************************************************************/
  97. void *Curl_saferealloc(void *ptr, size_t size)
  98. {
  99. void *datap = realloc(ptr, size);
  100. if(size && !datap)
  101. /* only free 'ptr' if size was non-zero */
  102. free(ptr);
  103. return datap;
  104. }