Skip to content

Commit 5b1de2f

Browse files
authored
Merge pull request #105 from multiformats/feat/no-empty-multiadd
forbid empty multiaddrs
2 parents fddba87 + 020c4d1 commit 5b1de2f

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

codec.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func stringToBytes(s string) ([]byte, error) {
2020
// consume first empty elem
2121
sp = sp[1:]
2222

23+
if len(sp) == 0 {
24+
return nil, fmt.Errorf("failed to parse multiaddr %q: empty multiaddr", s)
25+
}
26+
2327
for len(sp) > 0 {
2428
name := sp[0]
2529
p := ProtocolWithName(name)
@@ -58,6 +62,9 @@ func stringToBytes(s string) ([]byte, error) {
5862
}
5963

6064
func validateBytes(b []byte) (err error) {
65+
if len(b) == 0 {
66+
return fmt.Errorf("empty multiaddr")
67+
}
6168
for len(b) > 0 {
6269
code, n, err := ReadVarintCode(b)
6370
if err != nil {
@@ -136,6 +143,9 @@ func readComponent(b []byte) (int, Component, error) {
136143
}
137144

138145
func bytesToString(b []byte) (ret string, err error) {
146+
if len(b) == 0 {
147+
return "", fmt.Errorf("empty multiaddr")
148+
}
139149
var buf strings.Builder
140150

141151
for len(b) > 0 {

multiaddr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr {
159159
return &multiaddr{bytes: cpy}
160160
}
161161

162+
if i == 0 {
163+
return nil
164+
}
165+
162166
ma, err := NewMultiaddr(s1[:i])
163167
if err != nil {
164168
panic("Multiaddr.Decapsulate incorrect byte boundaries.")

multiaddr_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func TestConstructFails(t *testing.T) {
7474
"/unix",
7575
"/ip4/1.2.3.4/tcp/80/unix",
7676
"/ip4/127.0.0.1/tcp/9090/http/p2p-webcrt-direct",
77+
"/",
78+
"",
7779
}
7880

7981
for _, a := range cases {
@@ -83,6 +85,13 @@ func TestConstructFails(t *testing.T) {
8385
}
8486
}
8587

88+
func TestEmptyMultiaddr(t *testing.T) {
89+
_, err := NewMultiaddrBytes([]byte{})
90+
if err == nil {
91+
t.Fatal("should have failed to parse empty multiaddr")
92+
}
93+
}
94+
8695
func TestConstructSucceeds(t *testing.T) {
8796
cases := []string{
8897
"/ip4/1.2.3.4",
@@ -377,8 +386,8 @@ func TestEncapsulate(t *testing.T) {
377386

378387
m4, _ := NewMultiaddr("/ip4/127.0.0.1")
379388
d := c.Decapsulate(m4)
380-
if s := d.String(); s != "" {
381-
t.Error("decapsulate /ip4 failed.", "/", s)
389+
if d != nil {
390+
t.Error("decapsulate /ip4 failed: ", d)
382391
}
383392
}
384393

@@ -582,11 +591,11 @@ func TestBinaryMarshaler(t *testing.T) {
582591
t.Fatal(err)
583592
}
584593

585-
addr2 := newMultiaddr(t, "")
594+
var addr2 multiaddr
586595
if err = addr2.UnmarshalBinary(b); err != nil {
587596
t.Fatal(err)
588597
}
589-
if !addr.Equal(addr2) {
598+
if !addr.Equal(&addr2) {
590599
t.Error("expected equal addresses in circular marshaling test")
591600
}
592601
}
@@ -598,11 +607,11 @@ func TestTextMarshaler(t *testing.T) {
598607
t.Fatal(err)
599608
}
600609

601-
addr2 := newMultiaddr(t, "")
610+
var addr2 multiaddr
602611
if err = addr2.UnmarshalText(b); err != nil {
603612
t.Fatal(err)
604613
}
605-
if !addr.Equal(addr2) {
614+
if !addr.Equal(&addr2) {
606615
t.Error("expected equal addresses in circular marshaling test")
607616
}
608617
}
@@ -614,11 +623,11 @@ func TestJSONMarshaler(t *testing.T) {
614623
t.Fatal(err)
615624
}
616625

617-
addr2 := newMultiaddr(t, "")
626+
var addr2 multiaddr
618627
if err = addr2.UnmarshalJSON(b); err != nil {
619628
t.Fatal(err)
620629
}
621-
if !addr.Equal(addr2) {
630+
if !addr.Equal(&addr2) {
622631
t.Error("expected equal addresses in circular marshaling test")
623632
}
624633
}

util_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ func TestSplitFirstLast(t *testing.T) {
1616
[]string{ipStr, tcpStr, ipfsStr},
1717
[]string{ipStr, tcpStr},
1818
[]string{ipStr},
19-
[]string{},
2019
} {
2120
addr := StringCast(strings.Join(x, ""))
2221
head, tail := SplitFirst(addr)

0 commit comments

Comments
 (0)