asdine / storm

Simple and powerful toolkit for BoltDB
MIT License
2.06k stars 139 forks source link

scan empty db panic #257

Closed navono closed 5 years ago

navono commented 5 years ago

The root cause is in sacn.go, not check the returned value:

func (n *node) cursor(tx *bolt.Tx) *bolt.Cursor {

    var c *bolt.Cursor

    if len(n.rootBucket) > 0 {
        c = n.GetBucket(tx).Cursor()
    } else {
        c = tx.Cursor()
    }

    return c
}

If bucket not exists, GetBucket will return nil.

asdine commented 5 years ago

Could you share a code example so I can reproduce it?

navono commented 5 years ago

storm : v2.1.2 golang: v1.13.1

package main

import (
  "fmt"
  "github.com/asdine/storm"
)

func main()  {
  db , err := storm.Open("./db")
  if err != nil {
    return
  }

  repo := db.From("repo")
  // If store some data in `repo` bucket, it will not panic
  nodes := repo.PrefixScan("prj")
  fmt.Print(nodes)
}

If db is empty, it will panic, but if db alreay holds some data it's totaly OK.

I don't this usage is rightful or not, in some cases I use this to check lists from specified node.

asdine commented 5 years ago

Thank you, it should be good now. It will be shipped as part of the next release