Skip to content

Commit

Permalink
fix(cosmos): Prevent promotion of null arguments to "" in vstorag…
Browse files Browse the repository at this point in the history
…e handlers
  • Loading branch information
gibson042 committed Jul 18, 2023
1 parent 86a74d8 commit ce12eca
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions golang/cosmos/x/vstorage/vstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ func unmarshalString(jsonText json.RawMessage) (string, error) {
return *str, nil
}

func unmarshalSinglePathFromArgs(args []json.RawMessage, path *string) error {
func unmarshalSinglePathFromArgs(args []json.RawMessage) (string, error) {
if len(args) == 0 {
return fmt.Errorf("missing 'path' argument")
return "", fmt.Errorf("missing 'path' argument")
} else if len(args) != 1 {
return "", fmt.Errorf("extra arguments after 'path'")
}
if len(args) != 1 {
return fmt.Errorf("extra arguments after 'path'")
}
return json.Unmarshal(args[0], path)
return unmarshalString(args[0])
}

func unmarshalPathsFromArgs(args []json.RawMessage) ([]string, error) {
Expand Down Expand Up @@ -157,10 +156,9 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s

case "get":
// Note that "get" does not (currently) unwrap a StreamCell.
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
path, err := unmarshalSinglePathFromArgs(msg.Args)
if err != nil {
return
return "", err
}

entry := keeper.GetEntry(cctx.Context, path)
Expand All @@ -174,10 +172,9 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
return string(bz), nil

case "getStoreKey":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
path, err := unmarshalSinglePathFromArgs(msg.Args)
if err != nil {
return
return "", err
}
value := vstorageStoreKey{
StoreName: keeper.GetStoreName(),
Expand All @@ -192,10 +189,9 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
return string(bz), nil

case "has":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
path, err := unmarshalSinglePathFromArgs(msg.Args)
if err != nil {
return
return "", err
}
value := keeper.HasStorage(cctx.Context, path)
if !value {
Expand All @@ -205,10 +201,9 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s

// TODO: "keys" is deprecated
case "children", "keys":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
path, err := unmarshalSinglePathFromArgs(msg.Args)
if err != nil {
return
return "", err
}
children := keeper.GetChildren(cctx.Context, path)
if children.Children == nil {
Expand All @@ -221,10 +216,9 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
return string(bytes), nil

case "entries":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
path, err := unmarshalSinglePathFromArgs(msg.Args)
if err != nil {
return
return "", err
}
children := keeper.GetChildren(cctx.Context, path)
entries := make([][]interface{}, len(children.Children))
Expand All @@ -243,10 +237,9 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
return string(bytes), nil

case "values":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
path, err := unmarshalSinglePathFromArgs(msg.Args)
if err != nil {
return
return "", err
}
children := keeper.GetChildren(cctx.Context, path)
vals := make([]string, len(children.Children))
Expand All @@ -260,10 +253,9 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
return string(bytes), nil

case "size":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
path, err := unmarshalSinglePathFromArgs(msg.Args)
if err != nil {
return
return "", err
}
children := keeper.GetChildren(cctx.Context, path)
if children.Children == nil {
Expand Down

0 comments on commit ce12eca

Please sign in to comment.