Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor code to make the API harder to misuse #261

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 40 additions & 77 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func stringToBytes(s string) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("failed to parse multiaddr %q: invalid value %q for protocol %s: %s", s, sp[0], p.Name, err)
}
err = p.Transcoder.ValidateBytes(a)
if err != nil {
return nil, err
}
if p.Size < 0 { // varint size.
_, _ = b.Write(varint.ToUvarint(uint64(len(a))))
}
Expand All @@ -63,51 +67,6 @@ func stringToBytes(s string) ([]byte, error) {
return b.Bytes(), nil
}

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 {
return err
}

b = b[n:]
p := ProtocolWithCode(code)
if p.Code == 0 {
return fmt.Errorf("no protocol with code %d", code)
}

if p.Size == 0 {
continue
}

n, size, err := sizeForAddr(p, b)
if err != nil {
return err
}

b = b[n:]

if len(b) < size || size < 0 {
return fmt.Errorf("invalid value for size %d", len(b))
}
if p.Path && len(b) != size {
return fmt.Errorf("invalid size of component for path protocol %d: expected %d", size, len(b))
}

err = p.Transcoder.ValidateBytes(b[:size])
if err != nil {
return err
}

b = b[size:]
}

return nil
}

func readComponent(b []byte) (int, Component, error) {
var offset int
code, n, err := ReadVarintCode(b)
Expand All @@ -122,60 +81,64 @@ func readComponent(b []byte) (int, Component, error) {
}

if p.Size == 0 {
return offset, Component{
bytes: b[:offset],
c, err := validateComponent(Component{
bytes: string(b[:offset]),
offset: offset,
protocol: p,
}, nil
}
})

n, size, err := sizeForAddr(p, b[offset:])
if err != nil {
return 0, Component{}, err
return offset, c, err
}

offset += n
var size int
if p.Size < 0 {
// varint
var n int
size, n, err = ReadVarintCode(b[offset:])
if err != nil {
return 0, Component{}, err
}
offset += n
} else {
// Size is in bits, but we operate on bytes
size = p.Size / 8
}

if len(b[offset:]) < size || size < 0 {
if len(b[offset:]) < size || size <= 0 {
return 0, Component{}, fmt.Errorf("invalid value for size %d", len(b[offset:]))
}

return offset + size, Component{
bytes: b[:offset+size],
c, err := validateComponent(Component{
bytes: string(b[:offset+size]),
protocol: p,
offset: offset,
}, nil
})

return offset + size, c, err
}

func bytesToString(b []byte) (ret string, err error) {
func readMultiaddr(b []byte) (int, Multiaddr, error) {
if len(b) == 0 {
return "", fmt.Errorf("empty multiaddr")
return 0, nil, fmt.Errorf("empty multiaddr")
}
var buf strings.Builder

var res Multiaddr
bytesRead := 0
sawPathComponent := false
for len(b) > 0 {
n, c, err := readComponent(b)
if err != nil {
return "", err
return 0, nil, err
}
b = b[n:]
c.writeTo(&buf)
}
bytesRead += n

return buf.String(), nil
}

func sizeForAddr(p Protocol, b []byte) (skip, size int, err error) {
switch {
case p.Size > 0:
return 0, (p.Size / 8), nil
case p.Size == 0:
return 0, 0, nil
default:
size, n, err := ReadVarintCode(b)
if err != nil {
return 0, 0, err
if sawPathComponent {
// It is an error to have another component after a path component.
return bytesRead, nil, fmt.Errorf("unexpected component after path component")
}
return n, size, nil
sawPathComponent = c.protocol.Path
res = append(res, c)
}
return bytesRead, res, nil
}
Loading