Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
e3951368
Commit
e3951368
authored
May 20, 2019
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
forbid empty multiaddrs
fixes #104
parent
fddba870
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
22 additions
and
9 deletions
+22
-9
codec.go
codec.go
+10
-0
multiaddr.go
multiaddr.go
+4
-0
multiaddr_test.go
multiaddr_test.go
+8
-8
util_test.go
util_test.go
+0
-1
No files found.
codec.go
View file @
e3951368
...
...
@@ -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
{
...
...
multiaddr.go
View file @
e3951368
...
...
@@ -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."
)
...
...
multiaddr_test.go
View file @
e3951368
...
...
@@ -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
:=
newM
ultiaddr
(
t
,
""
)
var
addr2
m
ultiaddr
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
:=
newM
ultiaddr
(
t
,
""
)
var
addr2
m
ultiaddr
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
:=
newM
ultiaddr
(
t
,
""
)
var
addr2
m
ultiaddr
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"
)
}
}
...
...
util_test.go
View file @
e3951368
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment