Coverage Report

Created: 2023-03-26 06:11

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