jonhoo / inferno

A Rust port of FlameGraph
Other
1.68k stars 125 forks source link

flamegraph panics on empty lines with --reverse set #238

Closed mrob95 closed 2 years ago

mrob95 commented 2 years ago

Found while I was investigating #237

Repro:

noploop;[unknown] 2

* Run `cargo test flamegraph_should_warn_about_bad_input_lines_with_reversed_stack_ordering`

Failure:

---- flamegraph_should_warn_about_bad_input_lines_with_reversed_stack_ordering stdout ---- thread 'flamegraph_should_warn_about_bad_input_lines_with_reversed_stack_ordering' panicked at 'attempt to subtract with overflow', /home/mike/inferno/src/flamegraph/mod.rs:415:60 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

failures: flamegraph_should_warn_about_bad_input_lines_with_reversed_stack_ordering



The issue is here: https://github.com/jonhoo/inferno/blob/master/src/flamegraph/mod.rs#L411

`samples_idx` is 0 when we have an empty line, and we are trying to subtract from it.

A simple fix would be to add a check for `line.is_empty()` at the top of that loop. Alternatively, it seems like the reason we are subtracting 1 from sample_idx is to remove a trailing space from the line, so replacing with `&line[..samples_idx].trim_end()` would fix it.