Skip to content

Fix decode of modules.rpcResponse

Peter-Jan Brone requested to merge 3983-fix-decode-error-in-rpcwriteerror into master

MERGE REQUEST

Overview

This MR fixes the decoding of the modules.rpcResponse object in case an error occurred.

Scenario BEFORE FIX

  • error occurred and was written to the stream as an rpcResponse object with nested RPCError
  • during unmarshaling that error was returned as error of the unmarshal function
func (resp *rpcResponse) UnmarshalSia(r io.Reader) error {
	// NOTE: no allocation limit is required because this method is always
	// called via encoding.Unmarshal, which already imposes an allocation limit.
	d := encoding.NewDecoder(r, 0)
	if err := d.Decode(&resp.err); err != nil {
		return err

        // LOOK HERE
	} else if resp.err != nil {
		return resp.err
	}

	return d.Decode(resp.data)
}
  • this error was considered a panic
func (d *Decoder) decode(val reflect.Value) {
	// check for UnmarshalSia interface first
	if val.CanAddr() && val.Addr().CanInterface() {
		if u, ok := val.Addr().Interface().(SiaUnmarshaler); ok {
			err := u.UnmarshalSia(d)
			if err != nil {
                                // LOOK HERE
				panic(err)
			}
			return
		}
	}
  • however got recovered from
func (d *Decoder) Decode(v interface{}) (err error) {
	// v must be a pointer
	pval := reflect.ValueOf(v)
	if pval.Kind() != reflect.Ptr || pval.IsNil() {
		return errBadPointer
	}

	// catch decoding panics and convert them to errors
	// note that this allows us to skip boundary checks during decoding
        // LOOK HERE
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("could not decode type %s: %v", pval.Elem().Type().String(), r)
		}
	}()
  • resulting in errors always being prefixed with "could not decode type modules.rpcResponse:"

Example for Visual Changes

N/A

Checklist

Review and complete the checklist to ensure that the MR is complete before assigned to an approver.

  • All new methods or updated methods have clear docstrings
  • Testing added or updated for new methods
  • Any new packages are added to Makefile and .gitlab-ci.yml
  • API documentation updated for API updates
  • Module README.md updated for changes to workflow
  • Issue added to Sia-UI repo for new supporting features
  • Changelog File Created

Issues Closed

Closes #3983 (closed)

Edited by Peter-Jan Brone

Merge request reports