From a17ace35cc760a2698645c09868f9050fa219f57 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Fri, 11 Mar 2022 11:27:13 +1100 Subject: [PATCH] fix: use protowire for Links bytes decoding --- compat_test.go | 48 ++++++++++++++++++++++++++++++++---------------- unmarshal.go | 5 ++--- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/compat_test.go b/compat_test.go index 3cc4307..fa3dedf 100644 --- a/compat_test.go +++ b/compat_test.go @@ -32,6 +32,7 @@ type testCase struct { expectedForm string encodeError string decodeError string + testEncode bool } var testCases = []testCase{ @@ -64,6 +65,13 @@ var testCases = []testCase{ }`, encodeError: "missing required fields: Links", }, + { + name: "Data some, short", + node: &pbNode{data: dataSome}, + expectedBytes: "0a0500010203", + decodeError: "unexpected EOF", + encodeError: "missing required fields: Links", + }, { name: "Links zero", node: &pbNode{links: []pbLink{}}, @@ -113,6 +121,12 @@ var testCases = []testCase{ }`, }, { + name: "Links Hash some, short", + node: &pbNode{links: []pbLink{{hash: acid}}}, + expectedBytes: "120b0a090155000500010203", + decodeError: "unexpected EOF", + testEncode: false, + }, { name: "Links Name zero", node: &pbNode{links: []pbLink{{name: zeroName, hasName: true}}}, expectedBytes: "12021200", @@ -211,24 +225,26 @@ func verifyRoundTrip(t *testing.T, tc testCase) { node := buildNode(*tc.node) actualBytes, err = nodeToString(t, node) - if tc.encodeError != "" { - if err != nil { - if !strings.Contains(err.Error(), tc.encodeError) { - t.Fatalf("got unexpeced encode error: [%v] (expected [%v])", err.Error(), tc.encodeError) + if tc.testEncode { + if tc.encodeError != "" { + if err != nil { + if !strings.Contains(err.Error(), tc.encodeError) { + t.Fatalf("got unexpeced encode error: [%v] (expected [%v])", err.Error(), tc.encodeError) + } + } else { + t.Fatalf("did not get expected encode error: %v", tc.encodeError) } } else { - t.Fatalf("did not get expected encode error: %v", tc.encodeError) - } - } else { - if err != nil { - t.Fatal(err) - } else { - if actualBytes != tc.expectedBytes { - t.Logf( - "Expected bytes: [%v]\nGot: [%v]\n", - tc.expectedBytes, - actualBytes) - t.Error("Did not match") + if err != nil { + t.Fatal(err) + } else { + if actualBytes != tc.expectedBytes { + t.Logf( + "Expected bytes: [%v]\nGot: [%v]\n", + tc.expectedBytes, + actualBytes) + t.Error("Did not match") + } } } } diff --git a/unmarshal.go b/unmarshal.go index 4049859..58e245b 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -98,7 +98,7 @@ func DecodeBytes(na ipld.NodeAssembler, src []byte) error { haveData = true case 2: - bytesLen, n := protowire.ConsumeVarint(remaining) + chunk, n := protowire.ConsumeBytes(remaining) if n < 0 { return protowire.ParseError(n) } @@ -123,10 +123,9 @@ func DecodeBytes(na ipld.NodeAssembler, src []byte) error { if err != nil { return err } - if err := unmarshalLink(remaining[:bytesLen], curLink); err != nil { + if err := unmarshalLink(chunk, curLink); err != nil { return err } - remaining = remaining[bytesLen:] if err := curLink.Finish(); err != nil { return err }