Coverage Report

Created: 2026-12-01 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nghttp2/lib/nghttp2_buf.c
Line
Count
Source
1
/*
2
 * nghttp2 - HTTP/2 C Library
3
 *
4
 * Copyright (c) 2014 Tatsuhiro Tsujikawa
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
#include "nghttp2_buf.h"
26
27
#include <stdio.h>
28
29
#include "nghttp2_helper.h"
30
#include "nghttp2_debug.h"
31
32
static const uint8_t inibuf;
33
34
14.8k
void nghttp2_buf_init(nghttp2_buf *buf) {
35
14.8k
  *buf = (nghttp2_buf){
36
14.8k
    .begin = (uint8_t *)&inibuf,
37
14.8k
    .end = (uint8_t *)&inibuf,
38
14.8k
    .pos = (uint8_t *)&inibuf,
39
14.8k
    .last = (uint8_t *)&inibuf,
40
14.8k
    .mark = (uint8_t *)&inibuf,
41
14.8k
  };
42
14.8k
}
43
44
4.94k
int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem) {
45
4.94k
  nghttp2_buf_init(buf);
46
4.94k
  return nghttp2_buf_reserve(buf, initial, mem);
47
4.94k
}
48
49
4.94k
void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem) {
50
4.94k
  if (buf == NULL || buf->begin == &inibuf) {
51
0
    return;
52
0
  }
53
54
4.94k
  nghttp2_mem_free(mem, buf->begin);
55
4.94k
  buf->begin = NULL;
56
4.94k
}
57
58
4.94k
int nghttp2_buf_reserve(nghttp2_buf *buf, size_t size, nghttp2_mem *mem) {
59
4.94k
  uint8_t *p;
60
4.94k
  nghttp2_ssize pos_offset, last_offset, mark_offset;
61
62
4.94k
  if ((size_t)(buf->end - buf->begin) >= size) {
63
0
    return 0;
64
0
  }
65
66
4.94k
  pos_offset = buf->pos - buf->begin;
67
4.94k
  last_offset = buf->last - buf->begin;
68
4.94k
  mark_offset = buf->mark - buf->begin;
69
70
4.94k
  p = nghttp2_mem_realloc(mem, buf->begin == &inibuf ? NULL : buf->begin, size);
71
4.94k
  if (!p) {
72
0
    return NGHTTP2_ERR_NOMEM;
73
0
  }
74
75
4.94k
  *buf = (nghttp2_buf){
76
4.94k
    .begin = p,
77
4.94k
    .end = p + size,
78
4.94k
    .pos = p + pos_offset,
79
4.94k
    .last = p + last_offset,
80
4.94k
    .mark = p + mark_offset,
81
4.94k
  };
82
83
4.94k
  return 0;
84
4.94k
}
85
86
4.94k
void nghttp2_buf_reset(nghttp2_buf *buf) {
87
4.94k
  buf->pos = buf->last = buf->mark = buf->begin;
88
4.94k
}
89
90
11.7k
void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len) {
91
11.7k
  buf->begin = buf->pos = buf->last = buf->mark = buf->end = begin;
92
11.7k
  if (len) {
93
11.7k
    buf->end += len;
94
11.7k
  }
95
11.7k
}
96
97
static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length,
98
4.94k
                         nghttp2_mem *mem) {
99
4.94k
  int rv;
100
101
4.94k
  *chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
102
4.94k
  if (*chain == NULL) {
103
0
    return NGHTTP2_ERR_NOMEM;
104
0
  }
105
106
4.94k
  (*chain)->next = NULL;
107
108
4.94k
  rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem);
109
4.94k
  if (rv != 0) {
110
0
    nghttp2_mem_free(mem, *chain);
111
0
    return NGHTTP2_ERR_NOMEM;
112
0
  }
113
114
4.94k
  return 0;
115
4.94k
}
116
117
4.94k
static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) {
118
4.94k
  nghttp2_buf_free(&chain->buf, mem);
119
4.94k
  nghttp2_mem_free(mem, chain);
120
4.94k
}
121
122
int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
123
0
                      nghttp2_mem *mem) {
124
0
  return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem);
125
0
}
126
127
int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
128
4.94k
                       size_t max_chunk, size_t offset, nghttp2_mem *mem) {
129
4.94k
  return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset,
130
4.94k
                            mem);
131
4.94k
}
132
133
int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
134
                       size_t max_chunk, size_t chunk_keep, size_t offset,
135
4.94k
                       nghttp2_mem *mem) {
136
4.94k
  int rv;
137
4.94k
  nghttp2_buf_chain *chain;
138
139
4.94k
  if (chunk_keep == 0 || max_chunk < chunk_keep || chunk_length < offset) {
140
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
141
0
  }
142
143
4.94k
  rv = buf_chain_new(&chain, chunk_length, mem);
144
4.94k
  if (rv != 0) {
145
0
    return rv;
146
0
  }
147
148
4.94k
  bufs->mem = mem;
149
4.94k
  bufs->offset = offset;
150
151
4.94k
  bufs->head = chain;
152
4.94k
  bufs->cur = bufs->head;
153
154
4.94k
  nghttp2_buf_shift_right(&bufs->cur->buf, offset);
155
156
4.94k
  bufs->chunk_length = chunk_length;
157
4.94k
  bufs->chunk_used = 1;
158
4.94k
  bufs->max_chunk = max_chunk;
159
4.94k
  bufs->chunk_keep = chunk_keep;
160
161
4.94k
  return 0;
162
4.94k
}
163
164
0
int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) {
165
0
  int rv;
166
0
  nghttp2_buf_chain *chain;
167
168
0
  if (chunk_length < bufs->offset) {
169
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
170
0
  }
171
172
0
  rv = buf_chain_new(&chain, chunk_length, bufs->mem);
173
0
  if (rv != 0) {
174
0
    return rv;
175
0
  }
176
177
0
  nghttp2_bufs_free(bufs);
178
179
0
  bufs->head = chain;
180
0
  bufs->cur = bufs->head;
181
182
0
  nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
183
184
0
  bufs->chunk_length = chunk_length;
185
0
  bufs->chunk_used = 1;
186
187
0
  return 0;
188
0
}
189
190
4.94k
void nghttp2_bufs_free(nghttp2_bufs *bufs) {
191
4.94k
  nghttp2_buf_chain *chain, *next_chain;
192
193
4.94k
  if (bufs == NULL) {
194
0
    return;
195
0
  }
196
197
9.88k
  for (chain = bufs->head; chain;) {
198
4.94k
    next_chain = chain->next;
199
200
4.94k
    buf_chain_del(chain, bufs->mem);
201
202
4.94k
    chain = next_chain;
203
4.94k
  }
204
205
4.94k
  bufs->head = NULL;
206
4.94k
}
207
208
int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
209
0
                           nghttp2_mem *mem) {
210
0
  nghttp2_buf_chain *chain;
211
212
0
  chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
213
0
  if (chain == NULL) {
214
0
    return NGHTTP2_ERR_NOMEM;
215
0
  }
216
217
0
  chain->next = NULL;
218
219
0
  if (len == 0) {
220
0
    nghttp2_buf_init(&chain->buf);
221
0
  } else {
222
0
    nghttp2_buf_wrap_init(&chain->buf, begin, len);
223
0
  }
224
225
0
  *bufs = (nghttp2_bufs){
226
0
    .head = chain,
227
0
    .cur = chain,
228
0
    .mem = mem,
229
0
    .chunk_length = len,
230
0
    .max_chunk = 1,
231
0
    .chunk_used = 1,
232
0
    .chunk_keep = 1,
233
0
  };
234
235
0
  return 0;
236
0
}
237
238
int nghttp2_bufs_wrap_init2(nghttp2_bufs *bufs, const nghttp2_vec *vec,
239
0
                            size_t veclen, nghttp2_mem *mem) {
240
0
  size_t i = 0;
241
0
  nghttp2_buf_chain *cur_chain;
242
0
  nghttp2_buf_chain *head_chain;
243
0
  nghttp2_buf_chain **dst_chain = &head_chain;
244
245
0
  if (veclen == 0) {
246
0
    return nghttp2_bufs_wrap_init(bufs, NULL, 0, mem);
247
0
  }
248
249
0
  head_chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain) * veclen);
250
0
  if (head_chain == NULL) {
251
0
    return NGHTTP2_ERR_NOMEM;
252
0
  }
253
254
0
  for (i = 0; i < veclen; ++i) {
255
0
    cur_chain = &head_chain[i];
256
0
    cur_chain->next = NULL;
257
0
    if (vec[i].len == 0) {
258
0
      nghttp2_buf_init(&cur_chain->buf);
259
0
    } else {
260
0
      nghttp2_buf_wrap_init(&cur_chain->buf, vec[i].base, vec[i].len);
261
0
    }
262
263
0
    *dst_chain = cur_chain;
264
0
    dst_chain = &cur_chain->next;
265
0
  }
266
267
0
  *bufs = (nghttp2_bufs){
268
0
    .head = head_chain,
269
0
    .cur = head_chain,
270
0
    .mem = mem,
271
    /* We don't use chunk_length since no allocation is expected. */
272
0
    .max_chunk = veclen,
273
0
    .chunk_used = veclen,
274
0
    .chunk_keep = veclen,
275
0
  };
276
277
0
  return 0;
278
0
}
279
280
0
void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) {
281
0
  if (bufs == NULL) {
282
0
    return;
283
0
  }
284
285
0
  if (bufs->head) {
286
0
    nghttp2_mem_free(bufs->mem, bufs->head);
287
0
  }
288
0
}
289
290
0
void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) {
291
0
  nghttp2_buf_chain *ci;
292
293
0
  for (ci = bufs->cur; ci; ci = ci->next) {
294
0
    if (nghttp2_buf_len(&ci->buf) == 0) {
295
0
      return;
296
0
    } else {
297
0
      bufs->cur = ci;
298
0
    }
299
0
  }
300
0
}
301
302
4.94k
size_t nghttp2_bufs_len(nghttp2_bufs *bufs) {
303
4.94k
  nghttp2_buf_chain *ci;
304
4.94k
  size_t len;
305
306
4.94k
  len = 0;
307
9.88k
  for (ci = bufs->head; ci; ci = ci->next) {
308
4.94k
    len += nghttp2_buf_len(&ci->buf);
309
4.94k
  }
310
311
4.94k
  return len;
312
4.94k
}
313
314
0
static int bufs_alloc_chain(nghttp2_bufs *bufs) {
315
0
  int rv;
316
0
  nghttp2_buf_chain *chain;
317
318
0
  if (bufs->cur->next) {
319
0
    bufs->cur = bufs->cur->next;
320
321
0
    return 0;
322
0
  }
323
324
0
  if (bufs->max_chunk == bufs->chunk_used) {
325
0
    return NGHTTP2_ERR_BUFFER_ERROR;
326
0
  }
327
328
0
  rv = buf_chain_new(&chain, bufs->chunk_length, bufs->mem);
329
0
  if (rv != 0) {
330
0
    return rv;
331
0
  }
332
333
0
  DEBUGF("new buffer %zu bytes allocated for bufs %p, used %zu\n",
334
0
         bufs->chunk_length, bufs, bufs->chunk_used);
335
336
0
  ++bufs->chunk_used;
337
338
0
  bufs->cur->next = chain;
339
0
  bufs->cur = chain;
340
341
0
  nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
342
343
0
  return 0;
344
0
}
345
346
60.6k
int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len) {
347
60.6k
  int rv;
348
60.6k
  size_t nwrite;
349
60.6k
  nghttp2_buf *buf;
350
60.6k
  const uint8_t *p;
351
352
60.6k
  p = data;
353
354
108k
  while (len) {
355
48.1k
    buf = &bufs->cur->buf;
356
357
48.1k
    nwrite = nghttp2_min_size(nghttp2_buf_avail(buf), len);
358
48.1k
    if (nwrite == 0) {
359
0
      rv = bufs_alloc_chain(bufs);
360
0
      if (rv != 0) {
361
0
        return rv;
362
0
      }
363
0
      continue;
364
0
    }
365
366
48.1k
    buf->last = nghttp2_cpymem(buf->last, p, nwrite);
367
48.1k
    p += nwrite;
368
48.1k
    len -= nwrite;
369
48.1k
  }
370
371
60.6k
  return 0;
372
60.6k
}
373
374
11.5k
static int bufs_ensure_addb(nghttp2_bufs *bufs) {
375
11.5k
  int rv;
376
377
  /* The next chunk might have no space either if bufs wraps
378
     zero-length buffers (see nghttp2_bufs_wrap_init2). */
379
11.5k
  while (nghttp2_buf_avail(&bufs->cur->buf) == 0) {
380
0
    rv = bufs_alloc_chain(bufs);
381
0
    if (rv != 0) {
382
0
      return rv;
383
0
    }
384
0
  }
385
386
11.5k
  return 0;
387
11.5k
}
388
389
11.5k
int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b) {
390
11.5k
  int rv;
391
392
11.5k
  rv = bufs_ensure_addb(bufs);
393
11.5k
  if (rv != 0) {
394
0
    return rv;
395
0
  }
396
397
11.5k
  *bufs->cur->buf.last++ = b;
398
399
11.5k
  return 0;
400
11.5k
}
401
402
0
int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b) {
403
0
  int rv;
404
405
0
  rv = bufs_ensure_addb(bufs);
406
0
  if (rv != 0) {
407
0
    return rv;
408
0
  }
409
410
0
  *bufs->cur->buf.last = b;
411
412
0
  return 0;
413
0
}
414
415
0
int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b) {
416
0
  int rv;
417
418
0
  rv = bufs_ensure_addb(bufs);
419
0
  if (rv != 0) {
420
0
    return rv;
421
0
  }
422
423
0
  *bufs->cur->buf.last++ |= b;
424
425
0
  return 0;
426
0
}
427
428
0
int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b) {
429
0
  int rv;
430
431
0
  rv = bufs_ensure_addb(bufs);
432
0
  if (rv != 0) {
433
0
    return rv;
434
0
  }
435
436
0
  *bufs->cur->buf.last |= b;
437
438
0
  return 0;
439
0
}
440
441
0
nghttp2_ssize nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) {
442
0
  size_t len;
443
0
  nghttp2_buf_chain *chain;
444
0
  nghttp2_buf *buf;
445
0
  uint8_t *res;
446
0
  nghttp2_buf resbuf;
447
448
0
  len = 0;
449
450
0
  for (chain = bufs->head; chain; chain = chain->next) {
451
0
    len += nghttp2_buf_len(&chain->buf);
452
0
  }
453
454
0
  if (len == 0) {
455
0
    res = NULL;
456
0
    return 0;
457
0
  }
458
459
0
  res = nghttp2_mem_malloc(bufs->mem, len);
460
0
  if (res == NULL) {
461
0
    return NGHTTP2_ERR_NOMEM;
462
0
  }
463
464
0
  nghttp2_buf_wrap_init(&resbuf, res, len);
465
466
0
  for (chain = bufs->head; chain; chain = chain->next) {
467
0
    buf = &chain->buf;
468
0
    resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
469
0
  }
470
471
0
  *out = res;
472
473
0
  return (nghttp2_ssize)len;
474
0
}
475
476
0
size_t nghttp2_bufs_remove_copy(nghttp2_bufs *bufs, uint8_t *out) {
477
0
  size_t len;
478
0
  nghttp2_buf_chain *chain;
479
0
  nghttp2_buf *buf;
480
0
  nghttp2_buf resbuf;
481
482
0
  len = nghttp2_bufs_len(bufs);
483
484
0
  nghttp2_buf_wrap_init(&resbuf, out, len);
485
486
0
  for (chain = bufs->head; chain; chain = chain->next) {
487
0
    buf = &chain->buf;
488
0
    resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
489
0
  }
490
491
0
  return len;
492
0
}
493
494
4.94k
void nghttp2_bufs_reset(nghttp2_bufs *bufs) {
495
4.94k
  nghttp2_buf_chain *chain, *ci;
496
4.94k
  size_t k;
497
498
4.94k
  k = bufs->chunk_keep;
499
500
9.88k
  for (ci = bufs->head; ci; ci = ci->next) {
501
4.94k
    nghttp2_buf_reset(&ci->buf);
502
4.94k
    nghttp2_buf_shift_right(&ci->buf, bufs->offset);
503
504
4.94k
    if (--k == 0) {
505
0
      break;
506
0
    }
507
4.94k
  }
508
509
4.94k
  if (ci) {
510
0
    chain = ci->next;
511
0
    ci->next = NULL;
512
513
0
    for (ci = chain; ci;) {
514
0
      chain = ci->next;
515
516
0
      buf_chain_del(ci, bufs->mem);
517
518
0
      ci = chain;
519
0
    }
520
521
0
    bufs->chunk_used = bufs->chunk_keep;
522
0
  }
523
524
4.94k
  bufs->cur = bufs->head;
525
4.94k
}
526
527
0
int nghttp2_bufs_advance(nghttp2_bufs *bufs) { return bufs_alloc_chain(bufs); }
528
529
0
int nghttp2_bufs_next_present(nghttp2_bufs *bufs) {
530
0
  nghttp2_buf_chain *chain;
531
532
0
  chain = bufs->cur->next;
533
534
0
  return chain && nghttp2_buf_len(&chain->buf);
535
0
}