Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Cursor.Last() index out of range #63

Closed
tv42 opened this issue Mar 13, 2014 · 1 comment · Fixed by #64
Closed

Cursor.Last() index out of range #63

tv42 opened this issue Mar 13, 2014 · 1 comment · Fixed by #64

Comments

@tv42
Copy link
Contributor

tv42 commented Mar 13, 2014

This bug was introduced in 7214e08

func TestCursorLast(t *testing.T) {
    withOpenDB(func(db *DB, path string) {
        db.CreateBucket("widgets")
        db.Do(func(txn *RWTransaction) error {
            c := txn.Bucket("widgets").Cursor()
            _, _ = c.Last()
            return nil
        })
    })
}
[0 tv@brute ((7214e08...)) ~/go/src/github.com/boltdb/bolt]$ git checkout 1eb9e0902824fff3e7a943d421e83c7d30f43176
Previous HEAD position was 7214e08... Merge pull request #57 from benbjohnson/node-aware-cursors
HEAD is now at 1eb9e09... Merge branch 'master' of https://github.com/boltdb/bolt
[0 tv@brute ((1eb9e09...)) ~/go/src/github.com/boltdb/bolt]$ go test -run=TestCursorLast
seed: 64196
quick settings: count=5, items=1000, ksize=1024, vsize=1024
PASS
ok      github.com/boltdb/bolt  0.007s
[0 tv@brute ((1eb9e09...)) ~/go/src/github.com/boltdb/bolt]$ git checkout 7214e089c0f78fce8fb1e00f09f7d2b54c88bdbe
Previous HEAD position was 1eb9e09... Merge branch 'master' of https://github.com/boltdb/bolt
HEAD is now at 7214e08... Merge pull request #57 from benbjohnson/node-aware-cursors
[0 tv@brute ((7214e08...)) ~/go/src/github.com/boltdb/bolt]$ go test -run=TestCursorLast
seed: 87363
quick settings: count=5, items=1000, ksize=1024, vsize=1024
--- FAIL: TestCursorLast (0.00 seconds)
panic: runtime error: index out of range [recovered]
    panic: runtime error: index out of range

goroutine 19 [running]:
runtime.panic(0x5cd580, 0x766da3)
    /home/tv/src/go/src/pkg/runtime/panic.c:249 +0xba
testing.func·006()
    /home/tv/src/go/src/pkg/testing/testing.go:416 +0x15d
runtime.panic(0x5cd580, 0x766da3)
    /home/tv/src/go/src/pkg/runtime/panic.c:232 +0x116
github.com/boltdb/bolt.(*Cursor).keyValue(0xc208031d70, 0x0, 0x0, 0x0, 0x0, ...)
    /home/tv/go/src/github.com/boltdb/bolt/cursor.go:249 +0x30d
github.com/boltdb/bolt.(*Cursor).Last(0xc208031d70, 0x0, 0x0, 0x0, 0x0, ...)
    /home/tv/go/src/github.com/boltdb/bolt/cursor.go:35 +0x26e
github.com/boltdb/bolt.func·047(0xc208028050, 0x0, 0x0)
    /home/tv/go/src/github.com/boltdb/bolt/bug_test.go:11 +0xef
github.com/boltdb/bolt.(*DB).Do(0xc20805e000, 0x6ac508, 0x0, 0x0)
    /home/tv/go/src/github.com/boltdb/bolt/db.go:354 +0x82
github.com/boltdb/bolt.func·048(0xc20805e000, 0xc20800dec0, 0x1b)
    /home/tv/go/src/github.com/boltdb/bolt/bug_test.go:13 +0x53
github.com/boltdb/bolt.func·075(0xc20805e000, 0xc20800dec0, 0x1b)
    /home/tv/go/src/github.com/boltdb/bolt/db_test.go:372 +0x132
github.com/boltdb/bolt.withDB(0x7ff0c96fdf38)
    /home/tv/go/src/github.com/boltdb/bolt/db_test.go:353 +0xfc
github.com/boltdb/bolt.withOpenDB(0x6ac510)
    /home/tv/go/src/github.com/boltdb/bolt/db_test.go:373 +0x5c
github.com/boltdb/bolt.TestCursorLast(0xc208052120)
    /home/tv/go/src/github.com/boltdb/bolt/bug_test.go:14 +0x27
testing.tRunner(0xc208052120, 0x7691a0)
    /home/tv/src/go/src/pkg/testing/testing.go:422 +0x9d
created by testing.RunTests
    /home/tv/src/go/src/pkg/testing/testing.go:503 +0x8d9

goroutine 16 [chan receive]:
testing.RunTests(0x6ac570, 0x768fc0, 0x54, 0x54, 0x1)
    /home/tv/src/go/src/pkg/testing/testing.go:504 +0x909
testing.Main(0x6ac570, 0x768fc0, 0x54, 0x54, 0x76a9a0, ...)
    /home/tv/src/go/src/pkg/testing/testing.go:435 +0x9e
main.main()
    /home/tv/tmp/go-build690133294/github.com/boltdb/bolt/_test/_testmain.go:229 +0x9c
exit status 2
FAIL    github.com/boltdb/bolt  0.009s
[1 tv@brute ((7214e08...)) ~/go/src/github.com/boltdb/bolt]$ 

benbjohnson added a commit to benbjohnson/bolt that referenced this issue Mar 13, 2014
@tv42 reported that creating a cursor on an empty bucket and then calling
Cursor.Last() causes an index out of range error and panics. This commit
adds a check for the page's item count being greater than zero.

Fixes boltdb#63.
@benbjohnson
Copy link
Member

@tv42 Thanks for finding this and providing a test. It was just a simple lower bounds checking bug.

heyitsanthony pushed a commit to heyitsanthony/bolt that referenced this issue Nov 16, 2017
Add 'boltcli' to the list of projects that use Bolt
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants