From b68fa57c599720d33a2d735782969ce95eabf794 Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Tue, 12 May 2020 11:06:42 -0700 Subject: [PATCH] encoding/asn1: document what Unmarshal returns in rest Specifically, this change documents the behavior of Unmarshal when a SEQUENCE contains trailing elements. For context Unmarshal treats trailing elements of a SEQUENCE that do not have matching struct fields as valid, as this is how ASN.1 structures are typically extended. This can be somewhat confusing as you might expect those elements to be appended to rest, but rest is really only for trailing data unrelated to the structure being parsed (i.e. if you append a second sequence to b, it would be returned in rest). Fixes #35680 Change-Id: Ia2c68b2f7d8674d09e859b4b7f9aff327da26fa0 Reviewed-on: https://go-review.googlesource.com/c/go/+/233537 Run-TryBot: Emmanuel Odeke TryBot-Result: Gobot Gobot Reviewed-by: Katie Hockman --- src/encoding/asn1/asn1.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/encoding/asn1/asn1.go b/src/encoding/asn1/asn1.go index 90ba5775af..d809dde278 100644 --- a/src/encoding/asn1/asn1.go +++ b/src/encoding/asn1/asn1.go @@ -1037,6 +1037,12 @@ func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) { // Because Unmarshal uses the reflect package, the structs // being written to must use upper case field names. // +// After parsing b, any bytes that were leftover and not used to fill +// val will be returned in rest. When parsing a SEQUENCE into a struct, +// any trailing elements of the SEQUENCE that do not have matching +// fields in val will not be included in rest, as these are considered +// valid elements of the SEQUENCE and not trailing data. +// // An ASN.1 INTEGER can be written to an int, int32, int64, // or *big.Int (from the math/big package). // If the encoded value does not fit in the Go type, -- 2.48.1