From 1fb05e0436aebd604f861e00db98e8344adc2ee7 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 10 Nov 2017 19:36:05 -0800 Subject: [PATCH] channeldb: properly craft key for reading/writing channel commitments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this commit, we fix an existing bug that arose due to incorrectly crafting the key we use to store channel commitments. Before this commit, we tried to copy to a slice that hadn’t been allocated yet. As a result, the key would only have the 0x00 or 0x01 as its value. We fix this by properly crafting the key using the built-in append function. --- channeldb/channel.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index e01a035d8..174f7698f 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -1475,12 +1475,11 @@ func serializeChanCommit(w io.Writer, c *ChannelCommitment) error { func putChanCommitment(chanBucket *bolt.Bucket, c *ChannelCommitment, local bool) error { - var key []byte - copy(key[:], chanCommitmentKey) + var commitKey []byte if local { - key = append(key, byte(0x00)) + commitKey = append(chanCommitmentKey, byte(0x00)) } else { - key = append(key, byte(0x01)) + commitKey = append(chanCommitmentKey, byte(0x01)) } var b bytes.Buffer @@ -1488,7 +1487,7 @@ func putChanCommitment(chanBucket *bolt.Bucket, c *ChannelCommitment, return err } - return chanBucket.Put(key, b.Bytes()) + return chanBucket.Put(commitKey, b.Bytes()) } func putChanCommitments(chanBucket *bolt.Bucket, channel *OpenChannel) error { @@ -1581,15 +1580,14 @@ func deserializeChanCommit(r io.Reader) (ChannelCommitment, error) { } func fetchChanCommitment(chanBucket *bolt.Bucket, local bool) (ChannelCommitment, error) { - var key []byte - copy(key[:], chanCommitmentKey) + var commitKey []byte if local { - key = append(key, byte(0x00)) + commitKey = append(chanCommitmentKey, byte(0x00)) } else { - key = append(key, byte(0x01)) + commitKey = append(chanCommitmentKey, byte(0x01)) } - commitBytes := chanBucket.Get(key) + commitBytes := chanBucket.Get(commitKey) if commitBytes == nil { return ChannelCommitment{}, ErrNoCommitmentsFound }