halostatue / diff-lcs

Generate difference sets between Ruby sequences.
http://halostatue.github.com/diff-lcs
Other
290 stars 57 forks source link

Make Diff::LCS::Hunk Robust #43

Closed junaruga closed 4 years ago

junaruga commented 6 years ago

Seeing below lines. https://github.com/halostatue/diff-lcs/blob/v1.3/lib/diff/lcs/hunk.rb#L42-L45

@start_old = a1 || (b1 - before)
@start_new = b1 || (a1 + before)
@end_old   = a2 || (b2 - after)
@end_new = b2 || (a2 + after)

When both a1 and b1 are nil or both a2 and b2 are nil, NameError is raised unintentionally. Maybe we can add a code to check it in advance?

$ cat diff.rb 
a1 = nil
b1 = nil
start_old = a1 || (b1 - before)
$ ruby diff.rb
Traceback (most recent call last):
diff.rb:3:in `<main>': undefined local variable or method `before' for main:Object (NameError)
halostatue commented 5 years ago

Do you have an example that causes this in the real library? I can see that this could happen, but it would only be a case where there’s a hunk that has neither remove or insert operations. An actual example allows me to write useful tests and decide the correct sort of behaviour.

junaruga commented 5 years ago

Hi @halostatue . Thanks for checking the code. I do not have the real library. This was detected by a static code analysis tool Possibly it is Coverity Scan. https://scan.coverity.com/