From 59afdfa3a37d1c06a7f1b00aed196b7e5a78127d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Sch=C3=B6ll?= Date: Thu, 9 Nov 2023 15:48:19 +0100 Subject: [PATCH] improve the error handling when decoding table rows with variant types (#214) * add bound check for variant types * handle alias, improve error message * add changelog --- CHANGELOG.md | 2 ++ abidecoder.go | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2cc0d15..92ff0383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### Fixed +* Improved the error handling when decoding table rows with variant types. + * Fixed decoding of table rows with variant types. * Fixed serialization of `map[K]V` when using `eos.MarshalBinary` so that ordering in which the keys are serialized is in lexicographical order, just like JSON serialization. diff --git a/abidecoder.go b/abidecoder.go index a316c21e..58e73512 100644 --- a/abidecoder.go +++ b/abidecoder.go @@ -81,11 +81,13 @@ func (a *ABI) decode(binaryDecoder *Decoder, structName string) (map[string]inte } if variant := a.VariantForName(structName); variant != nil { - out, err := binaryDecoder.ReadUvarint32() + variantIndex, err := binaryDecoder.ReadUvarint32() if err != nil { - zlog.Error("error reading variant", zap.Error(err)) + return nil, fmt.Errorf("error reading variant: %w", err) + } else if int(variantIndex) >= len(variant.Types) { + return nil, fmt.Errorf("variant type index is unknown, got type index %d, know up to index %d", variantIndex, len(variant.Types)-1) } - structName = variant.Types[out] + structName, _ = a.TypeNameForNewTypeName(variant.Types[variantIndex]) } structure := a.StructForName(structName)