Coverage Report

Created: 2026-02-26 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/bufref.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
#include "curl_setup.h"
25
26
#include "urldata.h"
27
#include "bufref.h"
28
#include "curlx/strdup.h"
29
30
#ifdef DEBUGBUILD
31
719k
#define SIGNATURE 0x5c48e9b2    /* Random pattern. */
32
#endif
33
34
/*
35
 * Init a bufref struct.
36
 */
37
void Curl_bufref_init(struct bufref *br)
38
719k
{
39
719k
  DEBUGASSERT(br);
40
719k
  br->dtor = NULL;
41
719k
  br->ptr = NULL;
42
719k
  br->len = 0;
43
44
719k
#ifdef DEBUGBUILD
45
719k
  br->signature = SIGNATURE;
46
719k
#endif
47
719k
}
48
49
/*
50
 * Free the buffer and re-init the necessary fields. It does not touch the
51
 * 'signature' field and thus this buffer reference can be reused.
52
 */
53
54
void Curl_bufref_free(struct bufref *br)
55
1.48M
{
56
1.48M
  DEBUGASSERT(br);
57
1.48M
  DEBUGASSERT(br->signature == SIGNATURE);
58
1.48M
  DEBUGASSERT(br->ptr || !br->len);
59
60
1.48M
  if(br->ptr && br->dtor)
61
166k
    br->dtor(CURL_UNCONST(br->ptr));
62
63
1.48M
  br->dtor = NULL;
64
1.48M
  br->ptr = NULL;
65
1.48M
  br->len = 0;
66
1.48M
}
67
68
/*
69
 * Set the buffer reference to new values. The previously referenced buffer
70
 * is released before assignment.
71
 */
72
void Curl_bufref_set(struct bufref *br, const void *ptr, size_t len,
73
                     void (*dtor)(void *))
74
415k
{
75
415k
  DEBUGASSERT(ptr || !len);
76
415k
  DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
77
78
415k
  Curl_bufref_free(br);
79
415k
  br->ptr = (const unsigned char *)ptr;
80
415k
  br->len = len;
81
415k
  br->dtor = dtor;
82
415k
}
83
84
/*
85
 * Get a pointer to the referenced buffer.
86
 */
87
const unsigned char *Curl_bufref_uptr(const struct bufref *br)
88
4.57k
{
89
4.57k
  DEBUGASSERT(br);
90
4.57k
  DEBUGASSERT(br->signature == SIGNATURE);
91
4.57k
  DEBUGASSERT(br->ptr || !br->len);
92
93
4.57k
  return br->ptr;
94
4.57k
}
95
96
/*
97
 * Get a pointer to the referenced string.
98
 */
99
const char *Curl_bufref_ptr(const struct bufref *br)
100
391k
{
101
391k
  DEBUGASSERT(br);
102
391k
  DEBUGASSERT(br->signature == SIGNATURE);
103
391k
  DEBUGASSERT(br->ptr || !br->len);
104
105
391k
  return (const char *)br->ptr;
106
391k
}
107
108
/*
109
 * Get the length of the referenced buffer data.
110
 */
111
size_t Curl_bufref_len(const struct bufref *br)
112
9.04k
{
113
9.04k
  DEBUGASSERT(br);
114
9.04k
  DEBUGASSERT(br->signature == SIGNATURE);
115
9.04k
  DEBUGASSERT(br->ptr || !br->len);
116
117
9.04k
  return br->len;
118
9.04k
}
119
120
CURLcode Curl_bufref_memdup0(struct bufref *br, const void *ptr, size_t len)
121
1.12k
{
122
1.12k
  unsigned char *cpy = NULL;
123
124
1.12k
  DEBUGASSERT(br);
125
1.12k
  DEBUGASSERT(br->signature == SIGNATURE);
126
1.12k
  DEBUGASSERT(br->ptr || !br->len);
127
1.12k
  DEBUGASSERT(ptr || !len);
128
1.12k
  DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
129
130
1.12k
  if(ptr) {
131
1.12k
    cpy = curlx_memdup0(ptr, len);
132
1.12k
    if(!cpy)
133
0
      return CURLE_OUT_OF_MEMORY;
134
1.12k
  }
135
136
1.12k
  Curl_bufref_set(br, cpy, len, curl_free);
137
1.12k
  return CURLE_OK;
138
1.12k
}