From 0ac26711acf00789c137936e4cf0b7c2a6f1fed9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 15 Oct 2025 01:16:03 -0400 Subject: [PATCH] tape: Fix crash when decoding a bool sometimes --- tape/dynamic.go | 2 ++ tape/dynamic_test.go | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tape/dynamic.go b/tape/dynamic.go index 763d5db..1c80fa0 100644 --- a/tape/dynamic.go +++ b/tape/dynamic.go @@ -440,6 +440,8 @@ func setInt(destination reflect.Value, value int64, bytes int) { // setUint expects a settable destination. func setUint(destination reflect.Value, value uint64, bytes int) { switch { + case destination.Kind() == reflect.Bool: + destination.Set(reflect.ValueOf(value > 0)) case destination.CanInt(): destination.Set(reflect.ValueOf(int64(value)).Convert(destination.Type())) case destination.CanUint(): diff --git a/tape/dynamic_test.go b/tape/dynamic_test.go index 969183f..7aa301c 100644 --- a/tape/dynamic_test.go +++ b/tape/dynamic_test.go @@ -160,7 +160,7 @@ func TestDecodeWrongType(test *testing.T) { for index, data := range samplePayloads { test.Logf("data %2d %v [%s]", index, Tag(data[0]), tu.HexBytes(data[1:])) // integers should only assign to other integers - if index > 8 { + if index > 10 { cas := func(destination any) { n, err := DecodeAnyInto(NewDecoder(bytes.NewBuffer(data[1:])), destination, Tag(data[0])) if err != nil { test.Fatalf("error: %v | n: %d", err, n) } @@ -169,7 +169,7 @@ func TestDecodeWrongType(test *testing.T) { if reflectValue.Int() != 0 { test.Fatalf("destination not zero: %v", reflectValue.Elem().Interface()) } - } else { + } else if reflectValue.Kind() != reflect.Bool { if reflectValue.Uint() != 0 { test.Fatalf("destination not zero: %v", reflectValue.Elem().Interface()) } @@ -194,6 +194,8 @@ func TestDecodeWrongType(test *testing.T) { { var dest uint32; cas(&dest) } test.Log("- uint64") { var dest uint64; cas(&dest) } + test.Log("- bool") + { var dest bool; cas(&dest) } } arrayCase := func(destination any) { n, err := DecodeAnyInto(NewDecoder(bytes.NewBuffer(data[1:])), destination, Tag(data[0])) @@ -208,19 +210,19 @@ func TestDecodeWrongType(test *testing.T) { } } // SBA/LBA types should only assign to other SBA/LBA types - if index != 9 && index != 10 { + if index != 11 && index != 12 { test.Log("- string") { var dest string; arrayCase(&dest) } test.Log("- []byte") { var dest []byte; arrayCase(&dest) } } // arrays should only assign to other arrays - if index != 11 { + if index != 13 { test.Log("- []string") { var dest []string; arrayCase(&dest) } } // tables should only assign to other tables - if index != 12 && index != 13 { + if index != 14 && index != 15 { test.Log("- map[uint16] any") { var dest = map[uint16] any { }; arrayCase(&dest) } } @@ -245,6 +247,12 @@ func TestEncodeDecodeAnyTable(test *testing.T) { func TestEncodeDecodeAnyDestination(test *testing.T) { var destination any for index, data := range samplePayloads { + if _, isBool := sampleValues[index].(bool); isBool { + // test is invalid for bools because they are never + // created as a skeleton value + continue + } + tag := Tag(data[0]) payload := data[1:] test.Logf("data %2d %v [%s]", index, tag, tu.HexBytes(payload)) @@ -363,6 +371,12 @@ func TestTagAny(test *testing.T) { func TestDecodeAny(test *testing.T) { for index, payload := range samplePayloads { + if _, isBool := sampleValues[index].(bool); isBool { + // test is invalid for bools because they are never + // created as a skeleton value + continue + } + correctValue := sampleValues[index] data := payload[1:] decoder := NewDecoder(bytes.NewBuffer(data))