From c591ada0fb2b33b222878936c8aeb490ac140829 Mon Sep 17 00:00:00 2001 From: Javier Garcia Date: Mon, 20 Jun 2022 10:32:46 +0200 Subject: [PATCH] fix. nl.DeserializeRtNexthop return. Return a full created nl.RtNexthop ptr to avoid the "converted pointer straddles multiple allocations". UT by fasaxc. Co-authored-by: fasaxc Signed-off-by: Javier Garcia --- nl/route_linux.go | 4 +++- nl/route_linux_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/nl/route_linux.go b/nl/route_linux.go index 03c1900..c26f3bf 100644 --- a/nl/route_linux.go +++ b/nl/route_linux.go @@ -48,7 +48,9 @@ type RtNexthop struct { } func DeserializeRtNexthop(b []byte) *RtNexthop { - return (*RtNexthop)(unsafe.Pointer(&b[0:unix.SizeofRtNexthop][0])) + return &RtNexthop{ + RtNexthop: *((*unix.RtNexthop)(unsafe.Pointer(&b[0:unix.SizeofRtNexthop][0]))), + } } func (msg *RtNexthop) Len() int { diff --git a/nl/route_linux_test.go b/nl/route_linux_test.go index 4d405cc..58bc569 100644 --- a/nl/route_linux_test.go +++ b/nl/route_linux_test.go @@ -42,3 +42,26 @@ func TestRtMsgDeserializeSerialize(t *testing.T) { msg := DeserializeRtMsg(orig) testDeserializeSerialize(t, orig, safemsg, msg) } + +func TestDeserializeRtNexthop(t *testing.T) { + buf := make([]byte, unix.SizeofRtNexthop+64) + native := NativeEndian() + native.PutUint16(buf[0:2], unix.SizeofRtNexthop) + buf[2] = 17 + buf[3] = 1 + native.PutUint32(buf[4:8], 1234) + + msg := DeserializeRtNexthop(buf) + safemsg := &RtNexthop{ + unix.RtNexthop{ + Len: unix.SizeofRtNexthop, + Flags: 17, + Hops: 1, + Ifindex: 1234, + }, + nil, + } + if msg.Len() != safemsg.Len() || msg.Flags != safemsg.Flags || msg.Hops != safemsg.Hops || msg.Ifindex != safemsg.Ifindex { + t.Fatal("Deserialization failed.\nIn:", buf, "\nOut:", msg, "\n", msg.Serialize(), "\nExpected:", safemsg, "\n", safemsg.Serialize()) + } +}