Coverage Report

Created: 2025-10-10 06:31

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