Commit 10710de
authored
Enforce the chunk-size grammar in a single pass (#661)
#631 validated the chunk-size line twice: a hand-rolled byte walk
(hex_digits?/1, all_hex_digits?/1) followed by Integer.parse/2. The
overlap between the two is total once the first check passes: RFC9112
section 7.1 defines chunk-size as 1*HEXDIG, and Integer.parse/2's only
divergence from that grammar is that it additionally accepts a single
leading '+' or '-'. A sign can only ever appear in the first byte, so
once hex_digits?/1 confirms every byte is a hex digit, Integer.parse/2
on that string cannot return anything but {n, ""} - the with clause's
second branch was unreachable as a failure path.
Collapse the validator into one function with two clauses: a guard on
the leading byte closes the sign gap directly, and the existing
{chunk_size, ""} match still rules out trailing junk. The accepted
language is unchanged.
Verified byte-for-byte equivalence between the old and new validators
across 41 handpicked adversarial inputs, 200,000 randomized strings
over an adversarial alphabet, and 500,000 raw random byte strings
(not restricted to valid UTF-8) - zero divergences. All four of #631's
regression tests pass unchanged. One new test pins a case that was an
explicit clause before (empty chunk-size) but is now implicit in the
pattern match: a chunk-ext with no preceding size digits.
Credit: this refactor was proposed in a review comment on #631 by
@mpettibone (#631 (comment)).
I independently re-verified the equivalence claims (including with a
separate implementation and additional raw-byte fuzzing) before
applying it here.1 parent d69202e commit 10710de
2 files changed
Lines changed: 36 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
358 | 358 | | |
359 | 359 | | |
360 | 360 | | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
369 | 373 | | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
375 | 378 | | |
376 | 379 | | |
377 | 380 | | |
378 | 381 | | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
| 382 | + | |
391 | 383 | | |
392 | 384 | | |
393 | 385 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1350 | 1350 | | |
1351 | 1351 | | |
1352 | 1352 | | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
1353 | 1372 | | |
1354 | 1373 | | |
1355 | 1374 | | |
| |||
0 commit comments