Commit e3951368 authored by Steven Allen's avatar Steven Allen

forbid empty multiaddrs

fixes #104
parent fddba870
......@@ -20,6 +20,10 @@ func stringToBytes(s string) ([]byte, error) {
// consume first empty elem
sp = sp[1:]
if len(sp) == 0 {
return nil, fmt.Errorf("failed to parse multiaddr %q: empty multiaddr", s)
}
for len(sp) > 0 {
name := sp[0]
p := ProtocolWithName(name)
......@@ -58,6 +62,9 @@ func stringToBytes(s string) ([]byte, error) {
}
func validateBytes(b []byte) (err error) {
if len(b) == 0 {
return fmt.Errorf("empty multiaddr")
}
for len(b) > 0 {
code, n, err := ReadVarintCode(b)
if err != nil {
......@@ -136,6 +143,9 @@ func readComponent(b []byte) (int, Component, error) {
}
func bytesToString(b []byte) (ret string, err error) {
if len(b) == 0 {
return "", fmt.Errorf("empty multiaddr")
}
var buf strings.Builder
for len(b) > 0 {
......
......@@ -159,6 +159,10 @@ func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr {
return &multiaddr{bytes: cpy}
}
if i == 0 {
return nil
}
ma, err := NewMultiaddr(s1[:i])
if err != nil {
panic("Multiaddr.Decapsulate incorrect byte boundaries.")
......
......@@ -377,8 +377,8 @@ func TestEncapsulate(t *testing.T) {
m4, _ := NewMultiaddr("/ip4/127.0.0.1")
d := c.Decapsulate(m4)
if s := d.String(); s != "" {
t.Error("decapsulate /ip4 failed.", "/", s)
if d != nil {
t.Error("decapsulate /ip4 failed: ", d)
}
}
......@@ -582,11 +582,11 @@ func TestBinaryMarshaler(t *testing.T) {
t.Fatal(err)
}
addr2 := newMultiaddr(t, "")
var addr2 multiaddr
if err = addr2.UnmarshalBinary(b); err != nil {
t.Fatal(err)
}
if !addr.Equal(addr2) {
if !addr.Equal(&addr2) {
t.Error("expected equal addresses in circular marshaling test")
}
}
......@@ -598,11 +598,11 @@ func TestTextMarshaler(t *testing.T) {
t.Fatal(err)
}
addr2 := newMultiaddr(t, "")
var addr2 multiaddr
if err = addr2.UnmarshalText(b); err != nil {
t.Fatal(err)
}
if !addr.Equal(addr2) {
if !addr.Equal(&addr2) {
t.Error("expected equal addresses in circular marshaling test")
}
}
......@@ -614,11 +614,11 @@ func TestJSONMarshaler(t *testing.T) {
t.Fatal(err)
}
addr2 := newMultiaddr(t, "")
var addr2 multiaddr
if err = addr2.UnmarshalJSON(b); err != nil {
t.Fatal(err)
}
if !addr.Equal(addr2) {
if !addr.Equal(&addr2) {
t.Error("expected equal addresses in circular marshaling test")
}
}
......
......@@ -16,7 +16,6 @@ func TestSplitFirstLast(t *testing.T) {
[]string{ipStr, tcpStr, ipfsStr},
[]string{ipStr, tcpStr},
[]string{ipStr},
[]string{},
} {
addr := StringCast(strings.Join(x, ""))
head, tail := SplitFirst(addr)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment