exercism / exalysis

Mentoring tool for the Go track on Exercism. Downloads students code, checks it and provides suggestions.
33 stars 13 forks source link

hamming: panic: runtime error: invalid memory address or nil pointer dereference #79

Closed bitfield closed 5 years ago

bitfield commented 5 years ago

Solution 97618ad0f6ed4ebab33f591f91b59b6c:

package hamming

import "errors"

func Distance(left, right string) (int, error) {
    if len(left) != len(right) {
        return 0, errors.New("boom")
    }
    return hammingDistance(left, right, 0), nil
}

func hammingDistance(left, right string, distance int) int {
    if len(left) > 0 {
        return hammingDistance(tailOf(left), tailOf(right), newDistance(left, right, distance))
    }
    return distance
}

func newDistance(left, right string, distance int) int {
    if headOf(left) != headOf(right) {
        return distance + 1
    }
    return distance
}

func headOf(s string) string {
    return s[:1]
}

func tailOf(s string) string {
    return s[1:]
}

gives:

I know the "hamming" exercise, so I'll try to make some specific suggestions about it, as well as the general code checks:
go test:         OK
go test -race:   SKIPPED
golint:          FAIL
comments: exported function Distance should have comment or be unexported
        /Users/john/git/bitfield/exercism/users/mickaelw/go/hamming/hamming.go:5:1
        doc: https://golang.org/wiki/CodeReviewComments#doc-comments

gofmt:           OK
go vet:          OK
golangci-lint:   OK
benchmarks:      SKIPPED
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x78 pc=0x12a2992]

goroutine 1 [running]:
github.com/tehsphinx/exalysis/track/hamming.examIncrease(0xc0000d6000, 0xc0003260c0)
        /Users/john/git/bitfield/exalysis/track/hamming/hamming.go:173 +0x62
github.com/tehsphinx/exalysis/track/hamming.Suggest(0xc0000d6000, 0xc0003260c0)
        /Users/john/git/bitfield/exalysis/track/hamming/hamming.go:17 +0x7a
github.com/tehsphinx/exalysis.GetSuggestions(0xc000218640, 0x3b, 0xc0002b8ea0, 0x1, 0x1, 0x4e)
        /Users/john/git/bitfield/exalysis/suggestion.go:66 +0x54f
main.watchClipboard()
        /Users/john/git/bitfield/exalysis/cmd/exalysis/main.go:64 +0x23e
main.main()
        /Users/john/git/bitfield/exalysis/cmd/exalysis/main.go:31 +0x26a
tehsphinx commented 5 years ago

not crashing with the new algorithm any more