From d8c53b12a818ff7cf82d06a1a69c395bdef85ee6 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Fri, 8 Jul 2016 17:20:23 +0000 Subject: -avoid calling memcpy() with NULL argument, even if len is 0 --- src/include/gnunet_common.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/include/gnunet_common.h') diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index 1e42af95c..52322e05e 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h @@ -771,6 +771,17 @@ GNUNET_ntoh_double (double d); */ #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type)) +/** + * Call memcpy() but check for @a n being 0 first. In the latter + * case, it is now safe to pass NULL for @a src or @a dst. + * Unlike traditional memcpy(), returns nothing. + * + * @param dst destination of the copy, may be NULL if @a n is zero + * @param src source of the copy, may be NULL if @a n is zero + * @param n number of bytes to copy + */ +#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) + /** * @ingroup memory * Allocate a size @a n array with structs or unions of the given @a type. @@ -879,12 +890,12 @@ GNUNET_ntoh_double (double d); * * static void push(struct foo * elem) { * GNUNET_array_grow(myVector, myVecLen, myVecLen+1); - * memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo)); + * GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo)); * } * * static void pop(struct foo * elem) { * if (myVecLen == 0) die(); - * memcpy(elem, myVector[myVecLen-1], sizeof(struct foo)); + * GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo)); * GNUNET_array_grow(myVector, myVecLen, myVecLen-1); * } * -- cgit v1.2.3