Coverage Report

Created: 2023-12-08 06:48

/src/curl/lib/dynbuf.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 "dynbuf.h"
27
#include "curl_printf.h"
28
#ifdef BUILDING_LIBCURL
29
#include "curl_memory.h"
30
#endif
31
#include "memdebug.h"
32
33
4.08k
#define MIN_FIRST_ALLOC 32
34
35
1.72k
#define DYNINIT 0xbee51da /* random pattern */
36
37
/*
38
 * Init a dynbuf struct.
39
 */
40
void Curl_dyn_init(struct dynbuf *s, size_t toobig)
41
1.72k
{
42
1.72k
  DEBUGASSERT(s);
43
1.72k
  DEBUGASSERT(toobig);
44
1.72k
  s->bufr = NULL;
45
1.72k
  s->leng = 0;
46
1.72k
  s->allc = 0;
47
1.72k
  s->toobig = toobig;
48
1.72k
#ifdef DEBUGBUILD
49
1.72k
  s->init = DYNINIT;
50
1.72k
#endif
51
1.72k
}
52
53
/*
54
 * free the buffer and re-init the necessary fields. It doesn't touch the
55
 * 'init' field and thus this buffer can be reused to add data to again.
56
 */
57
void Curl_dyn_free(struct dynbuf *s)
58
762
{
59
762
  DEBUGASSERT(s);
60
762
  Curl_safefree(s->bufr);
61
762
  s->leng = s->allc = 0;
62
762
}
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
4.28k
{
70
4.28k
  size_t indx = s->leng;
71
4.28k
  size_t a = s->allc;
72
4.28k
  size_t fit = len + indx + 1; /* new string + old string + zero byte */
73
74
  /* try to detect if there's rubbish in the struct */
75
4.28k
  DEBUGASSERT(s->init == DYNINIT);
76
4.28k
  DEBUGASSERT(s->toobig);
77
4.28k
  DEBUGASSERT(indx < s->toobig);
78
4.28k
  DEBUGASSERT(!s->leng || s->bufr);
79
4.28k
  DEBUGASSERT(a <= s->toobig);
80
4.28k
  DEBUGASSERT(!len || mem);
81
82
4.28k
  if(fit > s->toobig) {
83
0
    Curl_dyn_free(s);
84
0
    return CURLE_OUT_OF_MEMORY;
85
0
  }
86
4.28k
  else if(!a) {
87
1.44k
    DEBUGASSERT(!indx);
88
    /* first invoke */
89
1.44k
    if(MIN_FIRST_ALLOC > s->toobig)
90
0
      a = s->toobig;
91
1.44k
    else if(fit < MIN_FIRST_ALLOC)
92
1.20k
      a = MIN_FIRST_ALLOC;
93
236
    else
94
236
      a = fit;
95
1.44k
  }
96
2.84k
  else {
97
2.84k
    while(a < fit)
98
0
      a *= 2;
99
2.84k
    if(a > s->toobig)
100
      /* no point in allocating a larger buffer than this is allowed to use */
101
0
      a = s->toobig;
102
2.84k
  }
103
104
4.28k
  if(a != s->allc) {
105
    /* this logic is not using Curl_saferealloc() to make the tool not have to
106
       include that as well when it uses this code */
107
1.44k
    void *p = realloc(s->bufr, a);
108
1.44k
    if(!p) {
109
0
      Curl_dyn_free(s);
110
0
      return CURLE_OUT_OF_MEMORY;
111
0
    }
112
1.44k
    s->bufr = p;
113
1.44k
    s->allc = a;
114
1.44k
  }
115
116
4.28k
  if(len)
117
4.27k
    memcpy(&s->bufr[indx], mem, len);
118
4.28k
  s->leng = indx + len;
119
4.28k
  s->bufr[s->leng] = 0;
120
4.28k
  return CURLE_OK;
121
4.28k
}
122
123
/*
124
 * Clears the string, keeps the allocation. This can also be called on a
125
 * buffer that already was freed.
126
 */
127
void Curl_dyn_reset(struct dynbuf *s)
128
588
{
129
588
  DEBUGASSERT(s);
130
588
  DEBUGASSERT(s->init == DYNINIT);
131
588
  DEBUGASSERT(!s->leng || s->bufr);
132
588
  if(s->leng)
133
487
    s->bufr[0] = 0;
134
588
  s->leng = 0;
135
588
}
136
137
/*
138
 * Specify the size of the tail to keep (number of bytes from the end of the
139
 * buffer). The rest will be dropped.
140
 */
141
CURLcode Curl_dyn_tail(struct dynbuf *s, size_t trail)
142
0
{
143
0
  DEBUGASSERT(s);
144
0
  DEBUGASSERT(s->init == DYNINIT);
145
0
  DEBUGASSERT(!s->leng || s->bufr);
146
0
  if(trail > s->leng)
147
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
148
0
  else if(trail == s->leng)
149
0
    return CURLE_OK;
150
0
  else if(!trail) {
151
0
    Curl_dyn_reset(s);
152
0
  }
153
0
  else {
154
0
    memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
155
0
    s->leng = trail;
156
0
    s->bufr[s->leng] = 0;
157
0
  }
158
0
  return CURLE_OK;
159
160
0
}
161
162
/*
163
 * Appends a buffer with length.
164
 */
165
CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
166
4.28k
{
167
4.28k
  DEBUGASSERT(s);
168
4.28k
  DEBUGASSERT(s->init == DYNINIT);
169
4.28k
  DEBUGASSERT(!s->leng || s->bufr);
170
4.28k
  return dyn_nappend(s, mem, len);
171
4.28k
}
172
173
/*
174
 * Append a null-terminated string at the end.
175
 */
176
CURLcode Curl_dyn_add(struct dynbuf *s, const char *str)
177
0
{
178
0
  size_t n;
179
0
  DEBUGASSERT(str);
180
0
  DEBUGASSERT(s);
181
0
  DEBUGASSERT(s->init == DYNINIT);
182
0
  DEBUGASSERT(!s->leng || s->bufr);
183
0
  n = strlen(str);
184
0
  return dyn_nappend(s, (unsigned char *)str, n);
185
0
}
186
187
/*
188
 * Append a string vprintf()-style
189
 */
190
CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap)
191
256
{
192
256
#ifdef BUILDING_LIBCURL
193
256
  int rc;
194
256
  DEBUGASSERT(s);
195
256
  DEBUGASSERT(s->init == DYNINIT);
196
256
  DEBUGASSERT(!s->leng || s->bufr);
197
256
  DEBUGASSERT(fmt);
198
256
  rc = Curl_dyn_vprintf(s, fmt, ap);
199
200
256
  if(!rc)
201
256
    return CURLE_OK;
202
#else
203
  char *str;
204
  str = vaprintf(fmt, ap); /* this allocs a new string to append */
205
206
  if(str) {
207
    CURLcode result = dyn_nappend(s, (unsigned char *)str, strlen(str));
208
    free(str);
209
    return result;
210
  }
211
  /* If we failed, we cleanup the whole buffer and return error */
212
  Curl_dyn_free(s);
213
#endif
214
0
  return CURLE_OUT_OF_MEMORY;
215
256
}
216
217
/*
218
 * Append a string printf()-style
219
 */
220
CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...)
221
256
{
222
256
  CURLcode result;
223
256
  va_list ap;
224
256
  DEBUGASSERT(s);
225
256
  DEBUGASSERT(s->init == DYNINIT);
226
256
  DEBUGASSERT(!s->leng || s->bufr);
227
256
  va_start(ap, fmt);
228
256
  result = Curl_dyn_vaddf(s, fmt, ap);
229
256
  va_end(ap);
230
256
  return result;
231
256
}
232
233
/*
234
 * Returns a pointer to the buffer.
235
 */
236
char *Curl_dyn_ptr(const struct dynbuf *s)
237
6.17k
{
238
6.17k
  DEBUGASSERT(s);
239
6.17k
  DEBUGASSERT(s->init == DYNINIT);
240
6.17k
  DEBUGASSERT(!s->leng || s->bufr);
241
6.17k
  return s->bufr;
242
6.17k
}
243
244
/*
245
 * Returns an unsigned pointer to the buffer.
246
 */
247
unsigned char *Curl_dyn_uptr(const struct dynbuf *s)
248
0
{
249
0
  DEBUGASSERT(s);
250
0
  DEBUGASSERT(s->init == DYNINIT);
251
0
  DEBUGASSERT(!s->leng || s->bufr);
252
0
  return (unsigned char *)s->bufr;
253
0
}
254
255
/*
256
 * Returns the length of the buffer.
257
 */
258
size_t Curl_dyn_len(const struct dynbuf *s)
259
2.23k
{
260
2.23k
  DEBUGASSERT(s);
261
2.23k
  DEBUGASSERT(s->init == DYNINIT);
262
2.23k
  DEBUGASSERT(!s->leng || s->bufr);
263
2.23k
  return s->leng;
264
2.23k
}
265
266
/*
267
 * Set a new (smaller) length.
268
 */
269
CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t set)
270
172
{
271
172
  DEBUGASSERT(s);
272
172
  DEBUGASSERT(s->init == DYNINIT);
273
172
  DEBUGASSERT(!s->leng || s->bufr);
274
172
  if(set > s->leng)
275
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
276
172
  s->leng = set;
277
172
  s->bufr[s->leng] = 0;
278
172
  return CURLE_OK;
279
172
}