From c8f7bf554b8c69001ecdd3ce8d5329efcca2999a Mon Sep 17 00:00:00 2001 From: Dominic Della Valle Date: Sun, 26 Mar 2023 20:03:06 -0400 Subject: [PATCH] ipfs: multiaddr - don't overwrite value when input is bad --- internal/filesystem/ipfs/multiaddr.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/internal/filesystem/ipfs/multiaddr.go b/internal/filesystem/ipfs/multiaddr.go index 9185ac59..a3ca383b 100644 --- a/internal/filesystem/ipfs/multiaddr.go +++ b/internal/filesystem/ipfs/multiaddr.go @@ -1,9 +1,7 @@ package ipfs import ( - "bytes" "encoding/json" - "fmt" "github.com/multiformats/go-multiaddr" ) @@ -41,17 +39,15 @@ func (mc *multiaddrContainer) MarshalJSON() ([]byte, error) { return []byte("null"), nil } -func (mc *multiaddrContainer) UnmarshalJSON(b []byte) (err error) { - if len(b) < 2 || bytes.Equal(b, []byte("{}")) { - return fmt.Errorf("response was empty or short: `%v`", b) - } - if bytes.Equal(b, []byte("null")) { - return - } +func (mc *multiaddrContainer) UnmarshalJSON(b []byte) error { var mcStr string - if err = json.Unmarshal(b, &mcStr); err != nil { - return + if err := json.Unmarshal(b, &mcStr); err != nil { + return err } - mc.Multiaddr, err = multiaddr.NewMultiaddr(mcStr) - return + maddr, err := multiaddr.NewMultiaddr(mcStr) + if err != nil { + return err + } + mc.Multiaddr = maddr + return nil }