From a242d41cc026d10ea11744d75ee3f5fdb834e464 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 14 Jul 2022 18:14:07 +0200 Subject: [PATCH] platform: improve nmp_object_stackinit_id() for types that don't implement cmd_plobj_id_copy() An object type that doesn't implement cmd_plobj_id_copy(), either: - implements cmd_obj_copy(), but then we cannot copy the ID only to a stack instance, because that cannot track ownership. This is a bug in the caller. We cannot use stackinit for an object of a type that is not a plain old data (in C++ terms). - fallback to plain memcpy(). That is in line with nmp_object_clone(). and nmp_object_copy(). --- src/libnm-platform/nmp-object.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libnm-platform/nmp-object.c b/src/libnm-platform/nmp-object.c index d411043d0f..4c0b19aa9a 100644 --- a/src/libnm-platform/nmp-object.c +++ b/src/libnm-platform/nmp-object.c @@ -840,6 +840,17 @@ nmp_object_stackinit_id(NMPObject *obj, const NMPObject *src) _nmp_object_stackinit_from_class(obj, klass); if (klass->cmd_plobj_id_copy) klass->cmd_plobj_id_copy(&obj->object, &src->object); + else { + /* This object must not implement cmd_obj_copy(). + * If it would, it would mean that we require a deep copy + * of the data. As @obj is stack-allocated, it cannot track + * ownership. The caller must not use nmp_object_stackinit_id() + * with an object of such a type. */ + nm_assert(!klass->cmd_obj_copy); + + /* plain memcpy of the public part suffices. */ + memcpy(&obj->object, &src->object, klass->sizeof_data); + } return obj; }