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