Coverage Report

Created: 2022-11-30 06:20

/src/curl/lib/dynbuf.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 2020 - 2022, 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
20.3M
#define MIN_FIRST_ALLOC 32
34
35
10.3M
#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
10.3M
{
42
10.3M
  DEBUGASSERT(s);
43
10.3M
  DEBUGASSERT(toobig);
44
10.3M
  s->bufr = NULL;
45
10.3M
  s->leng = 0;
46
10.3M
  s->allc = 0;
47
10.3M
  s->toobig = toobig;
48
10.3M
#ifdef DEBUGBUILD
49
10.3M
  s->init = DYNINIT;
50
10.3M
#endif
51
10.3M
}
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
419k
{
59
419k
  DEBUGASSERT(s);
60
419k
  Curl_safefree(s->bufr);
61
419k
  s->leng = s->allc = 0;
62
419k
}
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
1.49G
{
70
1.49G
  size_t indx = s->leng;
71
1.49G
  size_t a = s->allc;
72
1.49G
  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
1.49G
  DEBUGASSERT(s->init == DYNINIT);
76
1.49G
  DEBUGASSERT(s->toobig);
77
1.49G
  DEBUGASSERT(indx < s->toobig);
78
1.49G
  DEBUGASSERT(!s->leng || s->bufr);
79
80
1.49G
  if(fit > s->toobig) {
81
79
    Curl_dyn_free(s);
82
79
    return CURLE_OUT_OF_MEMORY;
83
79
  }
84
1.49G
  else if(!a) {
85
10.1M
    DEBUGASSERT(!indx);
86
    /* first invoke */
87
10.1M
    if(fit < MIN_FIRST_ALLOC)
88
10.1M
      a = MIN_FIRST_ALLOC;
89
8.98k
    else
90
8.98k
      a = fit;
91
10.1M
  }
92
1.48G
  else {
93
1.49G
    while(a < fit)
94
1.62M
      a *= 2;
95
1.48G
  }
96
97
1.49G
  if(a != s->allc) {
98
    /* this logic is not using Curl_saferealloc() to make the tool not have to
99
       include that as well when it uses this code */
100
11.7M
    void *p = realloc(s->bufr, a);
101
11.7M
    if(!p) {
102
0
      Curl_safefree(s->bufr);
103
0
      s->leng = s->allc = 0;
104
0
      return CURLE_OUT_OF_MEMORY;
105
0
    }
106
11.7M
    s->bufr = p;
107
11.7M
    s->allc = a;
108
11.7M
  }
109
110
1.49G
  if(len)
111
1.49G
    memcpy(&s->bufr[indx], mem, len);
112
1.49G
  s->leng = indx + len;
113
1.49G
  s->bufr[s->leng] = 0;
114
1.49G
  return CURLE_OK;
115
1.49G
}
116
117
/*
118
 * Clears the string, keeps the allocation. This can also be called on a
119
 * buffer that already was freed.
120
 */
121
void Curl_dyn_reset(struct dynbuf *s)
122
454k
{
123
454k
  DEBUGASSERT(s);
124
454k
  DEBUGASSERT(s->init == DYNINIT);
125
454k
  DEBUGASSERT(!s->leng || s->bufr);
126
454k
  if(s->leng)
127
400k
    s->bufr[0] = 0;
128
454k
  s->leng = 0;
129
454k
}
130
131
/*
132
 * Specify the size of the tail to keep (number of bytes from the end of the
133
 * buffer). The rest will be dropped.
134
 */
135
CURLcode Curl_dyn_tail(struct dynbuf *s, size_t trail)
136
18.2k
{
137
18.2k
  DEBUGASSERT(s);
138
18.2k
  DEBUGASSERT(s->init == DYNINIT);
139
18.2k
  DEBUGASSERT(!s->leng || s->bufr);
140
18.2k
  if(trail > s->leng)
141
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
142
18.2k
  else if(trail == s->leng)
143
0
    return CURLE_OK;
144
18.2k
  else if(!trail) {
145
264
    Curl_dyn_reset(s);
146
264
  }
147
17.9k
  else {
148
17.9k
    memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
149
17.9k
    s->leng = trail;
150
17.9k
    s->bufr[s->leng] = 0;
151
17.9k
  }
152
18.2k
  return CURLE_OK;
153
154
18.2k
}
155
156
/*
157
 * Appends a buffer with length.
158
 */
159
CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
160
1.49G
{
161
1.49G
  DEBUGASSERT(s);
162
1.49G
  DEBUGASSERT(s->init == DYNINIT);
163
1.49G
  DEBUGASSERT(!s->leng || s->bufr);
164
1.49G
  return dyn_nappend(s, mem, len);
165
1.49G
}
166
167
/*
168
 * Append a null-terminated string at the end.
169
 */
170
CURLcode Curl_dyn_add(struct dynbuf *s, const char *str)
171
111k
{
172
111k
  size_t n = strlen(str);
173
111k
  DEBUGASSERT(s);
174
111k
  DEBUGASSERT(s->init == DYNINIT);
175
111k
  DEBUGASSERT(!s->leng || s->bufr);
176
111k
  return dyn_nappend(s, (unsigned char *)str, n);
177
111k
}
178
179
/*
180
 * Append a string vprintf()-style
181
 */
182
CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap)
183
151M
{
184
151M
#ifdef BUILDING_LIBCURL
185
151M
  int rc;
186
151M
  DEBUGASSERT(s);
187
151M
  DEBUGASSERT(s->init == DYNINIT);
188
151M
  DEBUGASSERT(!s->leng || s->bufr);
189
151M
  rc = Curl_dyn_vprintf(s, fmt, ap);
190
191
151M
  if(!rc)
192
151M
    return CURLE_OK;
193
#else
194
  char *str;
195
  str = vaprintf(fmt, ap); /* this allocs a new string to append */
196
197
  if(str) {
198
    CURLcode result = dyn_nappend(s, (unsigned char *)str, strlen(str));
199
    free(str);
200
    return result;
201
  }
202
  /* If we failed, we cleanup the whole buffer and return error */
203
  Curl_dyn_free(s);
204
#endif
205
58
  return CURLE_OUT_OF_MEMORY;
206
151M
}
207
208
/*
209
 * Append a string printf()-style
210
 */
211
CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...)
212
151M
{
213
151M
  CURLcode result;
214
151M
  va_list ap;
215
151M
  DEBUGASSERT(s);
216
151M
  DEBUGASSERT(s->init == DYNINIT);
217
151M
  DEBUGASSERT(!s->leng || s->bufr);
218
151M
  va_start(ap, fmt);
219
151M
  result = Curl_dyn_vaddf(s, fmt, ap);
220
151M
  va_end(ap);
221
151M
  return result;
222
151M
}
223
224
/*
225
 * Returns a pointer to the buffer.
226
 */
227
char *Curl_dyn_ptr(const struct dynbuf *s)
228
12.3M
{
229
12.3M
  DEBUGASSERT(s);
230
12.3M
  DEBUGASSERT(s->init == DYNINIT);
231
12.3M
  DEBUGASSERT(!s->leng || s->bufr);
232
12.3M
  return s->bufr;
233
12.3M
}
234
235
/*
236
 * Returns an unsigned pointer to the buffer.
237
 */
238
unsigned char *Curl_dyn_uptr(const struct dynbuf *s)
239
0
{
240
0
  DEBUGASSERT(s);
241
0
  DEBUGASSERT(s->init == DYNINIT);
242
0
  DEBUGASSERT(!s->leng || s->bufr);
243
0
  return (unsigned char *)s->bufr;
244
0
}
245
246
/*
247
 * Returns the length of the buffer.
248
 */
249
size_t Curl_dyn_len(const struct dynbuf *s)
250
11.4M
{
251
11.4M
  DEBUGASSERT(s);
252
11.4M
  DEBUGASSERT(s->init == DYNINIT);
253
11.4M
  DEBUGASSERT(!s->leng || s->bufr);
254
11.4M
  return s->leng;
255
11.4M
}
256
257
/*
258
 * Set a new (smaller) length.
259
 */
260
CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t set)
261
4.24k
{
262
4.24k
  DEBUGASSERT(s);
263
4.24k
  DEBUGASSERT(s->init == DYNINIT);
264
4.24k
  DEBUGASSERT(!s->leng || s->bufr);
265
4.24k
  if(set > s->leng)
266
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
267
4.24k
  s->leng = set;
268
4.24k
  s->bufr[s->leng] = 0;
269
4.24k
  return CURLE_OK;
270
4.24k
}