]> git.ozlabs.org Git - ccan/blobdiff - ccan/mem/mem.c
mem: add memtaint().
[ccan] / ccan / mem / mem.c
index 5eb15070ed3c3f9f5bf8d2dd8b0d0f060ffa1766..13027a2a7b0fec1b18ae386f9704d84aed41c5f1 100644 (file)
@@ -107,3 +107,22 @@ bool memeqzero(const void *data, size_t length)
        /* Now we know that's zero, memcmp with self. */
        return memcmp(data, p, length) == 0;
 }
+
+void memtaint(void *data, size_t len)
+{
+       /* Using 16 bytes is a bit quicker than 4 */
+       const unsigned tainter[]
+               = { 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef };
+       char *p = data;
+
+       while (len >= sizeof(tainter)) {
+               memcpy(p, tainter, sizeof(tainter));
+               p += sizeof(tainter);
+               len -= sizeof(tainter);
+       }
+       memcpy(p, tainter, len);
+
+#if HAVE_VALGRIND_MEMCHECK_H
+       VALGRIND_MAKE_MEM_UNDEFINED(data, len);
+#endif
+}