Commit e4e61398 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

decoder: Try to provide more explicit error context on invalid data (#29)

This partly reverts 572d0a6a and just changes %t -> %T so that printing
does not enter infinite recursion and just print what we need: type of
an object (%t is for true/false).

Along the way I've added "pickle:" prefix to errors.

In the future maybe it would make sense to create dedicated DecodeError
which would contain position, current opcode, and error message itself.
This way error reporting would be more structured and useful for users.
parent 51c8a115
...@@ -87,7 +87,6 @@ var errNotImplemented = errors.New("unimplemented opcode") ...@@ -87,7 +87,6 @@ var errNotImplemented = errors.New("unimplemented opcode")
var ErrInvalidPickleVersion = errors.New("invalid pickle version") var ErrInvalidPickleVersion = errors.New("invalid pickle version")
var errNoMarker = errors.New("no marker in stack") var errNoMarker = errors.New("no marker in stack")
var errStackUnderflow = errors.New("pickle: stack underflow") var errStackUnderflow = errors.New("pickle: stack underflow")
var errMalformedData = errors.New("pickle: malformed data")
type OpcodeError struct { type OpcodeError struct {
Key byte Key byte
...@@ -618,7 +617,7 @@ func (d *Decoder) loadAppend() error { ...@@ -618,7 +617,7 @@ func (d *Decoder) loadAppend() error {
l := l.([]interface{}) l := l.([]interface{})
d.stack[len(d.stack)-1] = append(l, v) d.stack[len(d.stack)-1] = append(l, v)
default: default:
return errMalformedData return fmt.Errorf("pickle: loadAppend: expected a list, got %T", l)
} }
return nil return nil
} }
...@@ -683,7 +682,7 @@ func (d *Decoder) loadAppends() error { ...@@ -683,7 +682,7 @@ func (d *Decoder) loadAppends() error {
} }
d.stack = append(d.stack[:k-1], l) d.stack = append(d.stack[:k-1], l)
default: default:
return errMalformedData return fmt.Errorf("pickle: loadAppends: expected a list, got %T", l)
} }
return nil return nil
} }
...@@ -834,7 +833,7 @@ func (d *Decoder) loadSetItem() error { ...@@ -834,7 +833,7 @@ func (d *Decoder) loadSetItem() error {
m := m.(map[interface{}]interface{}) m := m.(map[interface{}]interface{})
m[k] = v m[k] = v
default: default:
return errMalformedData return fmt.Errorf("pickle: loadSetItem: expected a map, got %T", m)
} }
return nil return nil
} }
...@@ -856,7 +855,7 @@ func (d *Decoder) loadSetItems() error { ...@@ -856,7 +855,7 @@ func (d *Decoder) loadSetItems() error {
} }
d.stack = append(d.stack[:k-1], m) d.stack = append(d.stack[:k-1], m)
default: default:
return errMalformedData return fmt.Errorf("pickle: loadSetItems: expected a map, got %T", m)
} }
return nil return nil
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment