0
0

+ make copies of the []bytes returned by goleveldb

- The byte strings returned by goleveldb aren't necessarily safe.  See
    the following google group thread:

    https://groups.google.com/forum/#!topic/bleve/aHZ8gmihLiY

    This code change is based on the gist created here:

    https://groups.google.com/forum/#!topic/bleve/aHZ8gmihLiY
This commit is contained in:
indraniel 2015-03-25 12:03:59 -05:00
parent 8e1c75a1cf
commit 3a70401835
2 changed files with 14 additions and 4 deletions

View File

@ -59,11 +59,17 @@ func (ldi *Iterator) Current() ([]byte, []byte, bool) {
}
func (ldi *Iterator) Key() []byte {
return ldi.iterator.Key()
k := ldi.iterator.Key()
rv := make([]byte, len(k))
copy(rv, k)
return rv
}
func (ldi *Iterator) Value() []byte {
return ldi.iterator.Value()
v := ldi.iterator.Value()
rv := make([]byte, len(v))
copy(rv, v)
return rv
}
func (ldi *Iterator) Valid() bool {

View File

@ -52,7 +52,9 @@ func (ldbs *Store) get(key []byte) ([]byte, error) {
if err == leveldb.ErrNotFound {
return nil, nil
}
return b, err
rv := make([]byte, len(b))
copy(rv, b)
return rv, err
}
func (ldbs *Store) getWithSnapshot(key []byte, snapshot *leveldb.Snapshot) ([]byte, error) {
@ -61,7 +63,9 @@ func (ldbs *Store) getWithSnapshot(key []byte, snapshot *leveldb.Snapshot) ([]by
if err == leveldb.ErrNotFound {
return nil, nil
}
return b, err
rv := make([]byte, len(b))
copy(rv, b)
return rv, err
}
func (ldbs *Store) set(key, val []byte) error {