From 741e81301d5ae70a0b750a1aabd5c6efee90181f Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 19 Sep 2023 14:04:50 +0200 Subject: [PATCH 1/3] golang: Enforce maximum decoding depth --- lib/xdrgen/generators/go.rb | 43 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/xdrgen/generators/go.rb b/lib/xdrgen/generators/go.rb index 16c9c6bb2..8c84c248c 100644 --- a/lib/xdrgen/generators/go.rb +++ b/lib/xdrgen/generators/go.rb @@ -536,7 +536,11 @@ def check_error(str) def render_struct_decode_from_interface(out, struct) name = name(struct) out.puts "// DecodeFrom decodes this value using the Decoder." - out.puts "func (s *#{name}) DecodeFrom(d *xdr.Decoder) (int, error) {" + out.puts "func (s *#{name}) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) {" + out.puts " if maxDepth == 0 {" + out.puts " return 0, errors.New(\"maximum decoding depth reached\")" + out.puts " }" + out.puts " maxDepth -= 1" out.puts " var err error" out.puts " var n, nTmp int" declared_variables = [] @@ -552,7 +556,11 @@ def render_struct_decode_from_interface(out, struct) def render_union_decode_from_interface(out, union) name = name(union) out.puts "// DecodeFrom decodes this value using the Decoder." - out.puts "func (u *#{name}) DecodeFrom(d *xdr.Decoder) (int, error) {" + out.puts "func (u *#{name}) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) {" + out.puts " if maxDepth == 0 {" + out.puts " return 0, errors.New(\"decoding #{name}: maximum decoding depth reached\")" + out.puts " }" + out.puts " maxDepth -= 1" out.puts " var err error" out.puts " var n, nTmp int" render_decode_from_body(out, "u.#{name(union.discriminant)}", union.discriminant.type, declared_variables: [], self_encode: false) @@ -581,7 +589,11 @@ def render_enum_decode_from_interface(out, typedef) type = typedef out.puts <<-EOS.strip_heredoc // DecodeFrom decodes this value using the Decoder. - func (e *#{name}) DecodeFrom(d *xdr.Decoder) (int, error) { + func (e *#{name}) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding #{name}: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding #{name}: %s", err) @@ -599,7 +611,11 @@ def render_typedef_decode_from_interface(out, typedef) name = name(typedef) type = typedef.declaration.type out.puts "// DecodeFrom decodes this value using the Decoder." - out.puts "func (s *#{name}) DecodeFrom(d *xdr.Decoder) (int, error) {" + out.puts "func (s *#{name}) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) {" + out.puts " if maxDepth == 0 {" + out.puts " return 0, errors.New(\"decoding #{name}: maximum decoding depth reached\")" + out.puts " }" + out.puts " maxDepth -= 1" out.puts " var err error" out.puts " var n, nTmp int" var = "s" @@ -692,7 +708,7 @@ def render_decode_from_body(out, var, type, declared_variables:, self_encode:) out.puts " #{var} = new(#{name type.resolved_type.declaration.type})" end var = "(*#{name type})(#{var})" if self_encode - out.puts " nTmp, err = #{var}.DecodeFrom(d)" + out.puts " nTmp, err = #{var}.DecodeFrom(d, maxDepth)" out.puts tail if optional_within out.puts " }" @@ -709,7 +725,7 @@ def render_decode_from_body(out, var, type, declared_variables:, self_encode:) out.puts " if eb {" var = "(*#{element_var})" end - out.puts " nTmp, err = #{element_var}.DecodeFrom(d)" + out.puts " nTmp, err = #{element_var}.DecodeFrom(d, maxDepth)" out.puts tail if optional_within out.puts " }" @@ -739,7 +755,7 @@ def render_decode_from_body(out, var, type, declared_variables:, self_encode:) out.puts " #{element_var} = new(#{name type.resolved_type.declaration.type})" var = "(*#{element_var})" end - out.puts " nTmp, err = #{element_var}.DecodeFrom(d)" + out.puts " nTmp, err = #{element_var}.DecodeFrom(d, maxDepth)" out.puts tail if optional_within out.puts " }" @@ -751,13 +767,13 @@ def render_decode_from_body(out, var, type, declared_variables:, self_encode:) end when AST::Definitions::Base if self_encode - out.puts " nTmp, err = #{name type}(#{var}).DecodeFrom(d)" + out.puts " nTmp, err = #{name type}(#{var}).DecodeFrom(d, maxDepth)" else - out.puts " nTmp, err = #{var}.DecodeFrom(d)" + out.puts " nTmp, err = #{var}.DecodeFrom(d, maxDepth)" end out.puts tail else - out.puts " nTmp, err = d.Decode(&#{var})" + out.puts " nTmp, err = d.DecodeWithMaxDepth(&#{var}, maxDepth)" out.puts tail end if optional @@ -778,7 +794,7 @@ def render_binary_interface(out, name) out.puts "func (s *#{name}) UnmarshalBinary(inp []byte) error {" out.puts " r := bytes.NewReader(inp)" out.puts " d := xdr.NewDecoder(r)" - out.puts " _, err := s.DecodeFrom(d)" + out.puts " _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth)" out.puts " return err" out.puts "}" out.break @@ -817,6 +833,7 @@ def render_top_matter(out) import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -837,14 +854,14 @@ def render_top_matter(out) } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) From 16b05a85fa91daeacc4aa1ef6b6b8201d100489d Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 19 Sep 2023 15:44:50 +0200 Subject: [PATCH 2/3] Update examples --- .../block_comments.x/MyXDR_generated.go | 13 +- .../const.x/MyXDR_generated.go | 21 +- .../enum.x/MyXDR_generated.go | 29 ++- .../nesting.x/MyXDR_generated.go | 53 +++-- .../optional.x/MyXDR_generated.go | 23 +- .../struct.x/MyXDR_generated.go | 23 +- .../test.x/MyXDR_generated.go | 201 +++++++++++++----- .../union.x/MyXDR_generated.go | 65 ++++-- 8 files changed, 302 insertions(+), 126 deletions(-) diff --git a/spec/output/generator_spec_go/block_comments.x/MyXDR_generated.go b/spec/output/generator_spec_go/block_comments.x/MyXDR_generated.go index b384c9a18..72555ca33 100644 --- a/spec/output/generator_spec_go/block_comments.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/block_comments.x/MyXDR_generated.go @@ -11,6 +11,7 @@ package MyXDR import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -27,14 +28,14 @@ type xdrType interface { } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) @@ -92,7 +93,11 @@ func (e AccountFlags) EncodeTo(enc *xdr.Encoder) error { } var _ decoderFrom = (*AccountFlags)(nil) // DecodeFrom decodes this value using the Decoder. -func (e *AccountFlags) DecodeFrom(d *xdr.Decoder) (int, error) { +func (e *AccountFlags) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding AccountFlags: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding AccountFlags: %s", err) @@ -115,7 +120,7 @@ func (s AccountFlags) MarshalBinary() ([]byte, error) { func (s *AccountFlags) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } diff --git a/spec/output/generator_spec_go/const.x/MyXDR_generated.go b/spec/output/generator_spec_go/const.x/MyXDR_generated.go index c884470c4..2a1d7e28b 100644 --- a/spec/output/generator_spec_go/const.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/const.x/MyXDR_generated.go @@ -11,6 +11,7 @@ package MyXDR import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -27,14 +28,14 @@ type xdrType interface { } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) @@ -77,7 +78,11 @@ if _, err = e.EncodeInt(int32(s)); err != nil { var _ decoderFrom = (*TestArray)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *TestArray) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *TestArray) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding TestArray: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v [Foo]int32 @@ -102,7 +107,7 @@ func (s TestArray) MarshalBinary() ([]byte, error) { func (s *TestArray) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -137,7 +142,11 @@ if _, err = e.EncodeInt(int32(s)); err != nil { var _ decoderFrom = (*TestArray2)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *TestArray2) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *TestArray2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding TestArray2: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v []int32 @@ -162,7 +171,7 @@ func (s TestArray2) MarshalBinary() ([]byte, error) { func (s *TestArray2) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } diff --git a/spec/output/generator_spec_go/enum.x/MyXDR_generated.go b/spec/output/generator_spec_go/enum.x/MyXDR_generated.go index b11df486b..25d09c90f 100644 --- a/spec/output/generator_spec_go/enum.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/enum.x/MyXDR_generated.go @@ -11,6 +11,7 @@ package MyXDR import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -27,14 +28,14 @@ type xdrType interface { } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) @@ -137,7 +138,11 @@ func (e MessageType) EncodeTo(enc *xdr.Encoder) error { } var _ decoderFrom = (*MessageType)(nil) // DecodeFrom decodes this value using the Decoder. -func (e *MessageType) DecodeFrom(d *xdr.Decoder) (int, error) { +func (e *MessageType) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding MessageType: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding MessageType: %s", err) @@ -160,7 +165,7 @@ func (s MessageType) MarshalBinary() ([]byte, error) { func (s *MessageType) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -217,7 +222,11 @@ func (e Color) EncodeTo(enc *xdr.Encoder) error { } var _ decoderFrom = (*Color)(nil) // DecodeFrom decodes this value using the Decoder. -func (e *Color) DecodeFrom(d *xdr.Decoder) (int, error) { +func (e *Color) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Color: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding Color: %s", err) @@ -240,7 +249,7 @@ func (s Color) MarshalBinary() ([]byte, error) { func (s *Color) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -297,7 +306,11 @@ func (e Color2) EncodeTo(enc *xdr.Encoder) error { } var _ decoderFrom = (*Color2)(nil) // DecodeFrom decodes this value using the Decoder. -func (e *Color2) DecodeFrom(d *xdr.Decoder) (int, error) { +func (e *Color2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Color2: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding Color2: %s", err) @@ -320,7 +333,7 @@ func (s Color2) MarshalBinary() ([]byte, error) { func (s *Color2) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } diff --git a/spec/output/generator_spec_go/nesting.x/MyXDR_generated.go b/spec/output/generator_spec_go/nesting.x/MyXDR_generated.go index d353ad983..168a967e4 100644 --- a/spec/output/generator_spec_go/nesting.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/nesting.x/MyXDR_generated.go @@ -11,6 +11,7 @@ package MyXDR import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -27,14 +28,14 @@ type xdrType interface { } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) @@ -97,7 +98,11 @@ func (e UnionKey) EncodeTo(enc *xdr.Encoder) error { } var _ decoderFrom = (*UnionKey)(nil) // DecodeFrom decodes this value using the Decoder. -func (e *UnionKey) DecodeFrom(d *xdr.Decoder) (int, error) { +func (e *UnionKey) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding UnionKey: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding UnionKey: %s", err) @@ -120,7 +125,7 @@ func (s UnionKey) MarshalBinary() ([]byte, error) { func (s *UnionKey) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -152,7 +157,11 @@ if _, err = e.EncodeInt(int32(s)); err != nil { var _ decoderFrom = (*Foo)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Foo) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Foo) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Foo: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v int32 @@ -177,7 +186,7 @@ func (s Foo) MarshalBinary() ([]byte, error) { func (s *Foo) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -213,7 +222,11 @@ if _, err = e.EncodeInt(int32(s.SomeInt)); err != nil { var _ decoderFrom = (*MyUnionOne)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *MyUnionOne) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *MyUnionOne) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int s.SomeInt, nTmp, err = d.DecodeInt() @@ -236,7 +249,7 @@ func (s MyUnionOne) MarshalBinary() ([]byte, error) { func (s *MyUnionOne) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -277,7 +290,11 @@ if err = s.Foo.EncodeTo(e); err != nil { var _ decoderFrom = (*MyUnionTwo)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *MyUnionTwo) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *MyUnionTwo) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int s.SomeInt, nTmp, err = d.DecodeInt() @@ -285,7 +302,7 @@ n += nTmp if err != nil { return n, fmt.Errorf("decoding Int: %s", err) } - nTmp, err = s.Foo.DecodeFrom(d) + nTmp, err = s.Foo.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Foo: %s", err) @@ -305,7 +322,7 @@ func (s MyUnionTwo) MarshalBinary() ([]byte, error) { func (s *MyUnionTwo) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -463,10 +480,14 @@ return nil var _ decoderFrom = (*MyUnion)(nil) // DecodeFrom decodes this value using the Decoder. -func (u *MyUnion) DecodeFrom(d *xdr.Decoder) (int, error) { +func (u *MyUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding MyUnion: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int - nTmp, err = u.Type.DecodeFrom(d) + nTmp, err = u.Type.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding UnionKey: %s", err) @@ -474,7 +495,7 @@ if err != nil { switch UnionKey(u.Type) { case UnionKeyOne: u.One = new(MyUnionOne) - nTmp, err = (*u.One).DecodeFrom(d) + nTmp, err = (*u.One).DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding MyUnionOne: %s", err) @@ -482,7 +503,7 @@ if err != nil { return n, nil case UnionKeyTwo: u.Two = new(MyUnionTwo) - nTmp, err = (*u.Two).DecodeFrom(d) + nTmp, err = (*u.Two).DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding MyUnionTwo: %s", err) @@ -507,7 +528,7 @@ func (s MyUnion) MarshalBinary() ([]byte, error) { func (s *MyUnion) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } diff --git a/spec/output/generator_spec_go/optional.x/MyXDR_generated.go b/spec/output/generator_spec_go/optional.x/MyXDR_generated.go index 0f8db98de..e7389e977 100644 --- a/spec/output/generator_spec_go/optional.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/optional.x/MyXDR_generated.go @@ -11,6 +11,7 @@ package MyXDR import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -27,14 +28,14 @@ type xdrType interface { } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) @@ -71,7 +72,11 @@ if _, err = e.EncodeInt(int32(s)); err != nil { var _ decoderFrom = (*Arr)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Arr) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Arr) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Arr: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v [2]int32 @@ -96,7 +101,7 @@ func (s Arr) MarshalBinary() ([]byte, error) { func (s *Arr) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -158,7 +163,11 @@ if err = (*s.ThirdOption).EncodeTo(e); err != nil { var _ decoderFrom = (*HasOptions)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *HasOptions) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *HasOptions) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var b bool @@ -198,7 +207,7 @@ if err != nil { s.ThirdOption = nil if b { s.ThirdOption = new(Arr) - nTmp, err = s.ThirdOption.DecodeFrom(d) + nTmp, err = s.ThirdOption.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Arr: %s", err) @@ -219,7 +228,7 @@ func (s HasOptions) MarshalBinary() ([]byte, error) { func (s *HasOptions) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } diff --git a/spec/output/generator_spec_go/struct.x/MyXDR_generated.go b/spec/output/generator_spec_go/struct.x/MyXDR_generated.go index 9f4c1cb73..6a989cbe1 100644 --- a/spec/output/generator_spec_go/struct.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/struct.x/MyXDR_generated.go @@ -11,6 +11,7 @@ package MyXDR import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -27,14 +28,14 @@ type xdrType interface { } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) @@ -72,7 +73,11 @@ if _, err = e.EncodeHyper(int64(s)); err != nil { var _ decoderFrom = (*Int64)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Int64) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Int64) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Int64: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v int64 @@ -97,7 +102,7 @@ func (s Int64) MarshalBinary() ([]byte, error) { func (s *Int64) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -154,7 +159,11 @@ if _, err = e.EncodeString(string(s.MaxString)); err != nil { var _ decoderFrom = (*MyStruct)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *MyStruct) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *MyStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int s.SomeInt, nTmp, err = d.DecodeInt() @@ -162,7 +171,7 @@ n += nTmp if err != nil { return n, fmt.Errorf("decoding Int: %s", err) } - nTmp, err = s.ABigInt.DecodeFrom(d) + nTmp, err = s.ABigInt.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Int64: %s", err) @@ -197,7 +206,7 @@ func (s MyStruct) MarshalBinary() ([]byte, error) { func (s *MyStruct) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } diff --git a/spec/output/generator_spec_go/test.x/MyXDR_generated.go b/spec/output/generator_spec_go/test.x/MyXDR_generated.go index 62ac4168c..842da93d2 100644 --- a/spec/output/generator_spec_go/test.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/test.x/MyXDR_generated.go @@ -11,6 +11,7 @@ package MyXDR import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -27,14 +28,14 @@ type xdrType interface { } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) @@ -76,7 +77,11 @@ if _, err = e.EncodeFixedOpaque(s[:]); err != nil { var _ decoderFrom = (*Uint512)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Uint512) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Uint512) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Uint512: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int nTmp, err = d.DecodeFixedOpaqueInplace(s[:]) @@ -99,7 +104,7 @@ func (s Uint512) MarshalBinary() ([]byte, error) { func (s *Uint512) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -135,7 +140,11 @@ if _, err = e.EncodeOpaque(s[:]); err != nil { var _ decoderFrom = (*Uint513)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Uint513) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Uint513) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Uint513: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int (*s), nTmp, err = d.DecodeOpaque(64) @@ -158,7 +167,7 @@ func (s Uint513) MarshalBinary() ([]byte, error) { func (s *Uint513) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -190,7 +199,11 @@ if _, err = e.EncodeOpaque(s[:]); err != nil { var _ decoderFrom = (*Uint514)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Uint514) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Uint514) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Uint514: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int (*s), nTmp, err = d.DecodeOpaque(0) @@ -213,7 +226,7 @@ func (s Uint514) MarshalBinary() ([]byte, error) { func (s *Uint514) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -249,7 +262,11 @@ if _, err = e.EncodeString(string(s)); err != nil { var _ decoderFrom = (*Str)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Str) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Str) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Str: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v string @@ -274,7 +291,7 @@ func (s Str) MarshalBinary() ([]byte, error) { func (s *Str) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -306,7 +323,11 @@ if _, err = e.EncodeString(string(s)); err != nil { var _ decoderFrom = (*Str2)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Str2) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Str2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Str2: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v string @@ -331,7 +352,7 @@ func (s Str2) MarshalBinary() ([]byte, error) { func (s *Str2) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -367,7 +388,11 @@ if _, err = e.EncodeFixedOpaque(s[:]); err != nil { var _ decoderFrom = (*Hash)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Hash) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Hash) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Hash: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int nTmp, err = d.DecodeFixedOpaqueInplace(s[:]) @@ -390,7 +415,7 @@ func (s Hash) MarshalBinary() ([]byte, error) { func (s *Hash) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -423,11 +448,15 @@ if err = s[i].EncodeTo(e); err != nil { var _ decoderFrom = (*Hashes1)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Hashes1) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Hashes1) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Hashes1: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int for i := 0; i < len(s); i++ { - nTmp, err = s[i].DecodeFrom(d) + nTmp, err = s[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Hash: %s", err) @@ -448,7 +477,7 @@ func (s Hashes1) MarshalBinary() ([]byte, error) { func (s *Hashes1) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -488,7 +517,11 @@ if err = s[i].EncodeTo(e); err != nil { var _ decoderFrom = (*Hashes2)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Hashes2) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Hashes2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Hashes2: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var l uint32 @@ -504,7 +537,7 @@ if err != nil { if l > 0 { (*s) = make([]Hash, l) for i := uint32(0); i < l; i++ { - nTmp, err = (*s)[i].DecodeFrom(d) + nTmp, err = (*s)[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Hash: %s", err) @@ -526,7 +559,7 @@ func (s Hashes2) MarshalBinary() ([]byte, error) { func (s *Hashes2) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -562,7 +595,11 @@ if err = s[i].EncodeTo(e); err != nil { var _ decoderFrom = (*Hashes3)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Hashes3) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Hashes3) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Hashes3: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var l uint32 @@ -575,7 +612,7 @@ if err != nil { if l > 0 { (*s) = make([]Hash, l) for i := uint32(0); i < l; i++ { - nTmp, err = (*s)[i].DecodeFrom(d) + nTmp, err = (*s)[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Hash: %s", err) @@ -597,7 +634,7 @@ func (s Hashes3) MarshalBinary() ([]byte, error) { func (s *Hashes3) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -639,7 +676,11 @@ if _, err = e.EncodeInt(int32(s)); err != nil { var _ decoderFrom = (*Int1)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Int1) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Int1) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Int1: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v int32 @@ -664,7 +705,7 @@ func (s Int1) MarshalBinary() ([]byte, error) { func (s *Int1) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -696,7 +737,11 @@ if _, err = e.EncodeHyper(int64(s)); err != nil { var _ decoderFrom = (*Int2)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Int2) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Int2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Int2: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v int64 @@ -721,7 +766,7 @@ func (s Int2) MarshalBinary() ([]byte, error) { func (s *Int2) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -753,7 +798,11 @@ if _, err = e.EncodeUint(uint32(s)); err != nil { var _ decoderFrom = (*Int3)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Int3) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Int3) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Int3: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v uint32 @@ -778,7 +827,7 @@ func (s Int3) MarshalBinary() ([]byte, error) { func (s *Int3) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -810,7 +859,11 @@ if _, err = e.EncodeUhyper(uint64(s)); err != nil { var _ decoderFrom = (*Int4)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Int4) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Int4) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Int4: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v uint64 @@ -835,7 +888,7 @@ func (s Int4) MarshalBinary() ([]byte, error) { func (s *Int4) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -907,10 +960,14 @@ if _, err = e.EncodeBool(bool(s.Field7)); err != nil { var _ decoderFrom = (*MyStruct)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *MyStruct) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *MyStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int - nTmp, err = s.Field1.DecodeFrom(d) + nTmp, err = s.Field1.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Uint512: %s", err) @@ -924,13 +981,13 @@ if err != nil { s.Field2 = nil if b { s.Field2 = new(Hash) - nTmp, err = s.Field2.DecodeFrom(d) + nTmp, err = s.Field2.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding OptHash1: %s", err) } } - nTmp, err = s.Field3.DecodeFrom(d) + nTmp, err = s.Field3.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Int1: %s", err) @@ -940,12 +997,12 @@ n += nTmp if err != nil { return n, fmt.Errorf("decoding Unsigned int: %s", err) } - nTmp, err = d.Decode(&s.Field5) + nTmp, err = d.DecodeWithMaxDepth(&s.Field5, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Float: %s", err) } - nTmp, err = d.Decode(&s.Field6) + nTmp, err = d.DecodeWithMaxDepth(&s.Field6, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Double: %s", err) @@ -970,7 +1027,7 @@ func (s MyStruct) MarshalBinary() ([]byte, error) { func (s *MyStruct) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -1012,7 +1069,11 @@ if err = s.Members[i].EncodeTo(e); err != nil { var _ decoderFrom = (*LotsOfMyStructs)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *LotsOfMyStructs) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *LotsOfMyStructs) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var l uint32 @@ -1025,7 +1086,7 @@ if err != nil { if l > 0 { s.Members = make([]MyStruct, l) for i := uint32(0); i < l; i++ { - nTmp, err = s.Members[i].DecodeFrom(d) + nTmp, err = s.Members[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding MyStruct: %s", err) @@ -1047,7 +1108,7 @@ func (s LotsOfMyStructs) MarshalBinary() ([]byte, error) { func (s *LotsOfMyStructs) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -1084,10 +1145,14 @@ if err = s.Data.EncodeTo(e); err != nil { var _ decoderFrom = (*HasStuff)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *HasStuff) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *HasStuff) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int - nTmp, err = s.Data.DecodeFrom(d) + nTmp, err = s.Data.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding LotsOfMyStructs: %s", err) @@ -1107,7 +1172,7 @@ func (s HasStuff) MarshalBinary() ([]byte, error) { func (s *HasStuff) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -1164,7 +1229,11 @@ func (e Color) EncodeTo(enc *xdr.Encoder) error { } var _ decoderFrom = (*Color)(nil) // DecodeFrom decodes this value using the Decoder. -func (e *Color) DecodeFrom(d *xdr.Decoder) (int, error) { +func (e *Color) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Color: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding Color: %s", err) @@ -1187,7 +1256,7 @@ func (s Color) MarshalBinary() ([]byte, error) { func (s *Color) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -1253,7 +1322,11 @@ func (e NesterNestedEnum) EncodeTo(enc *xdr.Encoder) error { } var _ decoderFrom = (*NesterNestedEnum)(nil) // DecodeFrom decodes this value using the Decoder. -func (e *NesterNestedEnum) DecodeFrom(d *xdr.Decoder) (int, error) { +func (e *NesterNestedEnum) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding NesterNestedEnum: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding NesterNestedEnum: %s", err) @@ -1276,7 +1349,7 @@ func (s NesterNestedEnum) MarshalBinary() ([]byte, error) { func (s *NesterNestedEnum) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -1312,7 +1385,11 @@ if _, err = e.EncodeInt(int32(s.Blah)); err != nil { var _ decoderFrom = (*NesterNestedStruct)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *NesterNestedStruct) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *NesterNestedStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int s.Blah, nTmp, err = d.DecodeInt() @@ -1335,7 +1412,7 @@ func (s NesterNestedStruct) MarshalBinary() ([]byte, error) { func (s *NesterNestedStruct) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -1442,10 +1519,14 @@ return nil var _ decoderFrom = (*NesterNestedUnion)(nil) // DecodeFrom decodes this value using the Decoder. -func (u *NesterNestedUnion) DecodeFrom(d *xdr.Decoder) (int, error) { +func (u *NesterNestedUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding NesterNestedUnion: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int - nTmp, err = u.Color.DecodeFrom(d) + nTmp, err = u.Color.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Color: %s", err) @@ -1477,7 +1558,7 @@ func (s NesterNestedUnion) MarshalBinary() ([]byte, error) { func (s *NesterNestedUnion) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -1538,20 +1619,24 @@ if err = s.NestedUnion.EncodeTo(e); err != nil { var _ decoderFrom = (*Nester)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Nester) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Nester) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int - nTmp, err = s.NestedEnum.DecodeFrom(d) + nTmp, err = s.NestedEnum.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding NesterNestedEnum: %s", err) } - nTmp, err = s.NestedStruct.DecodeFrom(d) + nTmp, err = s.NestedStruct.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding NesterNestedStruct: %s", err) } - nTmp, err = s.NestedUnion.DecodeFrom(d) + nTmp, err = s.NestedUnion.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding NesterNestedUnion: %s", err) @@ -1571,7 +1656,7 @@ func (s Nester) MarshalBinary() ([]byte, error) { func (s *Nester) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } diff --git a/spec/output/generator_spec_go/union.x/MyXDR_generated.go b/spec/output/generator_spec_go/union.x/MyXDR_generated.go index c15e8edc1..6346dca0a 100644 --- a/spec/output/generator_spec_go/union.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/union.x/MyXDR_generated.go @@ -11,6 +11,7 @@ package MyXDR import ( "bytes" "encoding" + "errors" "io" "fmt" @@ -27,14 +28,14 @@ type xdrType interface { } type decoderFrom interface { - DecodeFrom(d *xdr.Decoder) (int, error) + DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) } // Unmarshal reads an xdr element from `r` into `v`. func Unmarshal(r io.Reader, v interface{}) (int, error) { if decodable, ok := v.(decoderFrom); ok { d := xdr.NewDecoder(r) - return decodable.DecodeFrom(d) + return decodable.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) } // delegate to xdr package's Unmarshal return xdr.Unmarshal(r, v) @@ -72,7 +73,11 @@ if _, err = e.EncodeInt(int32(s)); err != nil { var _ decoderFrom = (*Error)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Error) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Error) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Error: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v int32 @@ -97,7 +102,7 @@ func (s Error) MarshalBinary() ([]byte, error) { func (s *Error) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -129,7 +134,11 @@ if _, err = e.EncodeInt(int32(s)); err != nil { var _ decoderFrom = (*Multi)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *Multi) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *Multi) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding Multi: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int var v int32 @@ -154,7 +163,7 @@ func (s Multi) MarshalBinary() ([]byte, error) { func (s *Multi) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -208,7 +217,11 @@ func (e UnionKey) EncodeTo(enc *xdr.Encoder) error { } var _ decoderFrom = (*UnionKey)(nil) // DecodeFrom decodes this value using the Decoder. -func (e *UnionKey) DecodeFrom(d *xdr.Decoder) (int, error) { +func (e *UnionKey) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding UnionKey: maximum decoding depth reached") + } + maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { return n, fmt.Errorf("decoding UnionKey: %s", err) @@ -231,7 +244,7 @@ func (s UnionKey) MarshalBinary() ([]byte, error) { func (s *UnionKey) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -380,10 +393,14 @@ return nil var _ decoderFrom = (*MyUnion)(nil) // DecodeFrom decodes this value using the Decoder. -func (u *MyUnion) DecodeFrom(d *xdr.Decoder) (int, error) { +func (u *MyUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding MyUnion: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int - nTmp, err = u.Type.DecodeFrom(d) + nTmp, err = u.Type.DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding UnionKey: %s", err) @@ -391,7 +408,7 @@ if err != nil { switch UnionKey(u.Type) { case UnionKeyError: u.Error = new(Error) - nTmp, err = (*u.Error).DecodeFrom(d) + nTmp, err = (*u.Error).DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Error: %s", err) @@ -409,7 +426,7 @@ if err != nil { if l > 0 { (*u.Things) = make([]Multi, l) for i := uint32(0); i < l; i++ { - nTmp, err = (*u.Things)[i].DecodeFrom(d) + nTmp, err = (*u.Things)[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Multi: %s", err) @@ -433,7 +450,7 @@ func (s MyUnion) MarshalBinary() ([]byte, error) { func (s *MyUnion) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -581,7 +598,11 @@ return nil var _ decoderFrom = (*IntUnion)(nil) // DecodeFrom decodes this value using the Decoder. -func (u *IntUnion) DecodeFrom(d *xdr.Decoder) (int, error) { +func (u *IntUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding IntUnion: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int u.Type, nTmp, err = d.DecodeInt() @@ -592,7 +613,7 @@ if err != nil { switch int32(u.Type) { case 0: u.Error = new(Error) - nTmp, err = (*u.Error).DecodeFrom(d) + nTmp, err = (*u.Error).DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Error: %s", err) @@ -610,7 +631,7 @@ if err != nil { if l > 0 { (*u.Things) = make([]Multi, l) for i := uint32(0); i < l; i++ { - nTmp, err = (*u.Things)[i].DecodeFrom(d) + nTmp, err = (*u.Things)[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding Multi: %s", err) @@ -634,7 +655,7 @@ func (s IntUnion) MarshalBinary() ([]byte, error) { func (s *IntUnion) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } @@ -707,10 +728,14 @@ if err = IntUnion(s).EncodeTo(e); err != nil { var _ decoderFrom = (*IntUnion2)(nil) // DecodeFrom decodes this value using the Decoder. -func (s *IntUnion2) DecodeFrom(d *xdr.Decoder) (int, error) { +func (s *IntUnion2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { + if maxDepth == 0 { + return 0, errors.New("decoding IntUnion2: maximum decoding depth reached") + } + maxDepth -= 1 var err error var n, nTmp int - nTmp, err = (*IntUnion)(s).DecodeFrom(d) + nTmp, err = (*IntUnion)(s).DecodeFrom(d, maxDepth) n += nTmp if err != nil { return n, fmt.Errorf("decoding IntUnion: %s", err) @@ -730,7 +755,7 @@ func (s IntUnion2) MarshalBinary() ([]byte, error) { func (s *IntUnion2) UnmarshalBinary(inp []byte) error { r := bytes.NewReader(inp) d := xdr.NewDecoder(r) - _, err := s.DecodeFrom(d) + _, err := s.DecodeFrom(d, xdr.DecodeDefaultMaxDepth) return err } From 5aaecf3da53caca2bb87e621583b47fab7b54c2f Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 19 Sep 2023 16:56:54 +0200 Subject: [PATCH 3/3] Address review feedback --- lib/xdrgen/generators/go.rb | 16 +-- .../block_comments.x/MyXDR_generated.go | 6 +- .../const.x/MyXDR_generated.go | 10 +- .../enum.x/MyXDR_generated.go | 14 ++- .../nesting.x/MyXDR_generated.go | 32 ++--- .../optional.x/MyXDR_generated.go | 20 +-- .../struct.x/MyXDR_generated.go | 18 +-- .../test.x/MyXDR_generated.go | 114 +++++++++--------- .../union.x/MyXDR_generated.go | 46 +++---- 9 files changed, 147 insertions(+), 129 deletions(-) diff --git a/lib/xdrgen/generators/go.rb b/lib/xdrgen/generators/go.rb index 8c84c248c..f2a6da035 100644 --- a/lib/xdrgen/generators/go.rb +++ b/lib/xdrgen/generators/go.rb @@ -538,7 +538,7 @@ def render_struct_decode_from_interface(out, struct) out.puts "// DecodeFrom decodes this value using the Decoder." out.puts "func (s *#{name}) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) {" out.puts " if maxDepth == 0 {" - out.puts " return 0, errors.New(\"maximum decoding depth reached\")" + out.puts " return 0, fmt.Errorf(\"decoding #{name}: %w\", ErrMaxDecodingDepthReached)" out.puts " }" out.puts " maxDepth -= 1" out.puts " var err error" @@ -558,7 +558,7 @@ def render_union_decode_from_interface(out, union) out.puts "// DecodeFrom decodes this value using the Decoder." out.puts "func (u *#{name}) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) {" out.puts " if maxDepth == 0 {" - out.puts " return 0, errors.New(\"decoding #{name}: maximum decoding depth reached\")" + out.puts " return 0, fmt.Errorf(\"decoding #{name}: %w\", ErrMaxDecodingDepthReached)" out.puts " }" out.puts " maxDepth -= 1" out.puts " var err error" @@ -591,12 +591,12 @@ def render_enum_decode_from_interface(out, typedef) // DecodeFrom decodes this value using the Decoder. func (e *#{name}) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding #{name}: maximum decoding depth reached") + return 0, fmt.Errorf("decoding #{name}: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding #{name}: %s", err) + return n, fmt.Errorf("decoding #{name}: %w", err) } if _, ok := #{private_name type}Map[v]; !ok { return n, fmt.Errorf("'%d' is not a valid #{name} enum value", v) @@ -613,7 +613,7 @@ def render_typedef_decode_from_interface(out, typedef) out.puts "// DecodeFrom decodes this value using the Decoder." out.puts "func (s *#{name}) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) {" out.puts " if maxDepth == 0 {" - out.puts " return 0, errors.New(\"decoding #{name}: maximum decoding depth reached\")" + out.puts " return 0, fmt.Errorf(\"decoding #{name}: %w\", ErrMaxDecodingDepthReached)" out.puts " }" out.puts " maxDepth -= 1" out.puts " var err error" @@ -652,7 +652,7 @@ def render_decode_from_body(out, var, type, declared_variables:, self_encode:) tail = <<-EOS.strip_heredoc n += nTmp if err != nil { - return n, fmt.Errorf("decoding #{name type}: %s", err) + return n, fmt.Errorf("decoding #{name type}: %w", err) } EOS optional = type.sub_type == :optional @@ -849,6 +849,8 @@ def render_top_matter(out) EOS out.break out.puts <<-EOS.strip_heredoc + var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -996,7 +998,7 @@ def render_union_constructor(out, union) <<-EOS tv, ok := value.(#{reference arm.type}) if !ok { - err = fmt.Errorf("invalid value, must be #{reference arm.type}") + err = errors.New("invalid value, must be #{reference arm.type}") return } result.#{name arm} = &tv diff --git a/spec/output/generator_spec_go/block_comments.x/MyXDR_generated.go b/spec/output/generator_spec_go/block_comments.x/MyXDR_generated.go index 72555ca33..0eaa4ee67 100644 --- a/spec/output/generator_spec_go/block_comments.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/block_comments.x/MyXDR_generated.go @@ -23,6 +23,8 @@ var XdrFilesSHA256 = map[string]string{ "spec/fixtures/generator/block_comments.x": "e13131bc4134f38da17b9d5e9f67d2695a69ef98e3ef272833f4c18d0cc88a30", } +var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -95,12 +97,12 @@ var _ decoderFrom = (*AccountFlags)(nil) // DecodeFrom decodes this value using the Decoder. func (e *AccountFlags) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding AccountFlags: maximum decoding depth reached") + return 0, fmt.Errorf("decoding AccountFlags: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding AccountFlags: %s", err) + return n, fmt.Errorf("decoding AccountFlags: %w", err) } if _, ok := accountFlagsMap[v]; !ok { return n, fmt.Errorf("'%d' is not a valid AccountFlags enum value", v) diff --git a/spec/output/generator_spec_go/const.x/MyXDR_generated.go b/spec/output/generator_spec_go/const.x/MyXDR_generated.go index 2a1d7e28b..46a8aecff 100644 --- a/spec/output/generator_spec_go/const.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/const.x/MyXDR_generated.go @@ -23,6 +23,8 @@ var XdrFilesSHA256 = map[string]string{ "spec/fixtures/generator/const.x": "0bff3b37592fcc16cad2fe10b9a72f5d39d033a114917c24e86a9ebd9cda9c37", } +var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -80,7 +82,7 @@ var _ decoderFrom = (*TestArray)(nil) // DecodeFrom decodes this value using the Decoder. func (s *TestArray) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding TestArray: maximum decoding depth reached") + return 0, fmt.Errorf("decoding TestArray: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -89,7 +91,7 @@ func (s *TestArray) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } *s = TestArray(v) return n, nil @@ -144,7 +146,7 @@ var _ decoderFrom = (*TestArray2)(nil) // DecodeFrom decodes this value using the Decoder. func (s *TestArray2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding TestArray2: maximum decoding depth reached") + return 0, fmt.Errorf("decoding TestArray2: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -153,7 +155,7 @@ func (s *TestArray2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } *s = TestArray2(v) return n, nil diff --git a/spec/output/generator_spec_go/enum.x/MyXDR_generated.go b/spec/output/generator_spec_go/enum.x/MyXDR_generated.go index 25d09c90f..592762bc4 100644 --- a/spec/output/generator_spec_go/enum.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/enum.x/MyXDR_generated.go @@ -23,6 +23,8 @@ var XdrFilesSHA256 = map[string]string{ "spec/fixtures/generator/enum.x": "35cf5e97e2057039640ed260e8b38bb2733a3c3ca8529c93877bdec02a999d7f", } +var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -140,12 +142,12 @@ var _ decoderFrom = (*MessageType)(nil) // DecodeFrom decodes this value using the Decoder. func (e *MessageType) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding MessageType: maximum decoding depth reached") + return 0, fmt.Errorf("decoding MessageType: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding MessageType: %s", err) + return n, fmt.Errorf("decoding MessageType: %w", err) } if _, ok := messageTypeMap[v]; !ok { return n, fmt.Errorf("'%d' is not a valid MessageType enum value", v) @@ -224,12 +226,12 @@ var _ decoderFrom = (*Color)(nil) // DecodeFrom decodes this value using the Decoder. func (e *Color) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Color: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Color: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding Color: %s", err) + return n, fmt.Errorf("decoding Color: %w", err) } if _, ok := colorMap[v]; !ok { return n, fmt.Errorf("'%d' is not a valid Color enum value", v) @@ -308,12 +310,12 @@ var _ decoderFrom = (*Color2)(nil) // DecodeFrom decodes this value using the Decoder. func (e *Color2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Color2: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Color2: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding Color2: %s", err) + return n, fmt.Errorf("decoding Color2: %w", err) } if _, ok := color2Map[v]; !ok { return n, fmt.Errorf("'%d' is not a valid Color2 enum value", v) diff --git a/spec/output/generator_spec_go/nesting.x/MyXDR_generated.go b/spec/output/generator_spec_go/nesting.x/MyXDR_generated.go index 168a967e4..7f8219c92 100644 --- a/spec/output/generator_spec_go/nesting.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/nesting.x/MyXDR_generated.go @@ -23,6 +23,8 @@ var XdrFilesSHA256 = map[string]string{ "spec/fixtures/generator/nesting.x": "5537949272c11f1bd09cf613a3751668b5018d686a1c2aaa3baa91183ca18f6a", } +var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -100,12 +102,12 @@ var _ decoderFrom = (*UnionKey)(nil) // DecodeFrom decodes this value using the Decoder. func (e *UnionKey) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding UnionKey: maximum decoding depth reached") + return 0, fmt.Errorf("decoding UnionKey: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding UnionKey: %s", err) + return n, fmt.Errorf("decoding UnionKey: %w", err) } if _, ok := unionKeyMap[v]; !ok { return n, fmt.Errorf("'%d' is not a valid UnionKey enum value", v) @@ -159,7 +161,7 @@ var _ decoderFrom = (*Foo)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Foo) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Foo: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Foo: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -168,7 +170,7 @@ func (s *Foo) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } *s = Foo(v) return n, nil @@ -224,7 +226,7 @@ var _ decoderFrom = (*MyUnionOne)(nil) // DecodeFrom decodes this value using the Decoder. func (s *MyUnionOne) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding MyUnionOne: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -232,7 +234,7 @@ func (s *MyUnionOne) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { s.SomeInt, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } return n, nil } @@ -292,7 +294,7 @@ var _ decoderFrom = (*MyUnionTwo)(nil) // DecodeFrom decodes this value using the Decoder. func (s *MyUnionTwo) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding MyUnionTwo: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -300,12 +302,12 @@ func (s *MyUnionTwo) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { s.SomeInt, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } nTmp, err = s.Foo.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Foo: %s", err) + return n, fmt.Errorf("decoding Foo: %w", err) } return n, nil } @@ -389,14 +391,14 @@ switch UnionKey(aType) { case UnionKeyOne: tv, ok := value.(MyUnionOne) if !ok { - err = fmt.Errorf("invalid value, must be MyUnionOne") + err = errors.New("invalid value, must be MyUnionOne") return } result.One = &tv case UnionKeyTwo: tv, ok := value.(MyUnionTwo) if !ok { - err = fmt.Errorf("invalid value, must be MyUnionTwo") + err = errors.New("invalid value, must be MyUnionTwo") return } result.Two = &tv @@ -482,7 +484,7 @@ var _ decoderFrom = (*MyUnion)(nil) // DecodeFrom decodes this value using the Decoder. func (u *MyUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding MyUnion: maximum decoding depth reached") + return 0, fmt.Errorf("decoding MyUnion: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -490,7 +492,7 @@ func (u *MyUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = u.Type.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding UnionKey: %s", err) + return n, fmt.Errorf("decoding UnionKey: %w", err) } switch UnionKey(u.Type) { case UnionKeyOne: @@ -498,7 +500,7 @@ switch UnionKey(u.Type) { nTmp, err = (*u.One).DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding MyUnionOne: %s", err) + return n, fmt.Errorf("decoding MyUnionOne: %w", err) } return n, nil case UnionKeyTwo: @@ -506,7 +508,7 @@ if err != nil { nTmp, err = (*u.Two).DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding MyUnionTwo: %s", err) + return n, fmt.Errorf("decoding MyUnionTwo: %w", err) } return n, nil case UnionKeyOffer: diff --git a/spec/output/generator_spec_go/optional.x/MyXDR_generated.go b/spec/output/generator_spec_go/optional.x/MyXDR_generated.go index e7389e977..cb030c26d 100644 --- a/spec/output/generator_spec_go/optional.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/optional.x/MyXDR_generated.go @@ -23,6 +23,8 @@ var XdrFilesSHA256 = map[string]string{ "spec/fixtures/generator/optional.x": "3241e832fcf00bca4315ecb6c259621dafb0e302a63a993f5504b0b5cebb6bd7", } +var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -74,7 +76,7 @@ var _ decoderFrom = (*Arr)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Arr) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Arr: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Arr: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -83,7 +85,7 @@ func (s *Arr) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } *s = Arr(v) return n, nil @@ -165,7 +167,7 @@ var _ decoderFrom = (*HasOptions)(nil) // DecodeFrom decodes this value using the Decoder. func (s *HasOptions) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding HasOptions: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -174,7 +176,7 @@ func (s *HasOptions) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { b, nTmp, err = d.DecodeBool() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } s.FirstOption = nil if b { @@ -182,13 +184,13 @@ if err != nil { s.FirstOption, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } } b, nTmp, err = d.DecodeBool() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } s.SecondOption = nil if b { @@ -196,13 +198,13 @@ if err != nil { s.SecondOption, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } } b, nTmp, err = d.DecodeBool() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Arr: %s", err) + return n, fmt.Errorf("decoding Arr: %w", err) } s.ThirdOption = nil if b { @@ -210,7 +212,7 @@ if err != nil { nTmp, err = s.ThirdOption.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Arr: %s", err) + return n, fmt.Errorf("decoding Arr: %w", err) } } return n, nil diff --git a/spec/output/generator_spec_go/struct.x/MyXDR_generated.go b/spec/output/generator_spec_go/struct.x/MyXDR_generated.go index 6a989cbe1..b2f7d834b 100644 --- a/spec/output/generator_spec_go/struct.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/struct.x/MyXDR_generated.go @@ -23,6 +23,8 @@ var XdrFilesSHA256 = map[string]string{ "spec/fixtures/generator/struct.x": "c6911a83390e3b499c078fd0c579132eacce88a4a0538d3b8b5e57747a58db4a", } +var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -75,7 +77,7 @@ var _ decoderFrom = (*Int64)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Int64) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Int64: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Int64: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -84,7 +86,7 @@ func (s *Int64) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeHyper() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Hyper: %s", err) + return n, fmt.Errorf("decoding Hyper: %w", err) } *s = Int64(v) return n, nil @@ -161,7 +163,7 @@ var _ decoderFrom = (*MyStruct)(nil) // DecodeFrom decodes this value using the Decoder. func (s *MyStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding MyStruct: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -169,27 +171,27 @@ func (s *MyStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { s.SomeInt, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } nTmp, err = s.ABigInt.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int64: %s", err) + return n, fmt.Errorf("decoding Int64: %w", err) } nTmp, err = d.DecodeFixedOpaqueInplace(s.SomeOpaque[:]) n += nTmp if err != nil { - return n, fmt.Errorf("decoding SomeOpaque: %s", err) + return n, fmt.Errorf("decoding SomeOpaque: %w", err) } s.SomeString, nTmp, err = d.DecodeString(0) n += nTmp if err != nil { - return n, fmt.Errorf("decoding SomeString: %s", err) + return n, fmt.Errorf("decoding SomeString: %w", err) } s.MaxString, nTmp, err = d.DecodeString(100) n += nTmp if err != nil { - return n, fmt.Errorf("decoding MaxString: %s", err) + return n, fmt.Errorf("decoding MaxString: %w", err) } return n, nil } diff --git a/spec/output/generator_spec_go/test.x/MyXDR_generated.go b/spec/output/generator_spec_go/test.x/MyXDR_generated.go index 842da93d2..0bd57e8fb 100644 --- a/spec/output/generator_spec_go/test.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/test.x/MyXDR_generated.go @@ -23,6 +23,8 @@ var XdrFilesSHA256 = map[string]string{ "spec/fixtures/generator/test.x": "d29a98a6a3b9bf533a3e6712d928e0bed655e0f462ac4dae810c65d52ca9af41", } +var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -79,7 +81,7 @@ var _ decoderFrom = (*Uint512)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Uint512) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Uint512: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Uint512: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -87,7 +89,7 @@ func (s *Uint512) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = d.DecodeFixedOpaqueInplace(s[:]) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Uint512: %s", err) + return n, fmt.Errorf("decoding Uint512: %w", err) } return n, nil } @@ -142,7 +144,7 @@ var _ decoderFrom = (*Uint513)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Uint513) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Uint513: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Uint513: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -150,7 +152,7 @@ func (s *Uint513) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { (*s), nTmp, err = d.DecodeOpaque(64) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Uint513: %s", err) + return n, fmt.Errorf("decoding Uint513: %w", err) } return n, nil } @@ -201,7 +203,7 @@ var _ decoderFrom = (*Uint514)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Uint514) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Uint514: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Uint514: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -209,7 +211,7 @@ func (s *Uint514) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { (*s), nTmp, err = d.DecodeOpaque(0) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Uint514: %s", err) + return n, fmt.Errorf("decoding Uint514: %w", err) } return n, nil } @@ -264,7 +266,7 @@ var _ decoderFrom = (*Str)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Str) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Str: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Str: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -273,7 +275,7 @@ func (s *Str) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeString(64) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Str: %s", err) + return n, fmt.Errorf("decoding Str: %w", err) } *s = Str(v) return n, nil @@ -325,7 +327,7 @@ var _ decoderFrom = (*Str2)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Str2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Str2: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Str2: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -334,7 +336,7 @@ func (s *Str2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeString(0) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Str2: %s", err) + return n, fmt.Errorf("decoding Str2: %w", err) } *s = Str2(v) return n, nil @@ -390,7 +392,7 @@ var _ decoderFrom = (*Hash)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Hash) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Hash: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Hash: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -398,7 +400,7 @@ func (s *Hash) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = d.DecodeFixedOpaqueInplace(s[:]) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Hash: %s", err) + return n, fmt.Errorf("decoding Hash: %w", err) } return n, nil } @@ -450,7 +452,7 @@ var _ decoderFrom = (*Hashes1)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Hashes1) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Hashes1: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Hashes1: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -459,7 +461,7 @@ func (s *Hashes1) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = s[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Hash: %s", err) + return n, fmt.Errorf("decoding Hash: %w", err) } } return n, nil @@ -519,7 +521,7 @@ var _ decoderFrom = (*Hashes2)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Hashes2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Hashes2: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Hashes2: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -528,7 +530,7 @@ func (s *Hashes2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { l, nTmp, err = d.DecodeUint() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Hash: %s", err) + return n, fmt.Errorf("decoding Hash: %w", err) } if l > 12 { return n, fmt.Errorf("decoding Hash: data size (%d) exceeds size limit (12)", l) @@ -540,7 +542,7 @@ if err != nil { nTmp, err = (*s)[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Hash: %s", err) + return n, fmt.Errorf("decoding Hash: %w", err) } } } @@ -597,7 +599,7 @@ var _ decoderFrom = (*Hashes3)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Hashes3) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Hashes3: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Hashes3: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -606,7 +608,7 @@ func (s *Hashes3) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { l, nTmp, err = d.DecodeUint() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Hash: %s", err) + return n, fmt.Errorf("decoding Hash: %w", err) } (*s) = nil if l > 0 { @@ -615,7 +617,7 @@ if err != nil { nTmp, err = (*s)[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Hash: %s", err) + return n, fmt.Errorf("decoding Hash: %w", err) } } } @@ -678,7 +680,7 @@ var _ decoderFrom = (*Int1)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Int1) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Int1: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Int1: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -687,7 +689,7 @@ func (s *Int1) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } *s = Int1(v) return n, nil @@ -739,7 +741,7 @@ var _ decoderFrom = (*Int2)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Int2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Int2: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Int2: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -748,7 +750,7 @@ func (s *Int2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeHyper() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Hyper: %s", err) + return n, fmt.Errorf("decoding Hyper: %w", err) } *s = Int2(v) return n, nil @@ -800,7 +802,7 @@ var _ decoderFrom = (*Int3)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Int3) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Int3: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Int3: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -809,7 +811,7 @@ func (s *Int3) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeUint() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Unsigned int: %s", err) + return n, fmt.Errorf("decoding Unsigned int: %w", err) } *s = Int3(v) return n, nil @@ -861,7 +863,7 @@ var _ decoderFrom = (*Int4)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Int4) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Int4: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Int4: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -870,7 +872,7 @@ func (s *Int4) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeUhyper() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Unsigned hyper: %s", err) + return n, fmt.Errorf("decoding Unsigned hyper: %w", err) } *s = Int4(v) return n, nil @@ -962,7 +964,7 @@ var _ decoderFrom = (*MyStruct)(nil) // DecodeFrom decodes this value using the Decoder. func (s *MyStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding MyStruct: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -970,13 +972,13 @@ func (s *MyStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = s.Field1.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Uint512: %s", err) + return n, fmt.Errorf("decoding Uint512: %w", err) } var b bool b, nTmp, err = d.DecodeBool() n += nTmp if err != nil { - return n, fmt.Errorf("decoding OptHash1: %s", err) + return n, fmt.Errorf("decoding OptHash1: %w", err) } s.Field2 = nil if b { @@ -984,33 +986,33 @@ if err != nil { nTmp, err = s.Field2.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding OptHash1: %s", err) + return n, fmt.Errorf("decoding OptHash1: %w", err) } } nTmp, err = s.Field3.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int1: %s", err) + return n, fmt.Errorf("decoding Int1: %w", err) } s.Field4, nTmp, err = d.DecodeUint() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Unsigned int: %s", err) + return n, fmt.Errorf("decoding Unsigned int: %w", err) } nTmp, err = d.DecodeWithMaxDepth(&s.Field5, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Float: %s", err) + return n, fmt.Errorf("decoding Float: %w", err) } nTmp, err = d.DecodeWithMaxDepth(&s.Field6, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Double: %s", err) + return n, fmt.Errorf("decoding Double: %w", err) } s.Field7, nTmp, err = d.DecodeBool() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Bool: %s", err) + return n, fmt.Errorf("decoding Bool: %w", err) } return n, nil } @@ -1071,7 +1073,7 @@ var _ decoderFrom = (*LotsOfMyStructs)(nil) // DecodeFrom decodes this value using the Decoder. func (s *LotsOfMyStructs) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding LotsOfMyStructs: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -1080,7 +1082,7 @@ func (s *LotsOfMyStructs) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) l, nTmp, err = d.DecodeUint() n += nTmp if err != nil { - return n, fmt.Errorf("decoding MyStruct: %s", err) + return n, fmt.Errorf("decoding MyStruct: %w", err) } s.Members = nil if l > 0 { @@ -1089,7 +1091,7 @@ if err != nil { nTmp, err = s.Members[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding MyStruct: %s", err) + return n, fmt.Errorf("decoding MyStruct: %w", err) } } } @@ -1147,7 +1149,7 @@ var _ decoderFrom = (*HasStuff)(nil) // DecodeFrom decodes this value using the Decoder. func (s *HasStuff) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding HasStuff: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -1155,7 +1157,7 @@ func (s *HasStuff) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = s.Data.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding LotsOfMyStructs: %s", err) + return n, fmt.Errorf("decoding LotsOfMyStructs: %w", err) } return n, nil } @@ -1231,12 +1233,12 @@ var _ decoderFrom = (*Color)(nil) // DecodeFrom decodes this value using the Decoder. func (e *Color) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Color: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Color: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding Color: %s", err) + return n, fmt.Errorf("decoding Color: %w", err) } if _, ok := colorMap[v]; !ok { return n, fmt.Errorf("'%d' is not a valid Color enum value", v) @@ -1324,12 +1326,12 @@ var _ decoderFrom = (*NesterNestedEnum)(nil) // DecodeFrom decodes this value using the Decoder. func (e *NesterNestedEnum) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding NesterNestedEnum: maximum decoding depth reached") + return 0, fmt.Errorf("decoding NesterNestedEnum: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding NesterNestedEnum: %s", err) + return n, fmt.Errorf("decoding NesterNestedEnum: %w", err) } if _, ok := nestedEnumMap[v]; !ok { return n, fmt.Errorf("'%d' is not a valid NesterNestedEnum enum value", v) @@ -1387,7 +1389,7 @@ var _ decoderFrom = (*NesterNestedStruct)(nil) // DecodeFrom decodes this value using the Decoder. func (s *NesterNestedStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding NesterNestedStruct: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -1395,7 +1397,7 @@ func (s *NesterNestedStruct) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, err s.Blah, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } return n, nil } @@ -1467,7 +1469,7 @@ switch Color(color) { default: tv, ok := value.(int32) if !ok { - err = fmt.Errorf("invalid value, must be int32") + err = errors.New("invalid value, must be int32") return } result.Blah2 = &tv @@ -1521,7 +1523,7 @@ var _ decoderFrom = (*NesterNestedUnion)(nil) // DecodeFrom decodes this value using the Decoder. func (u *NesterNestedUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding NesterNestedUnion: maximum decoding depth reached") + return 0, fmt.Errorf("decoding NesterNestedUnion: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -1529,7 +1531,7 @@ func (u *NesterNestedUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, erro nTmp, err = u.Color.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Color: %s", err) + return n, fmt.Errorf("decoding Color: %w", err) } switch Color(u.Color) { case ColorRed: @@ -1540,7 +1542,7 @@ switch Color(u.Color) { (*u.Blah2), nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } return n, nil } @@ -1621,7 +1623,7 @@ var _ decoderFrom = (*Nester)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Nester) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("maximum decoding depth reached") + return 0, fmt.Errorf("decoding Nester: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -1629,17 +1631,17 @@ func (s *Nester) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = s.NestedEnum.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding NesterNestedEnum: %s", err) + return n, fmt.Errorf("decoding NesterNestedEnum: %w", err) } nTmp, err = s.NestedStruct.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding NesterNestedStruct: %s", err) + return n, fmt.Errorf("decoding NesterNestedStruct: %w", err) } nTmp, err = s.NestedUnion.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding NesterNestedUnion: %s", err) + return n, fmt.Errorf("decoding NesterNestedUnion: %w", err) } return n, nil } diff --git a/spec/output/generator_spec_go/union.x/MyXDR_generated.go b/spec/output/generator_spec_go/union.x/MyXDR_generated.go index 6346dca0a..980ddd3f8 100644 --- a/spec/output/generator_spec_go/union.x/MyXDR_generated.go +++ b/spec/output/generator_spec_go/union.x/MyXDR_generated.go @@ -23,6 +23,8 @@ var XdrFilesSHA256 = map[string]string{ "spec/fixtures/generator/union.x": "c251258d967223b341ebcf2d5bb0718e9a039b46232cb743865d9acd0c4bbe41", } +var ErrMaxDecodingDepthReached = errors.New("maximum decoding depth reached") + type xdrType interface { xdrType() } @@ -75,7 +77,7 @@ var _ decoderFrom = (*Error)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Error) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Error: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Error: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -84,7 +86,7 @@ func (s *Error) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } *s = Error(v) return n, nil @@ -136,7 +138,7 @@ var _ decoderFrom = (*Multi)(nil) // DecodeFrom decodes this value using the Decoder. func (s *Multi) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding Multi: maximum decoding depth reached") + return 0, fmt.Errorf("decoding Multi: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -145,7 +147,7 @@ func (s *Multi) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { v, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } *s = Multi(v) return n, nil @@ -219,12 +221,12 @@ var _ decoderFrom = (*UnionKey)(nil) // DecodeFrom decodes this value using the Decoder. func (e *UnionKey) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding UnionKey: maximum decoding depth reached") + return 0, fmt.Errorf("decoding UnionKey: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 v, n, err := d.DecodeInt() if err != nil { - return n, fmt.Errorf("decoding UnionKey: %s", err) + return n, fmt.Errorf("decoding UnionKey: %w", err) } if _, ok := unionKeyMap[v]; !ok { return n, fmt.Errorf("'%d' is not a valid UnionKey enum value", v) @@ -302,14 +304,14 @@ switch UnionKey(aType) { case UnionKeyError: tv, ok := value.(Error) if !ok { - err = fmt.Errorf("invalid value, must be Error") + err = errors.New("invalid value, must be Error") return } result.Error = &tv case UnionKeyMulti: tv, ok := value.([]Multi) if !ok { - err = fmt.Errorf("invalid value, must be []Multi") + err = errors.New("invalid value, must be []Multi") return } result.Things = &tv @@ -395,7 +397,7 @@ var _ decoderFrom = (*MyUnion)(nil) // DecodeFrom decodes this value using the Decoder. func (u *MyUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding MyUnion: maximum decoding depth reached") + return 0, fmt.Errorf("decoding MyUnion: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -403,7 +405,7 @@ func (u *MyUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = u.Type.DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding UnionKey: %s", err) + return n, fmt.Errorf("decoding UnionKey: %w", err) } switch UnionKey(u.Type) { case UnionKeyError: @@ -411,7 +413,7 @@ switch UnionKey(u.Type) { nTmp, err = (*u.Error).DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Error: %s", err) + return n, fmt.Errorf("decoding Error: %w", err) } return n, nil case UnionKeyMulti: @@ -420,7 +422,7 @@ if err != nil { l, nTmp, err = d.DecodeUint() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Multi: %s", err) + return n, fmt.Errorf("decoding Multi: %w", err) } (*u.Things) = nil if l > 0 { @@ -429,7 +431,7 @@ if err != nil { nTmp, err = (*u.Things)[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Multi: %s", err) + return n, fmt.Errorf("decoding Multi: %w", err) } } } @@ -507,14 +509,14 @@ switch int32(aType) { case 0: tv, ok := value.(Error) if !ok { - err = fmt.Errorf("invalid value, must be Error") + err = errors.New("invalid value, must be Error") return } result.Error = &tv case 1: tv, ok := value.([]Multi) if !ok { - err = fmt.Errorf("invalid value, must be []Multi") + err = errors.New("invalid value, must be []Multi") return } result.Things = &tv @@ -600,7 +602,7 @@ var _ decoderFrom = (*IntUnion)(nil) // DecodeFrom decodes this value using the Decoder. func (u *IntUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding IntUnion: maximum decoding depth reached") + return 0, fmt.Errorf("decoding IntUnion: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -608,7 +610,7 @@ func (u *IntUnion) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { u.Type, nTmp, err = d.DecodeInt() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Int: %s", err) + return n, fmt.Errorf("decoding Int: %w", err) } switch int32(u.Type) { case 0: @@ -616,7 +618,7 @@ switch int32(u.Type) { nTmp, err = (*u.Error).DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Error: %s", err) + return n, fmt.Errorf("decoding Error: %w", err) } return n, nil case 1: @@ -625,7 +627,7 @@ if err != nil { l, nTmp, err = d.DecodeUint() n += nTmp if err != nil { - return n, fmt.Errorf("decoding Multi: %s", err) + return n, fmt.Errorf("decoding Multi: %w", err) } (*u.Things) = nil if l > 0 { @@ -634,7 +636,7 @@ if err != nil { nTmp, err = (*u.Things)[i].DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding Multi: %s", err) + return n, fmt.Errorf("decoding Multi: %w", err) } } } @@ -730,7 +732,7 @@ var _ decoderFrom = (*IntUnion2)(nil) // DecodeFrom decodes this value using the Decoder. func (s *IntUnion2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { if maxDepth == 0 { - return 0, errors.New("decoding IntUnion2: maximum decoding depth reached") + return 0, fmt.Errorf("decoding IntUnion2: %w", ErrMaxDecodingDepthReached) } maxDepth -= 1 var err error @@ -738,7 +740,7 @@ func (s *IntUnion2) DecodeFrom(d *xdr.Decoder, maxDepth uint) (int, error) { nTmp, err = (*IntUnion)(s).DecodeFrom(d, maxDepth) n += nTmp if err != nil { - return n, fmt.Errorf("decoding IntUnion: %s", err) + return n, fmt.Errorf("decoding IntUnion: %w", err) } return n, nil }