Skip to content

Commit

Permalink
ipfs: multiaddr - don't overwrite value when input is bad
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Apr 3, 2023
1 parent 81da6bd commit c8f7bf5
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions internal/filesystem/ipfs/multiaddr.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package ipfs

import (
"bytes"
"encoding/json"
"fmt"

"github.com/multiformats/go-multiaddr"
)
Expand Down Expand Up @@ -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
}

0 comments on commit c8f7bf5

Please sign in to comment.