Coverage Report

Created: 2026-09-01 06:58

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
4.23M
#define MIN_FIRST_ALLOC 32
30
31
#ifdef DEBUGBUILD
32
1.51M
#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.51M
{
40
1.51M
  DEBUGASSERT(s);
41
1.51M
  DEBUGASSERT(toobig);
42
1.51M
  DEBUGASSERT(toobig <= MAX_DYNBUF_SIZE); /* catch crazy mistakes */
43
1.51M
  s->bufr = NULL;
44
1.51M
  s->leng = 0;
45
1.51M
  s->allc = 0;
46
1.51M
  s->toobig = toobig;
47
1.51M
#ifdef DEBUGBUILD
48
1.51M
  s->init = DYNINIT;
49
1.51M
#endif
50
1.51M
}
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
222k
{
58
222k
  DEBUGASSERT(s);
59
222k
  DEBUGASSERT(s->init == DYNINIT);
60
222k
  curlx_safefree(s->bufr);
61
222k
  s->leng = s->allc = 0;
62
222k
}
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
176M
{
70
176M
  size_t idx = s->leng;
71
176M
  size_t a = s->allc;
72
176M
  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
176M
  DEBUGASSERT(s->init == DYNINIT);
76
176M
  DEBUGASSERT(s->toobig);
77
176M
  DEBUGASSERT(idx < s->toobig);
78
176M
  DEBUGASSERT(!s->leng || s->bufr);
79
176M
  DEBUGASSERT(a <= s->toobig);
80
176M
  DEBUGASSERT(!len || mem);
81
82
176M
  if(fit > s->toobig) {
83
53
    curlx_dyn_free(s);
84
53
    return CURLE_TOO_LARGE;
85
53
  }
86
176M
  else if(!a) {
87
1.42M
    DEBUGASSERT(!idx);
88
    /* first invoke */
89
1.42M
    if(MIN_FIRST_ALLOC > s->toobig)
90
10.0k
      a = s->toobig;
91
1.41M
    else if(fit < MIN_FIRST_ALLOC)
92
1.40M
      a = MIN_FIRST_ALLOC;
93
4.02k
    else
94
4.02k
      a = fit;
95
1.42M
  }
96
175M
  else {
97
175M
    while(a < fit)
98
78.4k
      a *= 2;
99
175M
    if(a > s->toobig)
100
      /* no point in allocating a larger buffer than this is allowed to use */
101
347
      a = s->toobig;
102
175M
  }
103
104
176M
  if(a != s->allc) {
105
1.49M
    void *p = curlx_realloc(s->bufr, a);
106
1.49M
    if(!p) {
107
0
      curlx_dyn_free(s);
108
0
      return CURLE_OUT_OF_MEMORY;
109
0
    }
110
1.49M
    s->bufr = p;
111
1.49M
    s->allc = a;
112
1.49M
  }
113
114
176M
  if(len)
115
176M
    memcpy(&s->bufr[idx], mem, len);
116
176M
  s->leng = idx + len;
117
176M
  s->bufr[s->leng] = 0;
118
176M
  return CURLE_OK;
119
176M
}
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
496k
{
127
496k
  DEBUGASSERT(s);
128
496k
  DEBUGASSERT(s->init == DYNINIT);
129
496k
  DEBUGASSERT(!s->leng || s->bufr);
130
496k
  if(s->leng)
131
70.5k
    s->bufr[0] = 0;
132
496k
  s->leng = 0;
133
496k
}
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
53
{
141
53
  DEBUGASSERT(s);
142
53
  DEBUGASSERT(s->init == DYNINIT);
143
53
  DEBUGASSERT(!s->leng || s->bufr);
144
53
  if(trail > s->leng)
145
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
146
53
  else if(trail == s->leng)
147
0
    return CURLE_OK;
148
53
  else if(!trail) {
149
53
    curlx_dyn_reset(s);
150
53
  }
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
53
  return CURLE_OK;
157
53
}
158
159
/*
160
 * Appends a buffer with length.
161
 */
162
CURLcode curlx_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
163
176M
{
164
176M
  DEBUGASSERT(s);
165
176M
  DEBUGASSERT(s->init == DYNINIT);
166
176M
  DEBUGASSERT(!s->leng || s->bufr);
167
176M
  return dyn_nappend(s, mem, len);
168
176M
}
169
170
/*
171
 * Append a null-terminated string at the end.
172
 */
173
CURLcode curlx_dyn_add(struct dynbuf *s, const char *str)
174
79.3k
{
175
79.3k
  size_t n;
176
79.3k
  DEBUGASSERT(str);
177
79.3k
  DEBUGASSERT(s);
178
79.3k
  DEBUGASSERT(s->init == DYNINIT);
179
79.3k
  DEBUGASSERT(!s->leng || s->bufr);
180
79.3k
  n = strlen(str);
181
79.3k
  return dyn_nappend(s, (const unsigned char *)str, n);
182
79.3k
}
183
184
/*
185
 * Append a string vprintf()-style
186
 */
187
CURLcode curlx_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap)
188
108k
{
189
108k
#ifdef BUILDING_LIBCURL
190
108k
  int rc;
191
108k
  DEBUGASSERT(s);
192
108k
  DEBUGASSERT(s->init == DYNINIT);
193
108k
  DEBUGASSERT(!s->leng || s->bufr);
194
108k
  DEBUGASSERT(fmt);
195
108k
  rc = curlx_dyn_vprintf(s, fmt, ap);
196
197
108k
  if(!rc)
198
108k
    return CURLE_OK;
199
18
  else if(rc == MERR_TOO_LARGE)
200
18
    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
108k
}
216
217
/*
218
 * Append a string printf()-style
219
 */
220
CURLcode curlx_dyn_addf(struct dynbuf *s, const char *fmt, ...)
221
108k
{
222
108k
  CURLcode result;
223
108k
  va_list ap;
224
108k
  DEBUGASSERT(s);
225
108k
  DEBUGASSERT(s->init == DYNINIT);
226
108k
  DEBUGASSERT(!s->leng || s->bufr);
227
108k
  DEBUGASSERT(strcmp(fmt, "%s")); /* use curlx_dyn_add instead */
228
108k
  va_start(ap, fmt);
229
108k
  result = curlx_dyn_vaddf(s, fmt, ap);
230
108k
  va_end(ap);
231
108k
  return result;
232
108k
}
233
234
/*
235
 * Returns a pointer to the buffer.
236
 */
237
char *curlx_dyn_ptr(const struct dynbuf *s)
238
1.64M
{
239
1.64M
  DEBUGASSERT(s);
240
1.64M
  DEBUGASSERT(s->init == DYNINIT);
241
1.64M
  DEBUGASSERT(!s->leng || s->bufr);
242
1.64M
  return s->bufr;
243
1.64M
}
244
245
char *curlx_dyn_take(struct dynbuf *s, size_t *plen)
246
14.5k
{
247
14.5k
  char *ptr = s->bufr;
248
14.5k
  DEBUGASSERT(s);
249
14.5k
  DEBUGASSERT(s->init == DYNINIT);
250
14.5k
  *plen = s->leng;
251
14.5k
  s->bufr = NULL;
252
14.5k
  s->leng = 0;
253
14.5k
  s->allc = 0;
254
14.5k
  return ptr;
255
14.5k
}
256
257
/*
258
 * Returns an unsigned pointer to the buffer.
259
 */
260
unsigned char *curlx_dyn_uptr(const struct dynbuf *s)
261
807
{
262
807
  DEBUGASSERT(s);
263
807
  DEBUGASSERT(s->init == DYNINIT);
264
807
  DEBUGASSERT(!s->leng || s->bufr);
265
807
  return (unsigned char *)s->bufr;
266
807
}
267
268
/*
269
 * Returns the length of the buffer.
270
 */
271
size_t curlx_dyn_len(const struct dynbuf *s)
272
1.87M
{
273
1.87M
  DEBUGASSERT(s);
274
1.87M
  DEBUGASSERT(s->init == DYNINIT);
275
1.87M
  DEBUGASSERT(!s->leng || s->bufr);
276
1.87M
  return s->leng;
277
1.87M
}
278
279
/*
280
 * Set a new (smaller) length.
281
 */
282
CURLcode curlx_dyn_setlen(struct dynbuf *s, size_t set)
283
7.90k
{
284
7.90k
  DEBUGASSERT(s);
285
7.90k
  DEBUGASSERT(s->init == DYNINIT);
286
7.90k
  DEBUGASSERT(!s->leng || s->bufr);
287
7.90k
  if(set > s->leng)
288
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
289
7.90k
  s->leng = set;
290
7.90k
  s->bufr[s->leng] = 0;
291
7.90k
  return CURLE_OK;
292
7.90k
}