diff mbox series

[v3,2/3] util: Add l_memcpy

Message ID 20241031202627.1548592-2-denkenz@gmail.com (mailing list archive)
State New
Headers show
Series [v3,1/3] timeout: add l_timeout_remaining | expand

Commit Message

Denis Kenzior Oct. 31, 2024, 8:26 p.m. UTC
Some callers use memcpy without checking whether the size 'n' is 0 and
src is NULL.  'src' is declared as non-null and can thus cause
sanitizers to complain.  Having 'src' as NULL is fine as long as 'n' is
0.  Add a new l_memcpy function to take care of this case.
---
 ell/util.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

Comments

Denis Kenzior Oct. 31, 2024, 9:45 p.m. UTC | #1
On 10/31/24 3:26 PM, Denis Kenzior wrote:
> Some callers use memcpy without checking whether the size 'n' is 0 and
> src is NULL.  'src' is declared as non-null and can thus cause
> sanitizers to complain.  Having 'src' as NULL is fine as long as 'n' is
> 0.  Add a new l_memcpy function to take care of this case.
> ---
>   ell/util.h | 10 ++++++++++
>   1 file changed, 10 insertions(+)
> 

Applied
diff mbox series

Patch

diff --git a/ell/util.h b/ell/util.h
index c56f182292fa..2a0ee0e50b93 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -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))) l_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);