Skip to content

Commit 188150c

Browse files
committed
fuzzystrmatch: fix out of bounds in LevenshteinLessEqual
This fixes a bug that was added when this function was introduced in 7a538b3. No release note since this bug was not released yet. Release note: None
1 parent 7b270fe commit 188150c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pkg/util/fuzzystrmatch/leven.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ func LevenshteinLessEqualDistanceWithCost(
123123
bestColumn = -netInserts
124124
}
125125
stopColumn = bestColumn + (slackDist / (insCost + delCost)) + 1
126+
if stopColumn < 0 {
127+
stopColumn = 0
128+
}
126129
if stopColumn > lenS {
127130
stopColumn = lenS + 1
128131
}

pkg/util/fuzzystrmatch/leven_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
package fuzzystrmatch
77

8-
import "testing"
8+
import (
9+
"math"
10+
"testing"
11+
)
912

1013
func TestLevenshteinDistance(t *testing.T) {
1114
tt := []struct {
@@ -482,3 +485,33 @@ func TestLevenshteinLessEqualDistanceWithCost(t *testing.T) {
482485
}
483486
}
484487
}
488+
489+
func TestLevenshtein_ExtremeValues(t *testing.T) {
490+
// Test all combinations of math.MinInt64 and math.MaxInt64 for integer
491+
// arguments. This ensures the function handles extreme values correctly
492+
// without panicking.
493+
extremeValues := []int{math.MinInt64, math.MaxInt64, 0, 1, -1, -6503603920424974249, -3160545599026710833, 3234088755759361354}
494+
testStrings := []string{"ab", "bc", "", "test"}
495+
496+
for _, source := range testStrings {
497+
for _, target := range testStrings {
498+
for _, insCost := range extremeValues {
499+
for _, delCost := range extremeValues {
500+
for _, subCost := range extremeValues {
501+
for _, maxDist := range extremeValues {
502+
// Call the functions and ensure it doesn't panic. We don't check
503+
// the exact result since extreme values may cause integer
504+
// overflow, but we ensure no panic occurs.
505+
_ = LevenshteinLessEqualDistanceWithCost(
506+
source, target, insCost, delCost, subCost, maxDist,
507+
)
508+
_ = LevenshteinDistanceWithCost(
509+
source, target, insCost, delCost, subCost,
510+
)
511+
}
512+
}
513+
}
514+
}
515+
}
516+
}
517+
}

0 commit comments

Comments
 (0)