Skip to contentSkip to conversation

util: Add l_safe_memcpy

PATCH Superseded Denis Kenzior 1 participant 1 message Oct 30, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util: Add l_safe_memcpy

Some callers use memcpy without checking whether the size 'n' is greater than zero. This is generally fine, but does cause sanitizers to complain. Add a new l_safe_memcpy function to take care of this case.

---
 ell/util.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
ell/util.h +10 -0
index c56f182292fa..af03f894513d 100644
@@ -243,6 +243,16 @@ static inline void l_put_be64(uint64_t val, void *ptr)
 void *l_malloc(size_t size) __attribute__ ((warn_unused_result, malloc));
 void *l_memdup(const void *mem, size_t size)
 			__attribute__ ((warn_unused_result, malloc));
+
+static inline void * __attribute__((nonnull(1,2))) l_safe_memcpy(void *dest,
+						const void *src, size_t n)
+{
+	if (!n)
+		return dest;
+
+	return __builtin_memcpy(dest, src, n);
+}
+
 void l_free(void *ptr);
 DEFINE_CLEANUP_FUNC(l_free);
 
Signature · 1 line
2.45.2