From 76409d07a3e6bf0d1ee63ec7bff6f4c3114a4e01 Mon Sep 17 00:00:00 2001 From: adman Date: Sun, 12 Apr 2026 23:16:36 +0200 Subject: [PATCH] fix smart convert panic --- proto/smart_convert.go | 3 ++- proto/smart_convert_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/proto/smart_convert.go b/proto/smart_convert.go index ded0f74..0be44a9 100644 --- a/proto/smart_convert.go +++ b/proto/smart_convert.go @@ -47,7 +47,8 @@ func SmartConvert(from reflect.Value, to reflect.Type) (ret reflect.Value) { if from.IsNil() { return reflect.Zero(to) } - ret = SmartConvert(from.Elem(), to.Elem()).Addr() + ret = reflect.New(to.Elem()) + ret.Elem().Set(SmartConvert(from.Elem(), to.Elem())) case reflect.Slice: l := from.Len() ret = reflect.MakeSlice(to, l, l) diff --git a/proto/smart_convert_test.go b/proto/smart_convert_test.go index b7f375b..49ef6d0 100644 --- a/proto/smart_convert_test.go +++ b/proto/smart_convert_test.go @@ -46,3 +46,23 @@ func TestRegisterUntilLatest(t *testing.T) { t.Errorf("handler not called") } } + +var TestData = []struct{ V, T, E any }{ + {V: &v1_21_9.PlayToClientPacketPosition{Flags: v1_21_9.PlayToClientPositionUpdateRelatives{Val: 2}, X: 5}, T: &v1_21_8.PlayToClientPacketPosition{}}, + {V: &v1_21_9.ConfigurationToClientPacketTags{}, T: &v1_21_8.ConfigurationToClientPacketTags{}}, + {V: &v1_21_11.PlayToServerPacketChatMessage{Signature: &[256]byte{}}, T: &v1_21_8.PlayToServerPacketChatMessage{}}, +} + +func TestSmartConvert(t *testing.T) { + for _, d := range TestData { + can := proto.SmartConvertibleTo(reflect.TypeOf(d.V), reflect.TypeOf(d.T)) + if !can { + t.Errorf("can't smart convert %#v to %#v", d.V, d.T) + continue + } + got := proto.SmartConvert(reflect.ValueOf(d.V), reflect.TypeOf(d.T)) + if d.E != nil && !reflect.DeepEqual(got.Interface(), d.E) { + t.Errorf("got %#v, want %#v", got.Interface(), d.E) + } + } +}