diff mbox series

[v2,1/2] util: Add l_safe_memcpy

Message ID 20241031161928.1514776-1-denkenz@gmail.com (mailing list archive)
State New
Headers show
Series [v2,1/2] util: Add l_safe_memcpy | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
prestwoj/iwd-ci-setupell success Prep - Setup ELL
prestwoj/iwd-ci-makedistcheck success Make Distcheck
prestwoj/iwd-ci-build success Build - Configure
prestwoj/iwd-ci-makecheckvalgrind success Make Check w/Valgrind
prestwoj/iwd-ci-makecheck success Make Check
prestwoj/iwd-ci-clang success clang PASS
prestwoj/iwd-ci-testrunner success test-runner PASS

Commit Message

Denis Kenzior Oct. 31, 2024, 4:19 p.m. UTC
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(+)

Comments

Marcel Holtmann Oct. 31, 2024, 7:15 p.m. UTC | #1
Hi Denis,

> 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(+)
> 
> diff --git a/ell/util.h b/ell/util.h
> index c56f182292fa..8a4e0950b41b 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_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);

why not just l_memcpy?

Regards

Marcel
diff mbox series

Patch

diff --git a/ell/util.h b/ell/util.h
index c56f182292fa..8a4e0950b41b 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_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);