include/hyperman/hm_parse.h

filter: Statementstmt 97.5% 119 / 124 (2 uncoverable) Branchbran 78.7% 160 / 212 (16 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.7% 279 / 336 (18 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_compress.h ↑ Summary include/hyperman/hm_proxy.h »
1 #ifndef HM_PARSE_H
2 #define HM_PARSE_H
3  
4 /* Pure-C HTTP/1 request-framing helpers, with no Perl dependency, so they can
5 * be unit-tested and fuzzed standalone (see tools/fuzz/). Included by
6 * hm_core.h; the SV/HV-building parser (hm_build_env) stays there.
7 *
8 * These decide how a request body is framed and decode chunked bodies, and are
9 * the security-critical, attacker-controlled path - hardened against request
10 * smuggling (RFC 7230 3.3.3) and fuzzed for memory safety. */
11  
12 #include <stddef.h>
13 #include <string.h>
14 #include <limits.h> /* LONG_MAX, ULLONG_MAX */
15  
16 /* Case-insensitive compare. POSIX puts it in <strings.h>; MSVC has no such
17 * header at all and MinGW's is a thin inline over the CRT's _strnicmp, so we
18 * name the CRT call directly there. Prefixed rather than #defined over the
19 * system name: perl.h and the platform headers are already in this
20 * translation unit, and redefining an ambient symbol they may have declared
21 * is the mistake Eshu spent three releases undoing. This file stays
22 * dependency-free (tools/fuzz/ compiles it alone), so the shim lives here
23 * and not in hm_win.h. */
24 /* ASCII-only, and deliberately not the C library's.
25 *
26 * strncasecmp folds according to the CURRENT LOCALE, and every caller here is
27 * comparing an HTTP header name or token - which RFC 9110 defines as ASCII.
28 * Under a Turkish locale the library folds 'I' to the dotless form, so
29 * "CONTENT-LENGTH" stops matching "Content-Length" and the framing header
30 * goes unrecognised. A server's parse must not depend on the operator's
31 * LC_CTYPE.
32 *
33 * It is also faster: on glibc and on Darwin the call lands in strncasecmp_l,
34 * which resolves the thread's locale before it compares a byte. This is
35 * ~0.3% of the request path, so the reason to prefer it is the first
36 * paragraph and the speed is a bonus. */
37 765 static int hm_strncasecmp(const char *a, const char *b, size_t n) {
38 size_t i;
39 8285 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7581 704 8285
40 7581 unsigned char ca = (unsigned char)a[i], cb = (unsigned char)b[i];
41 7581 ▶ if (ca >= 'A' && ca <= 'Z') ca |= 32;
Branch: 2 branches
BranchTrueFalseTotal
if (ca >= 'A' && ca <= 'Z') ca |= 32; 7088 493 7581
if (ca >= 'A' && ca <= 'Z') ca |= 32; 910 6178 7088
42 7581 ▶ if (cb >= 'A' && cb <= 'Z') cb |= 32;
Branch: 2 branches
BranchTrueFalseTotal
if (cb >= 'A' && cb <= 'Z') cb |= 32; 7087 494 7581
if (cb >= 'A' && cb <= 'Z') cb |= 32; 844 6243 7087
43 7581 ▶ if (ca != cb) return (int)ca - (int)cb;
Branch: 1 branch
BranchTrueFalseTotal
if (ca != cb) return (int)ca - (int)cb; 61 7520 7581
44 /*
45 # uncoverable branch true
46 # uncoverable condition left count:1 class:ignore_covered_err */
47 7520 ▶ if (!ca) break; /* both NUL: equal within n */
Branch: 1 branch
BranchTrueFalseTotal
if (!ca) break; /* both NUL: equal within n */ 0 7520 7520
48 }
49 704 return 0;
50 }
51  
52 /* One parsed header line: name at head+off (klen bytes), value at head+voff
53 * (vlen bytes, leading OWS trimmed; trailing OWS kept, matching what the env
54 * builder has always exposed). Only lines with a non-empty name before a
55 * colon are recorded - the same lines the env builder consumes. */
56 typedef struct { unsigned off, klen, voff, vlen; } hm_hline;
57  
58 /* Single pass over the header block: decide how the request body is framed
59 * (hardened against request smuggling, RFC 7230 3.3.3) and, when idx is
60 * given, record every header line so the env builder never rescans.
61 *
62 * - Transfer-Encoding: "chunked" is decoded (signalled by *clen = -1); any
63 * other coding is 501; a repeated Transfer-Encoding, or Transfer-Encoding
64 * together with Content-Length, is the classic TE.CL / CL.TE smuggling
65 * vector and is rejected 400.
66 * - Content-Length must appear at most once and be a single run of ASCII
67 * digits (no sign, no spaces, no trailing junk, no overflow); a duplicate,
68 * conflicting, or malformed value is 400.
69 *
70 * Returns 0 and sets *clen (>= 0 for a fixed length, -1 for chunked); an HTTP
71 * error status (400/501) to reject the request; or -2 when idx was too small,
72 * with *nlines set to the count needed (rejection takes precedence). */
73 965 static int hm_parse_index(const char *head, size_t headlen, long *clen,
74 hm_hline *idx, int idx_cap, int *nlines) {
75 965 const char *line_end = (const char *)memchr(head, '\r', headlen);
76 965 ▶ const char *p = line_end ? line_end + 2 : head + headlen; /* skip req line */
Branch: 1 branch
BranchTrueFalseTotal
const char *p = line_end ? line_end + 2 : head + headlen; /* skip req line */ 729 236 965
77 965 const char *hend = head + headlen;
78 965 int seen_cl = 0, seen_te = 0, chunked = 0, n = 0;
79 965 long cl = 0;
80 /* Line terminators are CRLF and nothing else (RFC 9112 2.2). A bare LF
81 * is not a terminator here, so a front end that honours one would frame
82 * a different request from the same bytes; a bare CR likewise. Either
83 * is 400. The one memchr pass over the block is the whole cost. */
84 {
85 965 const char *q = head;
86 2363 ▶ while (q < hend && (q = (const char *)memchr(q, '\n', hend - q))) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (q < hend && (q = (const char *)memchr(q, '\n', hend - q))) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2363 0 2363
while (q < hend && (q = (const char *)memchr(q, '\n', hend - q))) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1399 964 2363
87 1399 ▶ if (q == head || q[-1] != '\r') return 400; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (q == head || q[-1] != '\r') return 400; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1399 0 1399
if (q == head || q[-1] != '\r') return 400; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 1398 1399
88 1398 q++;
89 }
90 }
91 /* The request line must exist and hold a method and a target. An empty
92 * line here is a client that led with CRLF beyond the one hm_process
93 * strips, or a line with no SP at all. */
94 {
95 964 ▶ size_t ll = line_end ? (size_t)(line_end - head) : headlen;
Branch: 1 branch
BranchTrueFalseTotal
size_t ll = line_end ? (size_t)(line_end - head) : headlen; 728 236 964
96 const char *sp1, *sp2, *ver;
97 /*
98 # uncoverable branch true
99 # uncoverable condition left count:1 class:ignore_covered_err */
100 964 ▶ if (ll == 0) return 400;
Branch: 1 branch
BranchTrueFalseTotal
if (ll == 0) return 400; 0 964 964
101 /* METHOD SP request-target SP HTTP/d.d, and nothing else on the
102 * line (RFC 9112 3). A line with more tokens, or without the
103 * version, is not a request: a PROXY protocol header sent to a
104 * listener that did not ask for one reads as six tokens and used
105 * to be served as a request with method "PROXY". */
106 964 sp1 = (const char *)memchr(head, ' ', ll);
107 /*
108 # uncoverable branch true class:ignore_covered_err
109 # uncoverable condition left count:1 class:ignore_covered_err
110 # uncoverable condition left count:2 class:ignore_covered_err */
111 964 ▶ if (!sp1 || sp1 == head) return 400;
Branch: 2 branches
BranchTrueFalseTotal
if (!sp1 || sp1 == head) return 400; 964 0 964
if (!sp1 || sp1 == head) return 400; 0 964 964
112 964 sp2 = (const char *)memchr(sp1 + 1, ' ', ll - (size_t)(sp1 + 1 - head));
113 /*
114 # uncoverable branch true class:ignore_covered_err
115 # uncoverable condition left count:1 class:ignore_covered_err
116 # uncoverable condition left count:2 class:ignore_covered_err */
117 964 ▶ if (!sp2 || sp2 == sp1 + 1) return 400;
Branch: 2 branches
BranchTrueFalseTotal
if (!sp2 || sp2 == sp1 + 1) return 400; 964 0 964
if (!sp2 || sp2 == sp1 + 1) return 400; 0 964 964
118 964 ver = sp2 + 1;
119 /*
120 # uncoverable condition left count:2 class:ignore_covered_err
121 # uncoverable condition left count:3 class:ignore_covered_err
122 # uncoverable condition left count:4 class:ignore_covered_err
123 # uncoverable condition left count:5 class:ignore_covered_err
124 # uncoverable condition left count:6 class:ignore_covered_err
125 # uncoverable condition left count:7 class:ignore_covered_err */
126 964 ▶ if ((size_t)(head + ll - ver) != 8 || memcmp(ver, "HTTP/", 5) != 0
Branch: 2 branches
BranchTrueFalseTotal
if ((size_t)(head + ll - ver) != 8 || memcmp(ver, "HTTP/", 5) != 0 963 1 964
if ((size_t)(head + ll - ver) != 8 || memcmp(ver, "HTTP/", 5) != 0 963 0 963
127 963 ▶ || ver[5] < '0' || ver[5] > '9' || ver[6] != '.'
Branch: 3 branches
BranchTrueFalseTotal
|| ver[5] < '0' || ver[5] > '9' || ver[6] != '.' 963 0 963
|| ver[5] < '0' || ver[5] > '9' || ver[6] != '.' 963 0 963
|| ver[5] < '0' || ver[5] > '9' || ver[6] != '.' 963 0 963
128 964 ▶ || ver[7] < '0' || ver[7] > '9') return 400;
Branch: 2 branches
BranchTrueFalseTotal
|| ver[7] < '0' || ver[7] > '9') return 400; 963 0 963
|| ver[7] < '0' || ver[7] > '9') return 400; 0 963 963
129 /*
130 # uncoverable branch true class:ignore_covered_err
131 # uncoverable condition left count:1 class:ignore_covered_err
132 # uncoverable condition left count:2 class:ignore_covered_err
133 # uncoverable condition left count:3 class:ignore_covered_err */
134 963 ▶ if (line_end && (line_end + 1 >= hend || line_end[1] != '\n')) return 400;
Branch: 3 branches
BranchTrueFalseTotal
if (line_end && (line_end + 1 >= hend || line_end[1] != '\n')) return 400; 727 236 963
if (line_end && (line_end + 1 >= hend || line_end[1] != '\n')) return 400; 727 0 727
if (line_end && (line_end + 1 >= hend || line_end[1] != '\n')) return 400; 0 727 727
135 }
136 2352 ▶ while (p < hend) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < hend) { 1395 957 2352
137 1395 const char *eol = (const char *)memchr(p, '\r', hend - p);
138 1395 ▶ size_t len = eol ? (size_t)(eol - p) : (size_t)(hend - p);
Branch: 1 branch
BranchTrueFalseTotal
size_t len = eol ? (size_t)(eol - p) : (size_t)(hend - p); 668 727 1395
139 1395 const char *colon = (const char *)memchr(p, ':', len);
140 /*
141 # uncoverable branch true class:ignore_covered_err
142 # uncoverable condition left count:1 class:ignore_covered_err
143 # uncoverable condition left count:2 class:ignore_covered_err
144 # uncoverable condition left count:3 class:ignore_covered_err */
145 1395 ▶ if (eol && (eol + 1 >= hend || eol[1] != '\n')) return 400;
Branch: 3 branches
BranchTrueFalseTotal
if (eol && (eol + 1 >= hend || eol[1] != '\n')) return 400; 668 727 1395
if (eol && (eol + 1 >= hend || eol[1] != '\n')) return 400; 668 0 668
if (eol && (eol + 1 >= hend || eol[1] != '\n')) return 400; 0 668 668
146 1395 ▶ if (colon && colon > p) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (colon && colon > p) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1395 0 1395
if (colon && colon > p) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1395 0 1395
147 1395 size_t nk = (size_t)(colon - p);
148 1395 const char *v = colon + 1, *vend = p + len;
149 /* No whitespace between a field name and its colon (RFC 9112
150 * 5.1): "Content-Length : 5" would otherwise be stored under a
151 * name with a trailing space and frame the body as empty,
152 * leaving the body to be read as the next request. */
153 1395 ▶ if (colon[-1] == ' ' || colon[-1] == '\t') return 400; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (colon[-1] == ' ' || colon[-1] == '\t') return 400; /* # uncoverable condition left count:2 class:ignore_covered_err */ 1394 1 1395
if (colon[-1] == ' ' || colon[-1] == '\t') return 400; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1394 1394
154 /*
155 # uncoverable condition right count:1 class:ignore_covered_err
156 # uncoverable condition left count:3 class:ignore_covered_err */
157 2788 ▶ while (v < vend && (*v == ' ' || *v == '\t')) v++;
Branch: 3 branches
BranchTrueFalseTotal
while (v < vend && (*v == ' ' || *v == '\t')) v++; 2788 0 2788
while (v < vend && (*v == ' ' || *v == '\t')) v++; 1394 1394 2788
while (v < vend && (*v == ' ' || *v == '\t')) v++; 0 1394 1394
158 1394 ▶ if (idx && n < idx_cap) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (idx && n < idx_cap) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1394 0 1394
if (idx && n < idx_cap) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1385 9 1394
159 1385 idx[n].off = (unsigned)(p - head);
160 1385 idx[n].klen = (unsigned)nk;
161 1385 idx[n].voff = (unsigned)(v - head);
162 1385 idx[n].vlen = (unsigned)(vend - v);
163 }
164 1394 n++;
165 1394 ▶ if (nk == 17 && hm_strncasecmp(p, "Transfer-Encoding", 17) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (nk == 17 && hm_strncasecmp(p, "Transfer-Encoding", 17) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 24 1370 1394
if (nk == 17 && hm_strncasecmp(p, "Transfer-Encoding", 17) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 24 0 24
166 24 const char *tvend = vend;
167 /*
168 # uncoverable branch true
169 # uncoverable condition left count:1 class:ignore_covered_err
170 # uncoverable condition right count:1 class:ignore_covered_err
171 # uncoverable condition left count:2 class:ignore_covered_err
172 # uncoverable condition left count:3 class:ignore_covered_err */
173 24 ▶ while (tvend > v && (tvend[-1] == ' ' || tvend[-1] == '\t')) tvend--;
Branch: 3 branches
BranchTrueFalseTotal
while (tvend > v && (tvend[-1] == ' ' || tvend[-1] == '\t')) tvend--; 24 0 24
while (tvend > v && (tvend[-1] == ' ' || tvend[-1] == '\t')) tvend--; 0 24 24
while (tvend > v && (tvend[-1] == ' ' || tvend[-1] == '\t')) tvend--; 0 24 24
174 /*
175 # uncoverable branch true
176 # uncoverable condition left count:1 class:ignore_covered_err */
177 24 ▶ if (seen_te) return 400; /* repeated TE: ambiguous */
Branch: 1 branch
BranchTrueFalseTotal
if (seen_te) return 400; /* repeated TE: ambiguous */ 0 24 24
178 24 seen_te = 1;
179 24 ▶ if ((size_t)(tvend - v) == 7 && hm_strncasecmp(v, "chunked", 7) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if ((size_t)(tvend - v) == 7 && hm_strncasecmp(v, "chunked", 7) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ 23 1 24
if ((size_t)(tvend - v) == 7 && hm_strncasecmp(v, "chunked", 7) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ 23 0 23
180 23 chunked = 1;
181 else
182 1 return 501; /* unsupported coding */
183 1370 ▶ } else if (nk == 14 && hm_strncasecmp(p, "Content-Length", 14) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (nk == 14 && hm_strncasecmp(p, "Content-Length", 14) == 0) { 86 1284 1370
} else if (nk == 14 && hm_strncasecmp(p, "Content-Length", 14) == 0) { 86 0 86
184 86 const char *d, *dvend = vend;
185 /*
186 # uncoverable branch true
187 # uncoverable condition left count:1 class:ignore_covered_err
188 # uncoverable condition right count:1 class:ignore_covered_err
189 # uncoverable condition left count:2 class:ignore_covered_err
190 # uncoverable condition left count:3 class:ignore_covered_err */
191 86 ▶ while (dvend > v && (dvend[-1] == ' ' || dvend[-1] == '\t')) dvend--;
Branch: 3 branches
BranchTrueFalseTotal
while (dvend > v && (dvend[-1] == ' ' || dvend[-1] == '\t')) dvend--; 86 0 86
while (dvend > v && (dvend[-1] == ' ' || dvend[-1] == '\t')) dvend--; 0 86 86
while (dvend > v && (dvend[-1] == ' ' || dvend[-1] == '\t')) dvend--; 0 86 86
192 86 ▶ if (seen_cl || v == dvend) return 400; /* duplicate or empty */ /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (seen_cl || v == dvend) return 400; /* duplicate or empty */ /* # uncoverable condition left count:2 class:ignore_covered_err */ 85 1 86
if (seen_cl || v == dvend) return 400; /* duplicate or empty */ /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 85 85
193 85 seen_cl = 1;
194 412 ▶ for (d = v; d < dvend; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = v; d < dvend; d++) { 330 82 412
195 330 ▶ if (*d < '0' || *d > '9') return 400; /* non-digit */
Branch: 2 branches
BranchTrueFalseTotal
if (*d < '0' || *d > '9') return 400; /* non-digit */ 328 2 330
if (*d < '0' || *d > '9') return 400; /* non-digit */ 1 327 328
196 /*
197 # uncoverable branch true
198 # uncoverable condition left count:1 class:ignore_covered_err */
199 327 ▶ if (cl > (LONG_MAX - (*d - '0')) / 10) return 400; /* overflow */
Branch: 1 branch
BranchTrueFalseTotal
if (cl > (LONG_MAX - (*d - '0')) / 10) return 400; /* overflow */ 0 327 327
200 327 cl = cl * 10 + (*d - '0');
201 }
202 }
203 }
204 1389 ▶ p = eol ? eol + 2 : hend;
Branch: 1 branch
BranchTrueFalseTotal
p = eol ? eol + 2 : hend; 668 721 1389
205 }
206 957 ▶ if (seen_te && seen_cl) return 400; /* TE + CL together: smuggling */
Branch: 2 branches
BranchTrueFalseTotal
if (seen_te && seen_cl) return 400; /* TE + CL together: smuggling */ 23 934 957
if (seen_te && seen_cl) return 400; /* TE + CL together: smuggling */ 1 22 23
207 /*
208 # uncoverable branch false class:ignore_covered_err
209 # uncoverable condition right count:1 class:ignore_covered_err */
210 956 ▶ if (nlines) *nlines = n;
Branch: 1 branch
BranchTrueFalseTotal
if (nlines) *nlines = n; 956 0 956
211 956 ▶ if (idx && n > idx_cap) return -2; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (idx && n > idx_cap) return -2; /* # uncoverable condition right count:1 class:ignore_covered_err */ 956 0 956
if (idx && n > idx_cap) return -2; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 955 956
212 955 ▶ *clen = chunked ? -1 : cl;
Branch: 1 branch
BranchTrueFalseTotal
*clen = chunked ? -1 : cl; 933 22 955
213 955 return 0;
214 }
215  
216 /* Framing decision alone (no line index) - the fuzzer's entry point. */
217 /*
218 # uncoverable statement
219 # uncoverable subroutine */
220 0 static int hm_frame_length(const char *head, size_t headlen, long *clen) {
221 0 return hm_parse_index(head, headlen, clen, NULL, 0, NULL); /* # uncoverable statement */
222 }
223  
224 /* Validate a chunked request body in buf[0..avail) WITHOUT mutating it (so an
225 * incomplete body can be re-checked as more bytes arrive). On a complete body
226 * set *encoded (input octets through the terminating CRLF) and *decoded (total
227 * data octets) and return 1; return 0 if more input is needed, -1 if
228 * malformed, and HM_CHUNKED_TOOBIG if the decoded size would exceed max.
229 *
230 * "Too large" is a separate answer from "malformed" because they are
231 * different answers to the CLIENT: 400 says your framing is broken and
232 * retrying identically is pointless, 413 says the framing was fine and you
233 * should send less. Folding them together (as this did before 0.25) told
234 * every oversize chunked upload to go and fix its syntax. */
235 #define HM_CHUNKED_TOOBIG (-2)
236 /* A chunk-size line is at most this many hex digits. Sixteen cover a 64-bit
237 * size; anything longer is leading zeros, which a client can send by the
238 * megabyte to keep the scanner busy on one chunk. */
239 #define HM_CHUNK_DIGITS_MAX 16
240  
241 /* The scan, resumable: *rpos / *rdec name the last chunk boundary already
242 * validated (octets consumed, data octets so far) and are advanced to the
243 * newest one before returning 0, so a body arriving in many segments costs
244 * each chunk one look rather than one per segment. Pass zeros to start. */
245 22 static int hm_chunked_scan_from(const char *buf, size_t avail,
246 size_t *encoded, size_t *decoded, size_t max,
247 size_t *rpos, size_t *rdec) {
248 22 size_t pos = *rpos, dec = *rdec;
249 /*
250 # uncoverable branch true class:ignore_covered_err
251 # uncoverable condition left count:1 class:ignore_covered_err */
252 22 ▶ if (pos > avail) { pos = 0; dec = 0; } /* stale: restart */
Branch: 1 branch
BranchTrueFalseTotal
if (pos > avail) { pos = 0; dec = 0; } /* stale: restart */ 0 22 22
253 30 for (;;) {
254 52 size_t i = pos;
255 52 unsigned long long size = 0;
256 52 int ndig = 0;
257 52 *rpos = pos; *rdec = dec;
258 205 ▶ while (i < avail && buf[i] != '\r' && buf[i] != ';') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < avail && buf[i] != '\r' && buf[i] != ';') { 202 3 205
while (i < avail && buf[i] != '\r' && buf[i] != ';') { 158 44 202
while (i < avail && buf[i] != '\r' && buf[i] != ';') { 155 3 158
259 155 int ch = (unsigned char)buf[i], v;
260 155 ▶ if (ch >= '0' && ch <= '9') v = ch - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= '0' && ch <= '9') v = ch - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */ 155 0 155
if (ch >= '0' && ch <= '9') v = ch - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */ 151 4 155
261 4 ▶ else if (ch >= 'a' && ch <= 'f') v = ch - 'a' + 10; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (ch >= 'a' && ch <= 'f') v = ch - 'a' + 10; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 3 4
else if (ch >= 'a' && ch <= 'f') v = ch - 'a' + 10; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
262 3 ▶ else if (ch >= 'A' && ch <= 'F') v = ch - 'A' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (ch >= 'A' && ch <= 'F') v = ch - 'A' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
else if (ch >= 'A' && ch <= 'F') v = ch - 'A' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 1 3
263 1 else return -1; /* bad hex digit */
264 /*
265 # uncoverable branch true
266 # uncoverable condition left count:1 class:ignore_covered_err */
267 154 ▶ if (size > (ULLONG_MAX >> 4)) return -1; /* size overflow */
Branch: 1 branch
BranchTrueFalseTotal
if (size > (ULLONG_MAX >> 4)) return -1; /* size overflow */ 0 154 154
268 154 ▶ if (++ndig > HM_CHUNK_DIGITS_MAX) return -1; /* digit flood */
Branch: 1 branch
BranchTrueFalseTotal
if (++ndig > HM_CHUNK_DIGITS_MAX) return -1; /* digit flood */ 1 153 154
269 153 size = (size << 4) | (unsigned)v;
270 153 i++;
271 }
272 /* No digits. Malformed only if the bytes to say so are HERE: a
273 * chunk-size line that has not arrived yet is indistinguishable from
274 * a missing one by length alone, and calling it malformed refuses
275 * every chunked request whose header block lands in its own packet -
276 * which is every chunked request that waits for a 100-continue, and
277 * any other whose body simply follows a segment boundary. */
278 50 ▶ if (ndig == 0) return i >= avail ? 0 : -1; /* need more / no size */ /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ndig == 0) return i >= avail ? 0 : -1; /* need more / no size */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 47 50
if (ndig == 0) return i >= avail ? 0 : -1; /* need more / no size */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
279 /* A bare LF or CR ends the scan and fails the CRLF test below: the
280 * header lines refuse them, and a chunk extension or a trailer that
281 * accepted one let an LF-tolerant front end see the body end where
282 * this parser did not (request smuggling). */
283 59 ▶ while (i < avail && buf[i] != '\r' && buf[i] != '\n') i++; /* skip chunk-ext */ /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
while (i < avail && buf[i] != '\r' && buf[i] != '\n') i++; /* skip chunk-ext */ /* # uncoverable condition right count:1 class:ignore_covered_err */ 59 0 59
while (i < avail && buf[i] != '\r' && buf[i] != '\n') i++; /* skip chunk-ext */ /* # uncoverable condition right count:1 class:ignore_covered_err */ 13 46 59
while (i < avail && buf[i] != '\r' && buf[i] != '\n') i++; /* skip chunk-ext */ /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 1 13
284 /*
285 # uncoverable branch true
286 # uncoverable condition left count:1 class:ignore_covered_err */
287 47 ▶ if (i + 1 >= avail) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 >= avail) return 0; 0 47 47
288 47 ▶ if (buf[i] != '\r' || buf[i + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i] != '\r' || buf[i + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 46 1 47
if (buf[i] != '\r' || buf[i + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 46 46
289 46 i += 2;
290 46 ▶ if (size == 0) { /* last-chunk + trailers */
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) { /* last-chunk + trailers */ 11 35 46
291 1 for (;;) {
292 12 size_t j = i;
293 36 ▶ while (j < avail && buf[j] != '\r' && buf[j] != '\n') j++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
while (j < avail && buf[j] != '\r' && buf[j] != '\n') j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 36 0 36
while (j < avail && buf[j] != '\r' && buf[j] != '\n') j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 25 11 36
while (j < avail && buf[j] != '\r' && buf[j] != '\n') j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 24 1 25
294 /*
295 # uncoverable branch true
296 # uncoverable condition left count:1 class:ignore_covered_err */
297 12 ▶ if (j + 1 >= avail) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (j + 1 >= avail) return 0; 0 12 12
298 12 ▶ if (buf[j] != '\r' || buf[j + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (buf[j] != '\r' || buf[j + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 11 1 12
if (buf[j] != '\r' || buf[j + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 11 11
299 11 ▶ if (j == i) { *encoded = j + 2; *decoded = dec; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (j == i) { *encoded = j + 2; *decoded = dec; return 1; } 10 1 11
300 1 i = j + 2; /* skip a trailer line */
301 }
302 }
303 35 ▶ if (size > max || dec + size > max) return HM_CHUNKED_TOOBIG;
Branch: 2 branches
BranchTrueFalseTotal
if (size > max || dec + size > max) return HM_CHUNKED_TOOBIG; 34 1 35
if (size > max || dec + size > max) return HM_CHUNKED_TOOBIG; 1 33 34
304 33 ▶ if (i + size + 2 > avail) return 0; /* await chunk + CRLF */
Branch: 1 branch
BranchTrueFalseTotal
if (i + size + 2 > avail) return 0; /* await chunk + CRLF */ 3 30 33
305 /*
306 # uncoverable branch true class:ignore_covered_err
307 # uncoverable condition left count:1 class:ignore_covered_err
308 # uncoverable condition left count:2 class:ignore_covered_err */
309 30 ▶ if (buf[i + size] != '\r' || buf[i + size + 1] != '\n') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i + size] != '\r' || buf[i + size + 1] != '\n') return -1; 30 0 30
if (buf[i + size] != '\r' || buf[i + size + 1] != '\n') return -1; 0 30 30
310 30 dec += (size_t)size;
311 30 pos = i + (size_t)size + 2;
312 }
313 }
314  
315 /*
316 # uncoverable statement
317 # uncoverable subroutine */
318 0 static int hm_chunked_scan(const char *buf, size_t avail,
319 size_t *encoded, size_t *decoded, size_t max) {
320 0 size_t pos = 0, dec = 0; /* # uncoverable statement */
321 0 return hm_chunked_scan_from(buf, avail, encoded, decoded, max, &pos, &dec); /* # uncoverable statement */
322 }
323  
324 /* Compact a chunked body already validated by hm_chunked_scan to its decoded
325 * octets at the front of buf; returns the decoded length. Structure is trusted
326 * (scan verified every bound over the same, unmodified bytes). */
327 10 static size_t hm_chunked_compact(char *buf) {
328 10 size_t pos = 0, w = 0;
329 29 for (;;) {
330 39 size_t i = pos;
331 39 size_t size = 0;
332 147 ▶ while (buf[i] != '\r' && buf[i] != ';') {
Branch: 2 branches
BranchTrueFalseTotal
while (buf[i] != '\r' && buf[i] != ';') { 110 37 147
while (buf[i] != '\r' && buf[i] != ';') { 108 2 110
333 108 int ch = (unsigned char)buf[i];
334 108 ▶ size = (size << 4) | (unsigned)(ch <= '9' ? ch - '0' : (ch | 32) - 'a' + 10);
Branch: 1 branch
BranchTrueFalseTotal
size = (size << 4) | (unsigned)(ch <= '9' ? ch - '0' : (ch | 32) - 'a' + 10); 105 3 108
335 108 i++;
336 }
337 47 ▶ while (buf[i] != '\r') i++;
Branch: 1 branch
BranchTrueFalseTotal
while (buf[i] != '\r') i++; 8 39 47
338 39 i += 2;
339 39 ▶ if (size == 0) return w;
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) return w; 10 29 39
340 29 memmove(buf + w, buf + i, size);
341 29 w += size;
342 29 pos = i + size + 2;
343 }
344 }
345  
346 #endif
« include/hyperman/hm_compress.h ↑ Summary include/hyperman/hm_proxy.h »