diff options
author | Thomas Haller <thaller@redhat.com> | 2022-07-14 18:14:07 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2022-07-20 10:24:45 +0200 |
commit | a242d41cc026d10ea11744d75ee3f5fdb834e464 (patch) | |
tree | 5a069848b0e62a6d67c8a780ccc13db3d6dc6452 | |
parent | 52c8ee2c9dfe080155d3129eba69d19119e296a1 (diff) | |
download | NetworkManager-a242d41cc026d10ea11744d75ee3f5fdb834e464.tar.gz |
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().
-rw-r--r-- | src/libnm-platform/nmp-object.c | 11 |
1 files changed, 11 insertions, 0 deletions
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; } |