tape: Fix crash when decoding a bool sometimes
This commit is contained in:
parent
8446ae6186
commit
0ac26711ac
@ -440,6 +440,8 @@ func setInt(destination reflect.Value, value int64, bytes int) {
|
|||||||
// setUint expects a settable destination.
|
// setUint expects a settable destination.
|
||||||
func setUint(destination reflect.Value, value uint64, bytes int) {
|
func setUint(destination reflect.Value, value uint64, bytes int) {
|
||||||
switch {
|
switch {
|
||||||
|
case destination.Kind() == reflect.Bool:
|
||||||
|
destination.Set(reflect.ValueOf(value > 0))
|
||||||
case destination.CanInt():
|
case destination.CanInt():
|
||||||
destination.Set(reflect.ValueOf(int64(value)).Convert(destination.Type()))
|
destination.Set(reflect.ValueOf(int64(value)).Convert(destination.Type()))
|
||||||
case destination.CanUint():
|
case destination.CanUint():
|
||||||
|
|||||||
@ -160,7 +160,7 @@ func TestDecodeWrongType(test *testing.T) {
|
|||||||
for index, data := range samplePayloads {
|
for index, data := range samplePayloads {
|
||||||
test.Logf("data %2d %v [%s]", index, Tag(data[0]), tu.HexBytes(data[1:]))
|
test.Logf("data %2d %v [%s]", index, Tag(data[0]), tu.HexBytes(data[1:]))
|
||||||
// integers should only assign to other integers
|
// integers should only assign to other integers
|
||||||
if index > 8 {
|
if index > 10 {
|
||||||
cas := func(destination any) {
|
cas := func(destination any) {
|
||||||
n, err := DecodeAnyInto(NewDecoder(bytes.NewBuffer(data[1:])), destination, Tag(data[0]))
|
n, err := DecodeAnyInto(NewDecoder(bytes.NewBuffer(data[1:])), destination, Tag(data[0]))
|
||||||
if err != nil { test.Fatalf("error: %v | n: %d", err, n) }
|
if err != nil { test.Fatalf("error: %v | n: %d", err, n) }
|
||||||
@ -169,7 +169,7 @@ func TestDecodeWrongType(test *testing.T) {
|
|||||||
if reflectValue.Int() != 0 {
|
if reflectValue.Int() != 0 {
|
||||||
test.Fatalf("destination not zero: %v", reflectValue.Elem().Interface())
|
test.Fatalf("destination not zero: %v", reflectValue.Elem().Interface())
|
||||||
}
|
}
|
||||||
} else {
|
} else if reflectValue.Kind() != reflect.Bool {
|
||||||
if reflectValue.Uint() != 0 {
|
if reflectValue.Uint() != 0 {
|
||||||
test.Fatalf("destination not zero: %v", reflectValue.Elem().Interface())
|
test.Fatalf("destination not zero: %v", reflectValue.Elem().Interface())
|
||||||
}
|
}
|
||||||
@ -194,6 +194,8 @@ func TestDecodeWrongType(test *testing.T) {
|
|||||||
{ var dest uint32; cas(&dest) }
|
{ var dest uint32; cas(&dest) }
|
||||||
test.Log("- uint64")
|
test.Log("- uint64")
|
||||||
{ var dest uint64; cas(&dest) }
|
{ var dest uint64; cas(&dest) }
|
||||||
|
test.Log("- bool")
|
||||||
|
{ var dest bool; cas(&dest) }
|
||||||
}
|
}
|
||||||
arrayCase := func(destination any) {
|
arrayCase := func(destination any) {
|
||||||
n, err := DecodeAnyInto(NewDecoder(bytes.NewBuffer(data[1:])), destination, Tag(data[0]))
|
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
|
// SBA/LBA types should only assign to other SBA/LBA types
|
||||||
if index != 9 && index != 10 {
|
if index != 11 && index != 12 {
|
||||||
test.Log("- string")
|
test.Log("- string")
|
||||||
{ var dest string; arrayCase(&dest) }
|
{ var dest string; arrayCase(&dest) }
|
||||||
test.Log("- []byte")
|
test.Log("- []byte")
|
||||||
{ var dest []byte; arrayCase(&dest) }
|
{ var dest []byte; arrayCase(&dest) }
|
||||||
}
|
}
|
||||||
// arrays should only assign to other arrays
|
// arrays should only assign to other arrays
|
||||||
if index != 11 {
|
if index != 13 {
|
||||||
test.Log("- []string")
|
test.Log("- []string")
|
||||||
{ var dest []string; arrayCase(&dest) }
|
{ var dest []string; arrayCase(&dest) }
|
||||||
}
|
}
|
||||||
// tables should only assign to other tables
|
// tables should only assign to other tables
|
||||||
if index != 12 && index != 13 {
|
if index != 14 && index != 15 {
|
||||||
test.Log("- map[uint16] any")
|
test.Log("- map[uint16] any")
|
||||||
{ var dest = map[uint16] any { }; arrayCase(&dest) }
|
{ var dest = map[uint16] any { }; arrayCase(&dest) }
|
||||||
}
|
}
|
||||||
@ -245,6 +247,12 @@ func TestEncodeDecodeAnyTable(test *testing.T) {
|
|||||||
func TestEncodeDecodeAnyDestination(test *testing.T) {
|
func TestEncodeDecodeAnyDestination(test *testing.T) {
|
||||||
var destination any
|
var destination any
|
||||||
for index, data := range samplePayloads {
|
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])
|
tag := Tag(data[0])
|
||||||
payload := data[1:]
|
payload := data[1:]
|
||||||
test.Logf("data %2d %v [%s]", index, tag, tu.HexBytes(payload))
|
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) {
|
func TestDecodeAny(test *testing.T) {
|
||||||
for index, payload := range samplePayloads {
|
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]
|
correctValue := sampleValues[index]
|
||||||
data := payload[1:]
|
data := payload[1:]
|
||||||
decoder := NewDecoder(bytes.NewBuffer(data))
|
decoder := NewDecoder(bytes.NewBuffer(data))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user