summaryrefslogtreecommitdiffhomepage
path: root/ir/adt
diff options
context:
space:
mode:
authorChristoph Mallon <mallon@cs.uni-saarland.de>2014-10-24 22:23:31 +0200
committerChristoph Mallon <mallon@cs.uni-saarland.de>2014-10-27 14:56:56 +0100
commit18489c39ee20b6b0f6cef5f79981ec70c042a77c (patch)
treeb09e9ac10c810a00bf944977fa807eb89c90e84d /ir/adt
parent79a69c11cb3ce8cba41d5bf8585c9f2775e5b2ee (diff)
util: Add an use MEMCPY().
Diffstat (limited to 'ir/adt')
-rw-r--r--ir/adt/pdeq.c5
-rw-r--r--ir/adt/util.h10
2 files changed, 13 insertions, 2 deletions
diff --git a/ir/adt/pdeq.c b/ir/adt/pdeq.c
index d391fbc..563c0e8 100644
--- a/ir/adt/pdeq.c
+++ b/ir/adt/pdeq.c
@@ -16,6 +16,7 @@
#include "fourcc.h"
#include "pdeq.h"
+#include "util.h"
#include "xmalloc.h"
/* Pointer Double Ended Queue */
@@ -401,11 +402,11 @@ void **pdeq_copyl(pdeq *dq, const void **dst)
if (n + p > NDATA) {
/* p is always < NDATA */
size_t nn = NDATA - p;
- memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
+ MEMCPY(d, &q->data[p], nn); d += nn;
p = 0; n -= nn;
}
- memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
+ MEMCPY(d, &q->data[p], n); d += n;
q = q->r;
}
diff --git a/ir/adt/util.h b/ir/adt/util.h
index 4a4c27c..adc154f 100644
--- a/ir/adt/util.h
+++ b/ir/adt/util.h
@@ -13,6 +13,7 @@
#define FIRM_ADT_UTIL_H
#include <stddef.h>
+#include <string.h>
/**
* Returns size of a static array. Warning: This returns invalid values for
@@ -49,4 +50,13 @@
#define QSORT_ARR(base, cmp) QSORT((base), ARR_LEN((base)), (cmp))
+static inline void *safe_memcpy(void* const dst, void const* const src, size_t const n)
+{
+ /* Calling memcpy with a null pointer leads to undefined behavior,
+ * even if we copy zero bytes (C99 7.21.1.p2). */
+ return n != 0 ? memcpy(dst, src, n) : dst;
+}
+
+#define MEMCPY(dst, src, n) safe_memcpy((dst), (src), (n) * sizeof(*(1 ? (dst) : (src))))
+
#endif