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 <javier.martin.garcia@ibm.com>
This commit is contained in:
Javier Garcia 2022-06-20 10:32:46 +02:00 committed by Alessandro Boch
parent 836d892b17
commit c591ada0fb
2 changed files with 26 additions and 1 deletions

View File

@ -48,7 +48,9 @@ type RtNexthop struct {
} }
func DeserializeRtNexthop(b []byte) *RtNexthop { 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 { func (msg *RtNexthop) Len() int {

View File

@ -42,3 +42,26 @@ func TestRtMsgDeserializeSerialize(t *testing.T) {
msg := DeserializeRtMsg(orig) msg := DeserializeRtMsg(orig)
testDeserializeSerialize(t, orig, safemsg, msg) 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())
}
}