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 16 commits into
base: master
Choose a base branch
from
Open
6 changes: 6 additions & 0 deletions BREAKING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Breaking changes in the large refactor of go-multiaddr v0.15

- There is no `Multiaddr` interface type.
- Multiaddr is now a concrete type. Not an interface.
- Empty Multiaddrs/ should be checked with `.Empty()`, not `== nil`
- Components do not implement `Multiaddr` as there is no `Multiaddr` to implement.
117 changes: 40 additions & 77 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
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
}

Check warning on line 59 in codec.go

View check run for this annotation

Codecov / codecov/patch

codec.go#L58-L59

Added lines #L58 - L59 were not covered by tests
if p.Size < 0 { // varint size.
_, _ = b.Write(varint.ToUvarint(uint64(len(a))))
}
Expand All @@ -63,51 +67,6 @@
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 @@
}

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
}

Check warning on line 100 in codec.go

View check run for this annotation

Codecov / codecov/patch

codec.go#L99-L100

Added lines #L99 - L100 were not covered by tests
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
Loading