mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 17:36:38 +08:00
Merge pull request #1282 from travisperson/bug/panic-on-empty-path
Bug/panic on empty path
This commit is contained in:
@ -29,6 +29,11 @@ func Resolve(ctx context.Context, n *IpfsNode, p path.Path) (*merkledag.Node, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
seg := p.Segments()
|
seg := p.Segments()
|
||||||
|
|
||||||
|
if len(seg) < 2 || seg[1] == "" { // just "/<protocol/>" without further segments
|
||||||
|
return nil, path.ErrNoComponents
|
||||||
|
}
|
||||||
|
|
||||||
extensions := seg[2:]
|
extensions := seg[2:]
|
||||||
resolvable, err := path.FromSegments("/", seg[0], seg[1])
|
resolvable, err := path.FromSegments("/", seg[0], seg[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
25
core/pathresolver_test.go
Normal file
25
core/pathresolver_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
path "github.com/ipfs/go-ipfs/path"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveNoComponents(t *testing.T) {
|
||||||
|
n, err := NewMockNode()
|
||||||
|
if n == nil || err != nil {
|
||||||
|
t.Fatal("Should have constructed a mock node", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = Resolve(n.Context(), n, path.Path("/ipns/"))
|
||||||
|
if err != path.ErrNoComponents {
|
||||||
|
t.Fatal("Should error with no components (/ipns/).", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = Resolve(n.Context(), n, path.Path("/ipfs/"))
|
||||||
|
if err != path.ErrNoComponents {
|
||||||
|
t.Fatal("Should error with no components (/ipfs/).", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,6 +4,7 @@ package path
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
"errors"
|
||||||
|
|
||||||
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
||||||
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||||
@ -14,6 +15,10 @@ import (
|
|||||||
|
|
||||||
var log = u.Logger("path")
|
var log = u.Logger("path")
|
||||||
|
|
||||||
|
// Paths after a protocol must contain at least one component
|
||||||
|
var ErrNoComponents = errors.New(
|
||||||
|
"path must contain at least one component")
|
||||||
|
|
||||||
// ErrNoLink is returned when a link is not found in a path
|
// ErrNoLink is returned when a link is not found in a path
|
||||||
type ErrNoLink struct {
|
type ErrNoLink struct {
|
||||||
name string
|
name string
|
||||||
@ -43,7 +48,7 @@ func SplitAbsPath(fpath Path) (mh.Multihash, []string, error) {
|
|||||||
|
|
||||||
// if nothing, bail.
|
// if nothing, bail.
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
return nil, nil, fmt.Errorf("ipfs path must contain at least one component")
|
return nil, nil, ErrNoComponents
|
||||||
}
|
}
|
||||||
|
|
||||||
// first element in the path is a b58 hash (for now)
|
// first element in the path is a b58 hash (for now)
|
||||||
|
Reference in New Issue
Block a user