client/db/bolt: fix data corruption from reused buffers#561
Merged
Conversation
The returned byte slice from bbolt.(*Bucket).Get is not valid outside of the transaction where it is used. This change fixes several places where the []byte was used without copying it, resulting in corrupted data. Add the client/db/bolt.getCopy function to load a value and make a copy. If the loaded value is an empty slice, it returns nil. Thus, to identify a non-existent value, use Bucket.Get directly instead. getCopy covers most of the cases where we want to get the value in a slice that is safe to use outside of a transaction, and when we do not wish to load an empty non-nil slice when nil was actually stored. Notably, this fixes decodeOrderBucket, and possibly filteredMatches, makeWallet, and NotificationsN.
buck54321
approved these changes
Jul 23, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The returned byte slice from
bbolt.(*Bucket).Getis not valid outside ofthe transaction where it is used. This change fixes several places
where the
[]bytewas used outside of the transaction without copying it,resulting in mysteriously corrupted data, including
ChangeCoin.Add the
client/db/bolt.getCopyfunction to load a value and make a copy.If the loaded value is an empty slice, it returns nil. Thus, to
identify a non-existent value, use
Bucket.Getdirectly instead.getCopycovers most of the cases where we want to get the value into aslice that is safe to use outside of a transaction, and when we do not
wish to load an empty non-nil slice when nil was actually stored.
Notably, this fixes
decodeOrderBucket,DecodeMatchProof, andmakeWallet>DecodeWallet(keyB).Likely resolves the "
btc coin 0000000000000000000000000000000000000000000000000000000000000000:0 not found" issue reported in #531 (review). I reproduced the issue with a different but equally impossible coin id of00000000000000000000000000000000000000000000002a000000516b444b63:131074after starting dexc with an active match inTakerSwapCastand following that with new matches.