From c9ad32bd9f670a64292de17d09f9ec8294a60f39 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Sun, 30 Jun 2024 02:46:12 +0800 Subject: [PATCH] encoding/asn1: unmarshal bool values correctly dealing with the ANY type Fixes #68241 Change-Id: I1ee81aa50c2f39f535ad27309e855f19acb2f2ea Reviewed-on: https://go-review.googlesource.com/c/go/+/595796 Auto-Submit: Roland Shoemaker Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Reviewed-by: Roland Shoemaker --- src/encoding/asn1/asn1.go | 2 ++ src/encoding/asn1/marshal_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/encoding/asn1/asn1.go b/src/encoding/asn1/asn1.go index 781ab87691..56e007d3a6 100644 --- a/src/encoding/asn1/asn1.go +++ b/src/encoding/asn1/asn1.go @@ -702,6 +702,8 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam if !t.isCompound && t.class == ClassUniversal { innerBytes := bytes[offset : offset+t.length] switch t.tag { + case TagBoolean: + result, err = parseBool(innerBytes) case TagPrintableString: result, err = parsePrintableString(innerBytes) case TagNumericString: diff --git a/src/encoding/asn1/marshal_test.go b/src/encoding/asn1/marshal_test.go index 64ce476400..dfd6d4e40b 100644 --- a/src/encoding/asn1/marshal_test.go +++ b/src/encoding/asn1/marshal_test.go @@ -311,6 +311,26 @@ func TestIssue11130(t *testing.T) { } } +func TestIssue68241(t *testing.T) { + for i, want := range []any{false, true} { + data, err := Marshal(want) + if err != nil { + t.Errorf("cannot Marshal: %v", err) + return + } + + var got any + _, err = Unmarshal(data, &got) + if err != nil { + t.Errorf("cannot Unmarshal: %v", err) + return + } + if !reflect.DeepEqual(got, want) { + t.Errorf("#%d Unmarshal, got: %v, want: %v", i, got, want) + } + } +} + func BenchmarkMarshal(b *testing.B) { b.ReportAllocs() -- 2.48.1