Skip to content

Commit bce1d82

Browse files
committed
lnwire: fix MilliSatoshi Record Size
In this commit we start validating TLV blobs when we decode them. This exposed a bug where the incorrect size was being used for the MilliSatoshi TLV record type. So this is fixed here too.
1 parent 427ff5e commit bce1d82

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

channeldb/migration/lnwire21/msat.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ func (m MilliSatoshi) String() string {
5555
// Record returns a TLV record that can be used to encode/decode a MilliSatoshi
5656
// to/from a TLV stream.
5757
func (m *MilliSatoshi) Record() tlv.Record {
58+
msat := uint64(*m)
59+
5860
return tlv.MakeDynamicRecord(
59-
0, m, tlv.SizeBigSize(m), encodeMilliSatoshis,
61+
0, m, tlv.SizeBigSize(&msat), encodeMilliSatoshis,
6062
decodeMilliSatoshis,
6163
)
6264
}

lnwire/extra_bytes.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ func (e *ExtraOpaqueData) Decode(r io.Reader) error {
6060
*e = make([]byte, 0)
6161
}
6262

63+
tlvStream, err := tlv.NewStream()
64+
if err != nil {
65+
return err
66+
}
67+
68+
// Ensure that the TLV stream is valid by attempting to decode it.
69+
_, err = tlvStream.DecodeWithParsedTypesP2P(bytes.NewReader(*e))
70+
if err != nil {
71+
return fmt.Errorf("invalid TLV stream: %w: %v", err, *e)
72+
}
73+
6374
return nil
6475
}
6576

lnwire/msat.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ func (m MilliSatoshi) String() string {
5555
// Record returns a TLV record that can be used to encode/decode a MilliSatoshi
5656
// to/from a TLV stream.
5757
func (m *MilliSatoshi) Record() tlv.Record {
58+
msat := uint64(*m)
59+
5860
return tlv.MakeDynamicRecord(
59-
0, m, tlv.SizeBigSize(m), encodeMilliSatoshis,
61+
0, m, tlv.SizeBigSize(&msat), encodeMilliSatoshis,
6062
decodeMilliSatoshis,
6163
)
6264
}

0 commit comments

Comments
 (0)