go-ssb icon indicating copy to clipboard operation
go-ssb copied to clipboard

Illegal slice reuse in Badger code

Open boreq opened this issue 1 year ago • 2 comments

In my opinion code such as this is incorrect: https://github.com/ssbc/go-ssb/blob/2cdd828cd8c898441c18b4b05790dc17007519ee/graph/builder.go#L105-L112

Reasoning:

https://pkg.go.dev/github.com/dgraph-io/badger/v3#Item.Key:

Key is only valid as long as item is valid, or transaction is valid. If you need to use it outside its validity, please use KeyCopy.

https://pkg.go.dev/github.com/dgraph-io/badger/v3#Iterator.Item:

Item returns pointer to the current key-value pair. This item is only valid until it.Next() gets called.

In my opinion in this situation the item is only valid until the next iteration therefore key is illegaly reused. KeyCopy should be used instead. It is possible that txn.Delete(...) will remove other data. I imagine this is the case in other places in the code as well.

boreq avatar Feb 13 '23 18:02 boreq