Coverage Report

Created: 2025-12-03 07:02

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