Commit 888c779
authored
Two bugs flagged by clippy's correctness lints that would each cause
real runtime failure given the right input -- not just style nits:
1. `read_all_lines` in lib/common/io.rs spun forever on a broken stdin.
`BufRead::lines()` returns an `io::Lines` iterator that, once the
underlying reader yields an error, keeps yielding the same `Err`
forever. The old `filter_map(|l| l.ok())` swallowed every `Err` and
asked for the next one -- so a single decoding error or closed
handle made the function never return. Swapped to
`map_while(Result::ok)` so the iterator terminates at the first
error (clippy::lines_filter_map_ok).
2. `int_to_string_radix` (string.rs:209) and `int_to_str_radix`
(types.rs:103) both did `n.abs() as u64`, which panics in debug on
`i64::MIN` because `i64::MIN.abs()` overflows (the magnitude
9_223_372_036_854_775_808 doesn't fit in `i64`). Swapped to
`n.unsigned_abs()`, which returns the magnitude as `u64` directly
and is defined for every `i64` (clippy::cast_abs_to_unsigned).
Regression tests added for the radix-conversion bug:
* `test_radix_i64_min_does_not_panic` in lib/common/string.rs
* `test_int_to_str_radix_i64_min_does_not_panic` in lib/common/types.rs
Both formerly panicked in debug builds (the test process aborted with
"attempt to negate with overflow"); both now round-trip to
"-9223372036854775808".
`read_all_lines` is harder to regression-test in unit tests since it
reads real stdin; the behavior change is verified by the swapped
iterator combinator and clippy no longer flagging that site.
Part 2 of 4 from #22. Off clean origin/main; does not depend on PR1
(#23).
1 parent fe4b51c commit 888c779
3 files changed
Lines changed: 35 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
40 | 45 | | |
41 | 46 | | |
42 | 47 | | |
43 | 48 | | |
44 | | - | |
| 49 | + | |
45 | 50 | | |
46 | 51 | | |
47 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
209 | | - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
210 | 213 | | |
211 | 214 | | |
212 | 215 | | |
| |||
305 | 308 | | |
306 | 309 | | |
307 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
308 | 321 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
104 | 107 | | |
105 | 108 | | |
106 | 109 | | |
| |||
312 | 315 | | |
313 | 316 | | |
314 | 317 | | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
315 | 328 | | |
316 | 329 | | |
317 | 330 | | |
| |||
0 commit comments