Coverage Report

Created: 2025-08-29 06:10

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