From ce12eca496998d0180192a30331cf4426872ba8d Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 18 Jul 2023 19:21:25 -0400 Subject: [PATCH] fix(cosmos): Prevent promotion of `null` arguments to `""` in vstorage handlers --- golang/cosmos/x/vstorage/vstorage.go | 46 ++++++++++++---------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/golang/cosmos/x/vstorage/vstorage.go b/golang/cosmos/x/vstorage/vstorage.go index e82aa966c3a..f497a9cf4df 100644 --- a/golang/cosmos/x/vstorage/vstorage.go +++ b/golang/cosmos/x/vstorage/vstorage.go @@ -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) { @@ -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) @@ -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(), @@ -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 { @@ -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 { @@ -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)) @@ -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)) @@ -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 {