skiprope icon indicating copy to clipboard operation
skiprope copied to clipboard

String() crashes with an incomplete (?) ending byte

Open codegoalie opened this issue 8 years ago • 2 comments

Discovered adding fuzz tests with #8

Reproducing test:

func TestIncompleteLast(t *testing.T) {
    a := "00000000000000000000" +
        "00000000000000000000" +
        "00000000000000000000" +
        "000\xcb"

    r := New()
    if err := r.Insert(0, a); err != nil {
        t.Fatal(err)
    }

    assert.Equal(t, a, r.String())
}
--- FAIL: TestIncompleteLast (0.00s)
panic: runtime error: slice bounds out of range [recovered]
        panic: runtime error: slice bounds out of range

goroutine 11 [running]:
testing.tRunner.func1(0xc420102690)
        /home/codegoalie/workspace/sources/go/src/testing/testing.go:711 +0x2d2
panic(0x6f86c0, 0x94c330)
        /home/codegoalie/workspace/sources/go/src/runtime/panic.go:491 +0x283
github.com/codegoalie/skiprope.(*Rope).SubstrBytes(0xc420104900, 0x0, 0x40, 0x0, 0xc420299600, 0x40)
        /home/codegoalie/workspace/go/src/github.com/codegoalie/skiprope/rope.go:107 +0x3b0
github.com/codegoalie/skiprope.(*Rope).Substr(0xc420104900, 0x0, 0x40, 0x4e5b01, 0xc420104900)
        /home/codegoalie/workspace/go/src/github.com/codegoalie/skiprope/rope.go:121 +0x3f
github.com/codegoalie/skiprope.(*Rope).String(0xc420104900, 0x0, 0x75c744)
        /home/codegoalie/workspace/go/src/github.com/codegoalie/skiprope/rope.go:186 +0x3d
github.com/codegoalie/skiprope.TestIncompleteLast(0xc420102690)
        /home/codegoalie/workspace/go/src/github.com/codegoalie/skiprope/rope_test.go:212 +0x17d
testing.tRunner(0xc420102690, 0x764360)
        /home/codegoalie/workspace/sources/go/src/testing/testing.go:746 +0xd0
created by testing.(*T).Run
        /home/codegoalie/workspace/sources/go/src/testing/testing.go:789 +0x2de
exit status 2
FAIL    github.com/codegoalie/skiprope  0.011s

codegoalie avatar Oct 21 '17 13:10 codegoalie

Looks like the reason is it's not calculating the length of bytes correctly. What do you think the fix should be?

chewxy avatar Oct 21 '17 19:10 chewxy

Seems like we are getting Rope.runes from utf8.RuneCount which treats incorrect stuff as a single rune:

RuneCount returns the number of runes in p. Erroneous and short encodings are treated as single runes of width 1 byte.

Perhaps there are instances where we are counting runes when we should be counting bytes? I haven't been able to dig into much of the search.go code.

codegoalie avatar Nov 02 '17 13:11 codegoalie