Commit 66e39ec
committed
fix(pack): a garbage e_lfanew threw instead of answering "not a PE"
`identify()` is documented as never throwing and, for one input, terminated
the process. `std::string_view::substr` throws `std::out_of_range` when
`pos > size()`, and the position came straight out of the file:
if (b.substr(*lfanew, 4) == "PE\0\0")
A file that starts with "MZ" and has garbage at 0x3C is ordinary malformed
input — a truncated download, a DOS stub, a text file named `.exe` — and
`mcpp pack` calls this on every artifact it packages. Measured rather than
argued: `std::string_view{256 bytes}.substr(0xFFFFFFFF, 4)` throws
`string_view::substr` under libc++.
Every other read in the module goes through a bounds-checked accessor; these
two comparisons were the exceptions, which is exactly how a module whose
stated contract is "total over nonsense" stopped being one. `has_at()` now
does the same job with the same check as the rest.
Found by re-reading the file after it was already merged and green — the
existing truncation test never produced an `e_lfanew` PAST the end, only one
exactly AT it, where `substr` is well-defined and returns empty.1 parent add6317 commit 66e39ec
2 files changed
Lines changed: 36 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
163 | 176 | | |
164 | 177 | | |
165 | 178 | | |
| |||
309 | 322 | | |
310 | 323 | | |
311 | 324 | | |
312 | | - | |
| 325 | + | |
313 | 326 | | |
314 | 327 | | |
315 | 328 | | |
| |||
445 | 458 | | |
446 | 459 | | |
447 | 460 | | |
448 | | - | |
| 461 | + | |
449 | 462 | | |
450 | 463 | | |
451 | 464 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
324 | 324 | | |
325 | 325 | | |
326 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
327 | 348 | | |
328 | 349 | | |
329 | 350 | | |
| |||
0 commit comments