jhvst / go-jaro-winkler-distance

Jaro-Winkler distance in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

panic in matches index

idawson-gl opened this issue · comments

There is a panic when comparing the window runes with the matches slice if the strings.Index call returns an index greater than the number of matches.

		idx := strings.Index(string(slider), string(runes1[i]))
		if idx != -1 && !matches[idx] { // panic here
			t += 0.5
			matches[idx] = true
		}

I was able to reproduce this when comparing the two following strings:

jwd.Calculate("asdfadsfT", "Academic Free License v1.2")
...

goroutine 19 [running]:
testing.tRunner.func1.2({0x104db2d80, 0x14000126258})
	/opt/homebrew/Cellar/go/1.19.1/libexec/src/testing/testing.go:1396 +0x1c8
testing.tRunner.func1()
	/opt/homebrew/Cellar/go/1.19.1/libexec/src/testing/testing.go:1399 +0x378
panic({0x104db2d80, 0x14000126258})
	/opt/homebrew/Cellar/go/1.19.1/libexec/src/runtime/panic.go:884 +0x204
github.com/jhvst/go-jaro-winkler-distance.Calculate({0x104d16155?, 0x104d16ed8?}, {0x104d1c760, 0x1a})
	/Users/xxx/go/pkg/mod/github.com/jhvst/go-jaro-winkler-distance@v0.0.0-20220308004700-32671379e37c/algo.go:58 +0x4c4
testing.tRunner(0x14000107040, 0x104dc3a28)

While this can be fixed with ensuring the length, I'm not entirely sure the logic of the strings.Index is correct, or how the matches slice is created is correct.

		idx := strings.Index(string(slider), string(runes1[i]))
		if idx != -1 && idx < len(matches) && !matches[idx] {
			t += 0.5
			matches[idx] = true
		}

Should the index really ever be returning a value that's greater than the number of matches?

Hi Isaac, thanks for the bug report. I have to remind myself what the reference implementation was, and recheck the logic issue you raised here. It's likely that you are correct here, but let me find the time to take a plunge at the code and return back to this.

Hi Has this issue been fixed?

I have run into this myself.

It looks like any strings that are less than size "window" or have fewer matches than size "window" will crash.