Line | Count | Source |
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 | | |
26 | | #ifdef _WIN32 |
27 | | #include <wchar.h> |
28 | | #endif |
29 | | |
30 | | #include "strdup.h" |
31 | | |
32 | | #ifndef HAVE_STRDUP |
33 | | char *Curl_strdup(const char *str) |
34 | | { |
35 | | size_t len; |
36 | | char *newstr; |
37 | | |
38 | | if(!str) |
39 | | return (char *)NULL; |
40 | | |
41 | | len = strlen(str) + 1; |
42 | | |
43 | | newstr = curlx_malloc(len); |
44 | | if(!newstr) |
45 | | return (char *)NULL; |
46 | | |
47 | | memcpy(newstr, str, len); |
48 | | return newstr; |
49 | | } |
50 | | #endif |
51 | | |
52 | | #ifdef _WIN32 |
53 | | /*************************************************************************** |
54 | | * |
55 | | * Curl_wcsdup(source) |
56 | | * |
57 | | * Copies the 'source' wchar string to a newly allocated buffer (that is |
58 | | * returned). |
59 | | * |
60 | | * Returns the new pointer or NULL on failure. |
61 | | * |
62 | | ***************************************************************************/ |
63 | | wchar_t *Curl_wcsdup(const wchar_t *src) |
64 | | { |
65 | | size_t length = wcslen(src); |
66 | | |
67 | | if(length > (SIZE_MAX / sizeof(wchar_t)) - 1) |
68 | | return (wchar_t *)NULL; /* integer overflow */ |
69 | | |
70 | | return (wchar_t *)Curl_memdup(src, (length + 1) * sizeof(wchar_t)); |
71 | | } |
72 | | #endif |
73 | | |
74 | | /*************************************************************************** |
75 | | * |
76 | | * Curl_memdup(source, length) |
77 | | * |
78 | | * Copies the 'source' data to a newly allocated buffer (that is |
79 | | * returned). Copies 'length' bytes. |
80 | | * |
81 | | * Returns the new pointer or NULL on failure. |
82 | | * |
83 | | ***************************************************************************/ |
84 | | void *Curl_memdup(const void *src, size_t length) |
85 | 1.36k | { |
86 | 1.36k | void *buffer = curlx_malloc(length); |
87 | 1.36k | if(!buffer) |
88 | 0 | return NULL; /* fail */ |
89 | | |
90 | 1.36k | memcpy(buffer, src, length); |
91 | | |
92 | 1.36k | return buffer; |
93 | 1.36k | } |
94 | | |
95 | | /*************************************************************************** |
96 | | * |
97 | | * Curl_memdup0(source, length) |
98 | | * |
99 | | * Copies the 'source' string to a newly allocated buffer (that is returned). |
100 | | * Copies 'length' bytes then adds a null-terminator. |
101 | | * |
102 | | * Returns the new pointer or NULL on failure. |
103 | | * |
104 | | ***************************************************************************/ |
105 | | void *Curl_memdup0(const char *src, size_t length) |
106 | 4.34k | { |
107 | 4.34k | char *buf = (length < SIZE_MAX) ? curlx_malloc(length + 1) : NULL; |
108 | 4.34k | if(!buf) |
109 | 0 | return NULL; |
110 | 4.34k | if(length) { |
111 | 3.14k | DEBUGASSERT(src); /* must never be NULL */ |
112 | 3.14k | memcpy(buf, src, length); |
113 | 3.14k | } |
114 | 4.34k | buf[length] = 0; |
115 | 4.34k | return buf; |
116 | 4.34k | } |
117 | | |
118 | | /*************************************************************************** |
119 | | * |
120 | | * Curl_saferealloc(ptr, size) |
121 | | * |
122 | | * Does a normal curlx_realloc(), but will free the data pointer if the realloc |
123 | | * fails. If 'size' is non-zero, it will free the data and return a failure. |
124 | | * |
125 | | * This convenience function is provided and used to help us avoid a common |
126 | | * mistake pattern when we could pass in a zero, catch the NULL return and end |
127 | | * up free'ing the memory twice. |
128 | | * |
129 | | * Returns the new pointer or NULL on failure. |
130 | | * |
131 | | ***************************************************************************/ |
132 | | void *Curl_saferealloc(void *ptr, size_t size) |
133 | 0 | { |
134 | 0 | void *datap = curlx_realloc(ptr, size); |
135 | 0 | if(size && !datap) |
136 | | /* only free 'ptr' if size was non-zero */ |
137 | 0 | curlx_free(ptr); |
138 | 0 | return datap; |
139 | 0 | } |