Coverage Report

Created: 2026-09-14 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curlx/dynbuf.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 "curlx/dynbuf.h"
27
#include "curl_printf.h"
28
29
2.95M
#define MIN_FIRST_ALLOC 32
30
31
#ifdef DEBUGBUILD
32
1.17M
#define DYNINIT 0xbee51da /* random pattern */
33
#endif
34
35
/*
36
 * Init a dynbuf struct.
37
 */
38
void curlx_dyn_init(struct dynbuf *s, size_t toobig)
39
1.17M
{
40
1.17M
  DEBUGASSERT(s);
41
1.17M
  DEBUGASSERT(toobig);
42
1.17M
  DEBUGASSERT(toobig <= MAX_DYNBUF_SIZE); /* catch crazy mistakes */
43
1.17M
  s->bufr = NULL;
44
1.17M
  s->leng = 0;
45
1.17M
  s->allc = 0;
46
1.17M
  s->toobig = toobig;
47
1.17M
#ifdef DEBUGBUILD
48
1.17M
  s->init = DYNINIT;
49
1.17M
#endif
50
1.17M
}
51
52
/*
53
 * free the buffer and re-init the necessary fields. It does not touch the
54
 * 'init' field and thus this buffer can be reused to add data to again.
55
 */
56
void curlx_dyn_free(struct dynbuf *s)
57
581k
{
58
581k
  DEBUGASSERT(s);
59
581k
  DEBUGASSERT(s->init == DYNINIT);
60
581k
  curlx_safefree(s->bufr);
61
581k
  s->leng = s->allc = 0;
62
581k
}
63
64
/*
65
 * Store/append an chunk of memory to the dynbuf.
66
 */
67
static CURLcode dyn_nappend(struct dynbuf *s,
68
                            const unsigned char *mem, size_t len)
69
10.6M
{
70
10.6M
  size_t idx = s->leng;
71
10.6M
  size_t a = s->allc;
72
10.6M
  size_t fit = len + idx + 1; /* new string + old string + zero byte */
73
74
  /* try to detect if there is rubbish in the struct */
75
10.6M
  DEBUGASSERT(s->init == DYNINIT);
76
10.6M
  DEBUGASSERT(s->toobig);
77
10.6M
  DEBUGASSERT(idx < s->toobig);
78
10.6M
  DEBUGASSERT(!s->leng || s->bufr);
79
10.6M
  DEBUGASSERT(a <= s->toobig);
80
10.6M
  DEBUGASSERT(!len || mem);
81
82
10.6M
  if(fit > s->toobig) {
83
25
    curlx_dyn_free(s);
84
25
    return CURLE_TOO_LARGE;
85
25
  }
86
10.6M
  else if(!a) {
87
1.08M
    DEBUGASSERT(!idx);
88
    /* first invoke */
89
1.08M
    if(MIN_FIRST_ALLOC > s->toobig)
90
52.3k
      a = s->toobig;
91
1.03M
    else if(fit < MIN_FIRST_ALLOC)
92
830k
      a = MIN_FIRST_ALLOC;
93
205k
    else
94
205k
      a = fit;
95
1.08M
  }
96
9.56M
  else {
97
9.81M
    while(a < fit)
98
255k
      a *= 2;
99
9.56M
    if(a > s->toobig)
100
      /* no point in allocating a larger buffer than this is allowed to use */
101
1.66k
      a = s->toobig;
102
9.56M
  }
103
104
10.6M
  if(a != s->allc) {
105
1.30M
    void *p = curlx_realloc(s->bufr, a);
106
1.30M
    if(!p) {
107
0
      curlx_dyn_free(s);
108
0
      return CURLE_OUT_OF_MEMORY;
109
0
    }
110
1.30M
    s->bufr = p;
111
1.30M
    s->allc = a;
112
1.30M
  }
113
114
10.6M
  if(len)
115
10.5M
    memcpy(&s->bufr[idx], mem, len);
116
10.6M
  s->leng = idx + len;
117
10.6M
  s->bufr[s->leng] = 0;
118
10.6M
  return CURLE_OK;
119
10.6M
}
120
121
/*
122
 * Clears the string, keeps the allocation. This can also be called on a
123
 * buffer that already was freed.
124
 */
125
void curlx_dyn_reset(struct dynbuf *s)
126
594k
{
127
594k
  DEBUGASSERT(s);
128
594k
  DEBUGASSERT(s->init == DYNINIT);
129
594k
  DEBUGASSERT(!s->leng || s->bufr);
130
594k
  if(s->leng)
131
252k
    s->bufr[0] = 0;
132
594k
  s->leng = 0;
133
594k
}
134
135
/*
136
 * Specify the size of the tail to keep (number of bytes from the end of the
137
 * buffer). The rest is dropped.
138
 */
139
CURLcode curlx_dyn_tail(struct dynbuf *s, size_t trail)
140
0
{
141
0
  DEBUGASSERT(s);
142
0
  DEBUGASSERT(s->init == DYNINIT);
143
0
  DEBUGASSERT(!s->leng || s->bufr);
144
0
  if(trail > s->leng)
145
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
146
0
  else if(trail == s->leng)
147
0
    return CURLE_OK;
148
0
  else if(!trail) {
149
0
    curlx_dyn_reset(s);
150
0
  }
151
0
  else {
152
0
    memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
153
0
    s->leng = trail;
154
0
    s->bufr[s->leng] = 0;
155
0
  }
156
0
  return CURLE_OK;
157
0
}
158
159
/*
160
 * Appends a buffer with length.
161
 */
162
CURLcode curlx_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
163
10.2M
{
164
10.2M
  DEBUGASSERT(s);
165
10.2M
  DEBUGASSERT(s->init == DYNINIT);
166
10.2M
  DEBUGASSERT(!s->leng || s->bufr);
167
10.2M
  return dyn_nappend(s, mem, len);
168
10.2M
}
169
170
/*
171
 * Append a null-terminated string at the end.
172
 */
173
CURLcode curlx_dyn_add(struct dynbuf *s, const char *str)
174
426k
{
175
426k
  size_t n;
176
426k
  DEBUGASSERT(str);
177
426k
  DEBUGASSERT(s);
178
426k
  DEBUGASSERT(s->init == DYNINIT);
179
426k
  DEBUGASSERT(!s->leng || s->bufr);
180
426k
  n = strlen(str);
181
426k
  return dyn_nappend(s, (const unsigned char *)str, n);
182
426k
}
183
184
/*
185
 * Append a string vprintf()-style
186
 */
187
CURLcode curlx_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap)
188
546k
{
189
546k
#ifdef BUILDING_LIBCURL
190
546k
  int rc;
191
546k
  DEBUGASSERT(s);
192
546k
  DEBUGASSERT(s->init == DYNINIT);
193
546k
  DEBUGASSERT(!s->leng || s->bufr);
194
546k
  DEBUGASSERT(fmt);
195
546k
  rc = curlx_dyn_vprintf(s, fmt, ap);
196
197
546k
  if(!rc)
198
546k
    return CURLE_OK;
199
15
  else if(rc == MERR_TOO_LARGE)
200
15
    return CURLE_TOO_LARGE;
201
0
  return CURLE_OUT_OF_MEMORY;
202
#else
203
  char *str;
204
  str = curl_mvaprintf(fmt, ap); /* this allocs a new string to append */
205
206
  if(str) {
207
    CURLcode result = dyn_nappend(s, (const unsigned char *)str, strlen(str));
208
    curl_free(str);
209
    return result;
210
  }
211
  /* If we failed, we cleanup the whole buffer and return error */
212
  curlx_dyn_free(s);
213
  return CURLE_OUT_OF_MEMORY;
214
#endif
215
546k
}
216
217
/*
218
 * Append a string printf()-style
219
 */
220
CURLcode curlx_dyn_addf(struct dynbuf *s, const char *fmt, ...)
221
546k
{
222
546k
  CURLcode result;
223
546k
  va_list ap;
224
546k
  DEBUGASSERT(s);
225
546k
  DEBUGASSERT(s->init == DYNINIT);
226
546k
  DEBUGASSERT(!s->leng || s->bufr);
227
546k
  DEBUGASSERT(strcmp(fmt, "%s")); /* use curlx_dyn_add instead */
228
546k
  va_start(ap, fmt);
229
546k
  result = curlx_dyn_vaddf(s, fmt, ap);
230
546k
  va_end(ap);
231
546k
  return result;
232
546k
}
233
234
/*
235
 * Returns a pointer to the buffer.
236
 */
237
char *curlx_dyn_ptr(const struct dynbuf *s)
238
1.86M
{
239
1.86M
  DEBUGASSERT(s);
240
1.86M
  DEBUGASSERT(s->init == DYNINIT);
241
1.86M
  DEBUGASSERT(!s->leng || s->bufr);
242
1.86M
  return s->bufr;
243
1.86M
}
244
245
char *curlx_dyn_take(struct dynbuf *s, size_t *plen)
246
60.3k
{
247
60.3k
  char *ptr = s->bufr;
248
60.3k
  DEBUGASSERT(s);
249
60.3k
  DEBUGASSERT(s->init == DYNINIT);
250
60.3k
  *plen = s->leng;
251
60.3k
  s->bufr = NULL;
252
60.3k
  s->leng = 0;
253
60.3k
  s->allc = 0;
254
60.3k
  return ptr;
255
60.3k
}
256
257
/*
258
 * Returns an unsigned pointer to the buffer.
259
 */
260
unsigned char *curlx_dyn_uptr(const struct dynbuf *s)
261
27
{
262
27
  DEBUGASSERT(s);
263
27
  DEBUGASSERT(s->init == DYNINIT);
264
27
  DEBUGASSERT(!s->leng || s->bufr);
265
27
  return (unsigned char *)s->bufr;
266
27
}
267
268
/*
269
 * Returns the length of the buffer.
270
 */
271
size_t curlx_dyn_len(const struct dynbuf *s)
272
1.82M
{
273
1.82M
  DEBUGASSERT(s);
274
1.82M
  DEBUGASSERT(s->init == DYNINIT);
275
1.82M
  DEBUGASSERT(!s->leng || s->bufr);
276
1.82M
  return s->leng;
277
1.82M
}
278
279
/*
280
 * Set a new (smaller) length.
281
 */
282
CURLcode curlx_dyn_setlen(struct dynbuf *s, size_t set)
283
5.28k
{
284
5.28k
  DEBUGASSERT(s);
285
5.28k
  DEBUGASSERT(s->init == DYNINIT);
286
5.28k
  DEBUGASSERT(!s->leng || s->bufr);
287
5.28k
  if(set > s->leng)
288
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
289
5.28k
  s->leng = set;
290
5.28k
  s->bufr[s->leng] = 0;
291
5.28k
  return CURLE_OK;
292
5.28k
}