Skip to content

Panic when calling AccuracyMutatingLetters() #9

@douglasm

Description

@douglasm

When testing some data and calling cm.AccuracyMutatingLetters() I am getting a panic of:
panic: invalid argument to Intn

math/rand.(*Rand).Intn(0xc000080180, 0x0, 0x2)
/usr/local/go/src/math/rand/rand.go:169 +0x9c
math/rand.Intn(0x0, 0x2)
/usr/local/go/src/math/rand/rand.go:329 +0x37
github.com/schollz/closestmatch.(*ClosestMatch).AccuracyMutatingLetters(0xc0005354d0, 0x10)
/Users/imac/go/src/github.com/schollz/closestmatch/closestmatch.go:309 +0x2b2

My call is:

	cm := closestmatch.New(possible, subsetSize)
	result := cm.Closest("Rose Villa")

where:

	subsetSize := []int{2, 3, 4}
	possible := []string{"Chris Horsburgh's B&B", "R & R Boutique and Breakfast", 
                          "Oaklands Guest House", "Guthrie Bed and Breakfast",
		          "Arndean Bed & Breakfast", "Duntrune House B & B",
                           "Cortachy House Bed & Breakfast", "Brucefield Boutique Bed and Breakfast",
		          "Rose Villa B&B", "Crepto Bed and Breakfast", "Newton Farm Holidays",
                           "West Adamston Farmhouse", "Liscara Bed & Breakfast",
		          "Chapel House B & B", "Kinnaber B & B", "Maulesbank Guest House",
                           "Fallady Bank Bed and Breakfast", "Church House Bed & Breakfast",
		          "Bridgelee Bed and Breakfast", "Croftsmuir B & B", "Denbrae Cottage B&B",
                           "Bubbly Oak (The) ", "Purgavie Farmhouse Bed and Breakfast ",
		          "Muirhouses Farm Bed and Breakfast", "Five Gables House B&B"}

On examining the code for AccuracyMutatingLetters(), at line 283 of closestmatch.go i is being incremented before being compared to the random integer returned from rand.IntN. When the returned random number is 0 the i != testStringNum will never pass and as such the testString is always blank. When you get to line 309 where a rand.IntN is returned using the length of the testString as the parameter you get the panic as rand.IntN does not accept 0 as a valid value.

I got the code to run by moving the i++ line to after the i != testStringNum compare, but I do not know if this fix will cause the function to return inaccurate results.

There is also a i++ before a comparison in the AccuracyMutatingWords function at line 208.

Hope this all helps, let me know if you need more information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions