From 263832a455469405858797a0c0768590eed87afe Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 29 Sep 2022 08:33:35 +0200 Subject: [PATCH] std-aux: add nm_memcpy() helper for handling copy of zero bytes --- src/libnm-std-aux/nm-std-aux.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h index 5cce1096f0..c11be14422 100644 --- a/src/libnm-std-aux/nm-std-aux.h +++ b/src/libnm-std-aux/nm-std-aux.h @@ -501,6 +501,17 @@ nm_memeq(const void *s1, const void *s2, size_t len) return nm_memcmp(s1, s2, len) == 0; } +static inline void * +nm_memcpy(void *restrict dest, const void *restrict src, size_t n) +{ + /* Workaround undefined behavior in memcpy() with NULL pointers. */ + if (n == 0) + return dest; + + nm_assert(src); + return memcpy(dest, src, n); +} + /* * Very similar to g_str_has_prefix() with the obvious meaning. * Differences: