Coverage Report

Created: 2026-03-30 09:00

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