mitsuhiko / similar

A high level diffing library for rust based on diffs
https://insta.rs/similar
Apache License 2.0
949 stars 31 forks source link

Crash in UnifiedDiff::to_string method #37

Closed sv-91 closed 2 years ago

sv-91 commented 2 years ago

This code crashes or produces incorrect output

    let fst = "\u{18}\n\n";
    let snd = "\n\n\r";

    let mut config = similar::TextDiffConfig::default();
    let diff = config.diff_lines(fst, snd);
    let mut output = diff.unified_diff();
    let result = output.context_radius(0).to_string();
    println!("Result:\n{}", result);
mitsuhiko commented 2 years ago

A context_radius of 0 does not make sense. I will add an explicit panic here in the internal function to make it error earlier.

sv-91 commented 2 years ago

Yes, there are other examples here with different radius but with panic. I don't understand, why there is needed explicit panic. I think, this is correct example, and it must work correctly. You can change first symbol of fst to any other symbol, it doesn't matter

mitsuhiko commented 2 years ago

As far as I can tell the example works fine with a non zero radius. I also believe the input data is irrelevant for the particular problem that a radius of 0 causes. What would you expect that a radius of 0 does? I will have another look at this.