Coverage Report

Created: 2026-12-01 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/bufq.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 "bufq.h"
27
28
static bool chunk_is_empty(const struct buf_chunk *chunk)
29
10.4M
{
30
10.4M
  return chunk->r_offset >= chunk->w_offset;
31
10.4M
}
32
33
static bool chunk_is_full(const struct buf_chunk *chunk)
34
4.79M
{
35
4.79M
  return chunk->w_offset >= chunk->dlen;
36
4.79M
}
37
38
static size_t chunk_len(const struct buf_chunk *chunk)
39
30.6M
{
40
30.6M
  return chunk->w_offset - chunk->r_offset;
41
30.6M
}
42
43
static void chunk_reset(struct buf_chunk *chunk)
44
2.24M
{
45
2.24M
  chunk->next = NULL;
46
2.24M
  chunk->r_offset = chunk->w_offset = 0;
47
2.24M
}
48
49
static size_t chunk_append(struct buf_chunk *chunk,
50
                           const uint8_t *buf, size_t len)
51
837k
{
52
837k
  uint8_t *p = &chunk->x.data[chunk->w_offset];
53
837k
  size_t n = chunk->dlen - chunk->w_offset;
54
837k
  DEBUGASSERT(chunk->dlen >= chunk->w_offset);
55
837k
  if(n) {
56
837k
    n = CURLMIN(n, len);
57
837k
    memcpy(p, buf, n);
58
837k
    chunk->w_offset += n;
59
837k
  }
60
837k
  return n;
61
837k
}
62
63
static size_t chunk_read(struct buf_chunk *chunk,
64
                         uint8_t *buf, size_t len)
65
488k
{
66
488k
  uint8_t *p = &chunk->x.data[chunk->r_offset];
67
488k
  size_t n = chunk->w_offset - chunk->r_offset;
68
488k
  DEBUGASSERT(chunk->w_offset >= chunk->r_offset);
69
488k
  if(!n) {
70
474
    return 0;
71
474
  }
72
488k
  else if(n <= len) {
73
464k
    memcpy(buf, p, n);
74
464k
    chunk->r_offset = chunk->w_offset = 0;
75
464k
    return n;
76
464k
  }
77
23.2k
  else {
78
23.2k
    memcpy(buf, p, len);
79
23.2k
    chunk->r_offset += len;
80
23.2k
    return len;
81
23.2k
  }
82
488k
}
83
84
static CURLcode chunk_slurpn(struct buf_chunk *chunk, size_t max_len,
85
                             Curl_bufq_reader *reader,
86
                             void *reader_ctx, size_t *pnread)
87
1.31M
{
88
1.31M
  uint8_t *p = &chunk->x.data[chunk->w_offset];
89
1.31M
  size_t n = chunk->dlen - chunk->w_offset; /* free amount */
90
1.31M
  CURLcode result;
91
92
1.31M
  *pnread = 0;
93
1.31M
  DEBUGASSERT(chunk->dlen >= chunk->w_offset);
94
1.31M
  if(!n)
95
0
    return CURLE_AGAIN;
96
1.31M
  if(max_len && n > max_len)
97
48.8k
    n = max_len;
98
1.31M
  result = reader(reader_ctx, p, n, pnread);
99
1.31M
  if(!result) {
100
1.30M
    DEBUGASSERT(*pnread <= n);
101
1.30M
    chunk->w_offset += *pnread;
102
1.30M
  }
103
1.31M
  return result;
104
1.31M
}
105
106
static void chunk_peek(const struct buf_chunk *chunk,
107
                       const uint8_t **pbuf, size_t *plen)
108
1.06M
{
109
1.06M
  DEBUGASSERT(chunk->w_offset >= chunk->r_offset);
110
1.06M
  *pbuf = &chunk->x.data[chunk->r_offset];
111
1.06M
  *plen = chunk->w_offset - chunk->r_offset;
112
1.06M
}
113
114
static void chunk_peek_at(const struct buf_chunk *chunk, size_t offset,
115
                          const uint8_t **pbuf, size_t *plen)
116
137k
{
117
137k
  offset += chunk->r_offset;
118
137k
  DEBUGASSERT(chunk->w_offset >= offset);
119
137k
  *pbuf = &chunk->x.data[offset];
120
137k
  *plen = chunk->w_offset - offset;
121
137k
}
122
123
static size_t chunk_skip(struct buf_chunk *chunk, size_t amount)
124
896k
{
125
896k
  size_t n = chunk->w_offset - chunk->r_offset;
126
896k
  DEBUGASSERT(chunk->w_offset >= chunk->r_offset);
127
896k
  if(n) {
128
895k
    n = CURLMIN(n, amount);
129
895k
    chunk->r_offset += n;
130
895k
    if(chunk->r_offset == chunk->w_offset)
131
795k
      chunk->r_offset = chunk->w_offset = 0;
132
895k
  }
133
896k
  return n;
134
896k
}
135
136
static void chunk_list_free(struct buf_chunk **anchor)
137
2.79k
{
138
2.79k
  struct buf_chunk *chunk;
139
8.22k
  while(*anchor) {
140
5.43k
    chunk = *anchor;
141
5.43k
    *anchor = chunk->next;
142
5.43k
    curlx_free(chunk);
143
5.43k
  }
144
2.79k
}
145
146
void Curl_bufcp_init(struct bufc_pool *pool,
147
                     size_t chunk_size, size_t spare_max)
148
516
{
149
516
  DEBUGASSERT(chunk_size > 0);
150
516
  DEBUGASSERT(spare_max > 0);
151
516
  memset(pool, 0, sizeof(*pool));
152
516
  pool->chunk_size = chunk_size;
153
516
  pool->spare_max = spare_max;
154
516
}
155
156
static CURLcode bufcp_take(struct bufc_pool *pool,
157
                           struct buf_chunk **pchunk)
158
535k
{
159
535k
  struct buf_chunk *chunk = NULL;
160
161
535k
  if(pool->spare) {
162
371k
    chunk = pool->spare;
163
371k
    pool->spare = chunk->next;
164
371k
    --pool->spare_count;
165
371k
    chunk_reset(chunk);
166
371k
    *pchunk = chunk;
167
371k
    return CURLE_OK;
168
371k
  }
169
170
  /* Check for integer overflow before allocation */
171
163k
  if(pool->chunk_size > SIZE_MAX - sizeof(*chunk)) {
172
0
    *pchunk = NULL;
173
0
    return CURLE_OUT_OF_MEMORY;
174
0
  }
175
176
163k
  chunk = curlx_calloc(1, sizeof(*chunk) + pool->chunk_size);
177
163k
  if(!chunk) {
178
0
    *pchunk = NULL;
179
0
    return CURLE_OUT_OF_MEMORY;
180
0
  }
181
163k
  chunk->dlen = pool->chunk_size;
182
163k
  *pchunk = chunk;
183
163k
  return CURLE_OK;
184
163k
}
185
186
static void bufcp_put(struct bufc_pool *pool,
187
                      struct buf_chunk *chunk)
188
532k
{
189
532k
  if(pool->spare_count >= pool->spare_max) {
190
161k
    curlx_free(chunk);
191
161k
  }
192
371k
  else {
193
371k
    chunk_reset(chunk);
194
371k
    chunk->next = pool->spare;
195
371k
    pool->spare = chunk;
196
371k
    ++pool->spare_count;
197
371k
  }
198
532k
}
199
200
void Curl_bufcp_free(struct bufc_pool *pool)
201
516
{
202
516
  chunk_list_free(&pool->spare);
203
516
  pool->spare_count = 0;
204
516
}
205
206
static void bufq_init(struct bufq *q, struct bufc_pool *pool,
207
                      size_t chunk_size, size_t max_chunks, int opts)
208
1.13k
{
209
1.13k
  DEBUGASSERT(chunk_size > 0);
210
1.13k
  DEBUGASSERT(max_chunks > 0);
211
1.13k
  memset(q, 0, sizeof(*q));
212
1.13k
  q->chunk_size = chunk_size;
213
1.13k
  q->max_chunks = max_chunks;
214
1.13k
  q->pool = pool;
215
1.13k
  q->opts = opts;
216
1.13k
}
217
218
void Curl_bufq_init2(struct bufq *q, size_t chunk_size, size_t max_chunks,
219
                     int opts)
220
0
{
221
0
  bufq_init(q, NULL, chunk_size, max_chunks, opts);
222
0
}
223
224
void Curl_bufq_init(struct bufq *q, size_t chunk_size, size_t max_chunks)
225
621
{
226
621
  bufq_init(q, NULL, chunk_size, max_chunks, BUFQ_OPT_NONE);
227
621
}
228
229
void Curl_bufq_initp(struct bufq *q, struct bufc_pool *pool,
230
                     size_t max_chunks, int opts)
231
516
{
232
516
  bufq_init(q, pool, pool->chunk_size, max_chunks, opts);
233
516
}
234
235
void Curl_bufq_free(struct bufq *q)
236
1.13k
{
237
1.13k
  chunk_list_free(&q->head);
238
1.13k
  chunk_list_free(&q->spare);
239
1.13k
  q->tail = NULL;
240
1.13k
  q->chunk_count = 0;
241
1.13k
}
242
243
void Curl_bufq_reset(struct bufq *q)
244
3.33M
{
245
3.33M
  struct buf_chunk *chunk;
246
4.11M
  while(q->head) {
247
774k
    chunk = q->head;
248
774k
    q->head = chunk->next;
249
774k
    chunk->next = q->spare;
250
774k
    q->spare = chunk;
251
774k
  }
252
3.33M
  q->tail = NULL;
253
3.33M
}
254
255
size_t Curl_bufq_len(const struct bufq *q)
256
14.7M
{
257
14.7M
  const struct buf_chunk *chunk = q->head;
258
14.7M
  size_t len = 0;
259
44.1M
  while(chunk) {
260
29.4M
    len += chunk_len(chunk);
261
29.4M
    chunk = chunk->next;
262
29.4M
  }
263
14.7M
  return len;
264
14.7M
}
265
266
bool Curl_bufq_is_empty(const struct bufq *q)
267
14.7M
{
268
14.7M
  return !q->head || chunk_is_empty(q->head);
269
14.7M
}
270
271
bool Curl_bufq_is_full(const struct bufq *q)
272
0
{
273
0
  if(!q->tail || q->spare)
274
0
    return FALSE;
275
0
  if(q->chunk_count < q->max_chunks)
276
0
    return FALSE;
277
0
  if(q->chunk_count > q->max_chunks)
278
0
    return TRUE;
279
  /* we have no spares and cannot make more, is the tail full? */
280
0
  return chunk_is_full(q->tail);
281
0
}
282
283
static struct buf_chunk *get_spare(struct bufq *q)
284
4.18M
{
285
4.18M
  struct buf_chunk *chunk = NULL;
286
287
4.18M
  if(q->spare) {
288
1.50M
    chunk = q->spare;
289
1.50M
    q->spare = chunk->next;
290
1.50M
    chunk_reset(chunk);
291
1.50M
    return chunk;
292
1.50M
  }
293
294
2.67M
  if(q->chunk_count >= q->max_chunks && (!(q->opts & BUFQ_OPT_SOFT_LIMIT)))
295
2.13M
    return NULL;
296
297
537k
  if(q->pool) {
298
535k
    if(bufcp_take(q->pool, &chunk))
299
0
      return NULL;
300
535k
    ++q->chunk_count;
301
535k
    return chunk;
302
535k
  }
303
2.83k
  else {
304
    /* Check for integer overflow before allocation */
305
2.83k
    if(q->chunk_size > SIZE_MAX - sizeof(*chunk)) {
306
0
      return NULL;
307
0
    }
308
309
2.83k
    chunk = curlx_calloc(1, sizeof(*chunk) + q->chunk_size);
310
2.83k
    if(!chunk)
311
0
      return NULL;
312
2.83k
    chunk->dlen = q->chunk_size;
313
2.83k
    ++q->chunk_count;
314
2.83k
    return chunk;
315
2.83k
  }
316
537k
}
317
318
static void prune_head(struct bufq *q)
319
1.38M
{
320
1.38M
  struct buf_chunk *chunk;
321
322
2.65M
  while(q->head && chunk_is_empty(q->head)) {
323
1.26M
    chunk = q->head;
324
1.26M
    q->head = chunk->next;
325
1.26M
    if(q->tail == chunk)
326
267k
      q->tail = q->head;
327
1.26M
    if(q->pool) {
328
532k
      bufcp_put(q->pool, chunk);
329
532k
      --q->chunk_count;
330
532k
    }
331
732k
    else if((q->chunk_count > q->max_chunks) ||
332
732k
            (q->opts & BUFQ_OPT_NO_SPARES)) {
333
      /* SOFT_LIMIT allowed us more than max. free spares until
334
       * we are at max again. Or free them if we are configured
335
       * to not use spares. */
336
0
      curlx_free(chunk);
337
0
      --q->chunk_count;
338
0
    }
339
732k
    else {
340
732k
      chunk->next = q->spare;
341
732k
      q->spare = chunk;
342
732k
    }
343
1.26M
  }
344
1.38M
}
345
346
static struct buf_chunk *get_non_full_tail(struct bufq *q)
347
4.29M
{
348
4.29M
  struct buf_chunk *chunk;
349
350
4.29M
  if(q->tail && !chunk_is_full(q->tail))
351
110k
    return q->tail;
352
4.18M
  chunk = get_spare(q);
353
4.18M
  if(chunk) {
354
    /* new tail, and possibly new head */
355
2.04M
    if(q->tail) {
356
1.51M
      q->tail->next = chunk;
357
1.51M
      q->tail = chunk;
358
1.51M
    }
359
524k
    else {
360
524k
      DEBUGASSERT(!q->head);
361
524k
      q->head = q->tail = chunk;
362
524k
    }
363
2.04M
  }
364
4.18M
  return chunk;
365
4.18M
}
366
367
CURLcode Curl_bufq_write(struct bufq *q,
368
                         const uint8_t *buf, size_t len,
369
                         size_t *pnwritten)
370
6.57M
{
371
6.57M
  struct buf_chunk *tail;
372
6.57M
  size_t n;
373
374
6.57M
  DEBUGASSERT(q->max_chunks > 0);
375
6.57M
  *pnwritten = 0;
376
7.40M
  while(len) {
377
1.90M
    tail = get_non_full_tail(q);
378
1.90M
    if(!tail) {
379
1.06M
      if((q->chunk_count < q->max_chunks) || (q->opts & BUFQ_OPT_SOFT_LIMIT))
380
        /* should have gotten a tail, but did not */
381
0
        return CURLE_OUT_OF_MEMORY;
382
1.06M
      break;
383
1.06M
    }
384
837k
    n = chunk_append(tail, buf, len);
385
837k
    if(!n)
386
0
      break;
387
837k
    *pnwritten += n;
388
837k
    buf += n;
389
837k
    len -= n;
390
837k
  }
391
6.57M
  return (!*pnwritten && len) ? CURLE_AGAIN : CURLE_OK;
392
6.57M
}
393
394
CURLcode Curl_bufq_cwrite(struct bufq *q,
395
                          const char *buf, size_t len,
396
                          size_t *pnwritten)
397
0
{
398
0
  return Curl_bufq_write(q, (const uint8_t *)buf, len, pnwritten);
399
0
}
400
401
CURLcode Curl_bufq_read(struct bufq *q, uint8_t *buf, size_t len,
402
                        size_t *pnread)
403
428k
{
404
428k
  *pnread = 0;
405
917k
  while(len && q->head) {
406
488k
    size_t n = chunk_read(q->head, buf, len);
407
488k
    if(n) {
408
488k
      *pnread += n;
409
488k
      buf += n;
410
488k
      len -= n;
411
488k
    }
412
488k
    prune_head(q);
413
488k
  }
414
428k
  return (!*pnread) ? CURLE_AGAIN : CURLE_OK;
415
428k
}
416
417
CURLcode Curl_bufq_cread(struct bufq *q, char *buf, size_t len,
418
                         size_t *pnread)
419
0
{
420
0
  return Curl_bufq_read(q, (uint8_t *)buf, len, pnread);
421
0
}
422
423
bool Curl_bufq_peek(struct bufq *q,
424
                    const uint8_t **pbuf, size_t *plen)
425
2.00M
{
426
2.00M
  if(q->head && chunk_is_empty(q->head)) {
427
3.72k
    prune_head(q);
428
3.72k
  }
429
2.00M
  if(q->head && !chunk_is_empty(q->head)) {
430
1.06M
    chunk_peek(q->head, pbuf, plen);
431
1.06M
    return TRUE;
432
1.06M
  }
433
938k
  *pbuf = NULL;
434
938k
  *plen = 0;
435
938k
  return FALSE;
436
2.00M
}
437
438
bool Curl_bufq_peek_at(struct bufq *q, size_t offset,
439
                       const uint8_t **pbuf, size_t *plen)
440
721k
{
441
721k
  struct buf_chunk *c = q->head;
442
721k
  size_t clen;
443
444
1.77M
  while(c) {
445
1.19M
    clen = chunk_len(c);
446
1.19M
    if(!clen)
447
534
      break;
448
1.19M
    if(offset >= clen) {
449
1.05M
      offset -= clen;
450
1.05M
      c = c->next;
451
1.05M
      continue;
452
1.05M
    }
453
137k
    chunk_peek_at(c, offset, pbuf, plen);
454
137k
    return TRUE;
455
1.19M
  }
456
583k
  *pbuf = NULL;
457
583k
  *plen = 0;
458
583k
  return FALSE;
459
721k
}
460
461
void Curl_bufq_skip(struct bufq *q, size_t amount)
462
1.06M
{
463
1.06M
  size_t n;
464
465
1.95M
  while(amount && q->head) {
466
896k
    n = chunk_skip(q->head, amount);
467
896k
    amount -= n;
468
896k
    prune_head(q);
469
896k
  }
470
1.06M
}
471
472
CURLcode Curl_bufq_pass(struct bufq *q, Curl_bufq_writer *writer,
473
                        void *writer_ctx, size_t *pwritten)
474
371k
{
475
371k
  const uint8_t *buf;
476
371k
  size_t blen;
477
371k
  CURLcode result = CURLE_OK;
478
479
371k
  *pwritten = 0;
480
788k
  while(Curl_bufq_peek(q, &buf, &blen)) {
481
478k
    size_t chunk_written;
482
483
478k
    result = writer(writer_ctx, buf, blen, &chunk_written);
484
478k
    if(result) {
485
62.1k
      if((result == CURLE_AGAIN) && *pwritten) {
486
        /* blocked on subsequent write, report success */
487
59.8k
        result = CURLE_OK;
488
59.8k
      }
489
62.1k
      break;
490
62.1k
    }
491
416k
    if(!chunk_written) {
492
0
      if(!*pwritten) {
493
        /* treat as blocked */
494
0
        result = CURLE_AGAIN;
495
0
      }
496
0
      break;
497
0
    }
498
416k
    *pwritten += chunk_written;
499
416k
    Curl_bufq_skip(q, chunk_written);
500
416k
  }
501
371k
  return result;
502
371k
}
503
504
CURLcode Curl_bufq_write_pass(struct bufq *q,
505
                              const uint8_t *buf, size_t len,
506
                              Curl_bufq_writer *writer, void *writer_ctx,
507
                              size_t *pwritten)
508
0
{
509
0
  CURLcode result = CURLE_OK;
510
0
  size_t n;
511
512
0
  *pwritten = 0;
513
0
  while(len) {
514
0
    if(Curl_bufq_is_full(q)) {
515
      /* try to make room in case we are full */
516
0
      result = Curl_bufq_pass(q, writer, writer_ctx, &n);
517
0
      if(result) {
518
0
        if(result != CURLE_AGAIN) {
519
          /* real error, fail */
520
0
          return result;
521
0
        }
522
        /* would block, bufq is full, give up */
523
0
        break;
524
0
      }
525
0
    }
526
527
    /* Add to bufq as much as there is room for */
528
0
    result = Curl_bufq_write(q, buf, len, &n);
529
0
    if(result) {
530
0
      if(result != CURLE_AGAIN)
531
        /* real error, fail */
532
0
        return result;
533
      /* result == CURLE_AGAIN */
534
0
      if(*pwritten)
535
        /* we did write successfully before */
536
0
        result = CURLE_OK;
537
0
      return result;
538
0
    }
539
0
    else if(n == 0)
540
      /* edge case of writer returning 0 (and len is >0)
541
       * break or we might enter an infinite loop here */
542
0
      break;
543
544
    /* Track what we added to bufq */
545
0
    buf += n;
546
0
    len -= n;
547
0
    *pwritten += n;
548
0
  }
549
550
0
  return (!*pwritten && len) ? CURLE_AGAIN : CURLE_OK;
551
0
}
552
553
CURLcode Curl_bufq_sipn(struct bufq *q, size_t max_len,
554
                        Curl_bufq_reader *reader, void *reader_ctx,
555
                        size_t *pnread)
556
2.38M
{
557
2.38M
  struct buf_chunk *tail = NULL;
558
559
2.38M
  *pnread = 0;
560
2.38M
  tail = get_non_full_tail(q);
561
2.38M
  if(!tail) {
562
1.06M
    if(q->chunk_count < q->max_chunks)
563
0
      return CURLE_OUT_OF_MEMORY;
564
    /* full, blocked */
565
1.06M
    return CURLE_AGAIN;
566
1.06M
  }
567
568
1.31M
  return chunk_slurpn(tail, max_len, reader, reader_ctx, pnread);
569
2.38M
}
570
571
/**
572
 * Read up to `max_len` bytes and append it to the end of the buffer queue.
573
 * if `max_len` is 0, no limit is imposed and the call behaves exactly
574
 * the same as `Curl_bufq_slurp()`.
575
 * Returns the total amount of buf read (may be 0) in `pnread` or error
576
 * Note that even in case of an error chunks may have been read and
577
 * the buffer queue will have different length than before.
578
 */
579
static CURLcode bufq_slurpn(struct bufq *q, size_t max_len,
580
                            Curl_bufq_reader *reader, void *reader_ctx,
581
                            size_t *pnread)
582
693k
{
583
693k
  CURLcode result;
584
585
693k
  *pnread = 0;
586
1.65M
  while(1) {
587
1.65M
    size_t n;
588
1.65M
    result = Curl_bufq_sipn(q, max_len, reader, reader_ctx, &n);
589
1.65M
    if(result) {
590
623k
      if(!*pnread || result != CURLE_AGAIN) {
591
        /* blocked on first read or real error, fail */
592
515k
        return result;
593
515k
      }
594
107k
      result = CURLE_OK;
595
107k
      break;
596
623k
    }
597
1.03M
    else if(n == 0) {
598
      /* eof, result remains CURLE_OK */
599
0
      break;
600
0
    }
601
1.03M
    *pnread += n;
602
1.03M
    if(max_len) {
603
0
      DEBUGASSERT(n <= max_len);
604
0
      max_len -= n;
605
0
      if(!max_len)
606
0
        break;
607
0
    }
608
    /* give up slurping when we get less bytes than we asked for */
609
1.03M
    if(q->tail && !chunk_is_full(q->tail))
610
69.7k
      break;
611
1.03M
  }
612
177k
  return result;
613
693k
}
614
615
CURLcode Curl_bufq_slurp(struct bufq *q, Curl_bufq_reader *reader,
616
                         void *reader_ctx, size_t *pnread)
617
693k
{
618
693k
  return bufq_slurpn(q, 0, reader, reader_ctx, pnread);
619
693k
}