aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2014-06-24 14:52:42 +0200
committerTobias Brunner <tobias@strongswan.org>2014-06-24 15:11:27 +0200
commit6d4654b9f91adf4effadebe0c7a4e5cfc91eb32b (patch)
treef9eb253e0fcf0ca980a522b4ec9bbd11300d2471 /src
parentbb91109af82531b45e421568b5aeb8f9e4b6731e (diff)
downloadstrongswan-6d4654b9f91adf4effadebe0c7a4e5cfc91eb32b.tar.bz2
strongswan-6d4654b9f91adf4effadebe0c7a4e5cfc91eb32b.tar.xz
utils: Add wrappers for memcpy(3), memmove(3) and memset(3)
These wrappers guarantee that calls to these functions are noops if the number of bytes is 0, as calling them with NULL pointers is undefined according to the C standard, even if the number of bytes is 0 (most implementations probably ignore the pointers anyway in this case, but lets make sure).
Diffstat (limited to 'src')
-rw-r--r--src/libstrongswan/utils/utils.h34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/libstrongswan/utils/utils.h b/src/libstrongswan/utils/utils.h
index 961ddb583..3c1a203a1 100644
--- a/src/libstrongswan/utils/utils.h
+++ b/src/libstrongswan/utils/utils.h
@@ -167,6 +167,39 @@ static inline bool memeq(const void *x, const void *y, size_t len)
}
/**
+ * Calling memcpy() with NULL pointers, even with n == 0, results in undefined
+ * behavior according to the C standard. This version is guaranteed to not
+ * access the pointers if n is 0.
+ */
+static inline void *memcpy_noop(void *dst, const void *src, size_t n)
+{
+ return n ? memcpy(dst, src, n) : dst;
+}
+#define memcpy(d,s,n) memcpy_noop(d,s,n)
+
+/**
+ * Calling memmove() with NULL pointers, even with n == 0, results in undefined
+ * behavior according to the C standard. This version is guaranteed to not
+ * access the pointers if n is 0.
+ */
+static inline void *memmove_noop(void *dst, const void *src, size_t n)
+{
+ return n ? memmove(dst, src, n) : dst;
+}
+#define memmove(d,s,n) memmove_noop(d,s,n)
+
+/**
+ * Calling memset() with a NULL pointer, even with n == 0, results in undefined
+ * behavior according to the C standard. This version is guaranteed to not
+ * access the pointer if n is 0.
+ */
+static inline void *memset_noop(void *s, int c, size_t n)
+{
+ return n ? memset(s, c, n) : s;
+}
+#define memset(s,c,n) memset_noop(s,c,n)
+
+/**
* Macro gives back larger of two values.
*/
#define max(x,y) ({ \
@@ -174,7 +207,6 @@ static inline bool memeq(const void *x, const void *y, size_t len)
typeof(y) _y = (y); \
_x > _y ? _x : _y; })
-
/**
* Macro gives back smaller of two values.
*/