Skip to content

Commit adbbabe

Browse files
authored
Merge pull request #57 from astromechza/patch-1
Fixed incorrect truncated tlv error that prevents reading empty (0 byte) TLVs
2 parents 152f707 + 2cb1b5b commit adbbabe

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

tlv.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func SplitTLVs(raw []byte) ([]TLV, error) {
5555
tlv := TLV{
5656
Type: PP2Type(raw[i]),
5757
}
58-
if len(raw)-i <= 3 {
58+
if len(raw)-i <= 2 {
5959
return nil, ErrTruncatedTLV
6060
}
6161
tlvLen := int(binary.BigEndian.Uint16(raw[i+1 : i+3])) // Max length = 65K

tlv_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ var invalidTLVTests = []struct {
6868
},
6969
}
7070

71+
func TestValid0Length(t *testing.T) {
72+
r := bufio.NewReader(bytes.NewReader(append(append(SIGV2, byte(PROXY), byte(TCPv4)), fixtureWithTLV(lengthV4Bytes, fixtureIPv4Address, []byte{byte(PP2_TYPE_MIN_CUSTOM), 0x00, 0x00})...)))
73+
h, err := Read(r)
74+
if err != nil {
75+
t.Fatalf("unexpected error: %v", err)
76+
}
77+
tlvs, err := h.TLVs()
78+
if err != nil {
79+
t.Fatalf("unexpected error: %v", err)
80+
}
81+
if len(tlvs) != 1 {
82+
t.Fatalf("expected 1 tlv, got %d", len(tlvs))
83+
}
84+
if len(tlvs[0].Value) != 0 {
85+
t.Fatalf("expected 0 byte tlv value, got %x", tlvs[0].Value)
86+
}
87+
}
88+
7189
func TestInvalidV2TLV(t *testing.T) {
7290
for _, tc := range invalidTLVTests {
7391
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)