]> git.ozlabs.org Git - ccan/blobdiff - ccan/mem/mem.h
mem: add memtaint().
[ccan] / ccan / mem / mem.h
index 89b16d42a155990587bda108c5b970903f5c22b2..f2c3d5c7a6f795e2e091df8a545992c6533b50ec 100644 (file)
@@ -3,16 +3,19 @@
 #define CCAN_MEM_H
 
 #include "config.h"
+#include <ccan/compiler/compiler.h>
 
 #include <string.h>
 #include <stdbool.h>
 
 #if !HAVE_MEMMEM
+PURE_FUNCTION
 void *memmem(const void *haystack, size_t haystacklen,
             const void *needle, size_t needlelen);
 #endif
 
 #if !HAVE_MEMRCHR
+PURE_FUNCTION
 void *memrchr(const void *s, int c, size_t n);
 #endif
 
@@ -37,6 +40,7 @@ void *memrchr(const void *s, int c, size_t n);
  *     }
  *
  */
+PURE_FUNCTION
 void *mempbrkm(const void *data, size_t len, const void *accept, size_t accept_len);
 
 /**
@@ -57,6 +61,7 @@ void *mempbrkm(const void *data, size_t len, const void *accept, size_t accept_l
  *             printf("Nada\n");
  *     }
  */
+PURE_FUNCTION
 static inline char *mempbrk(const void *data, size_t len, const char *accept)
 {
        return mempbrkm(data, len, accept, strlen(accept));
@@ -81,6 +86,7 @@ static inline char *mempbrk(const void *data, size_t len, const char *accept)
  *             printf("Found %c after trimming spaces\n", *r);
  *     }
  */
+PURE_FUNCTION
 void *memcchr(void const *data, int c, size_t data_len);
 
 /**
@@ -95,6 +101,7 @@ void *memcchr(void const *data, int c, size_t data_len);
  *             printf("memory blocks are the same!\n");
  *     }
  */
+PURE_FUNCTION
 static inline bool memeq(const void *a, size_t al, const void *b, size_t bl)
 {
        return al == bl && !memcmp(a, b, bl);
@@ -114,6 +121,7 @@ static inline bool memeq(const void *a, size_t al, const void *b, size_t bl)
  *             printf("somebytes starts with otherbytes!\n");
  *     }
  */
+PURE_FUNCTION
 static inline bool memstarts(void const *data, size_t data_len,
                void const *prefix, size_t prefix_len)
 {
@@ -135,11 +143,25 @@ static inline bool memstarts(void const *data, size_t data_len,
  *             printf("somebytes == 'foo'!\n");
  *     }
  */
+PURE_FUNCTION
 static inline bool memeqstr(const void *data, size_t length, const char *string)
 {
        return memeq(data, length, string, strlen(string));
 }
 
+/**
+ * memeqzero - Is a byte array all zeroes?
+ * @data: byte array
+ * @length: length of @data in bytes
+ *
+ * Example:
+ *     if (memeqzero(somebytes, bytes_len)) {
+ *             printf("somebytes == 0!\n");
+ *     }
+ */
+PURE_FUNCTION
+bool memeqzero(const void *data, size_t length);
+
 /**
  * memstarts_str - Does this byte array start with a string prefix?
  * @a: byte array
@@ -151,6 +173,7 @@ static inline bool memeqstr(const void *data, size_t length, const char *string)
  *             printf("somebytes starts with 'It'\n");
  *     }
  */
+PURE_FUNCTION
 static inline bool memstarts_str(const void *a, size_t al, const char *s)
 {
        return memstarts(a, al, s, strlen(s));
@@ -166,10 +189,106 @@ static inline bool memstarts_str(const void *a, size_t al, const char *s)
  * Returns true if @suffix appears as a substring at the end of @s,
  * false otherwise.
  */
+PURE_FUNCTION
 static inline bool memends(const void *s, size_t s_len, const void *suffix, size_t suffix_len)
 {
        return (s_len >= suffix_len) && (memcmp((const char *)s + s_len - suffix_len,
                                                suffix, suffix_len) == 0);
 }
 
+/**
+ * memends_str - Does this byte array end with a string suffix?
+ * @a: byte array
+ * @al: length in bytes
+ * @s: string suffix
+ *
+ * Example:
+ *     if (memends_str(somebytes, bytes_len, "It")) {
+ *             printf("somebytes ends with with 'It'\n");
+ *     }
+ */
+PURE_FUNCTION
+static inline bool memends_str(const void *a, size_t al, const char *s)
+{
+       return memends(a, al, s, strlen(s));
+}
+
+/**
+ * memoverlaps - Do two memory ranges overlap?
+ * @a: pointer to first memory range
+ * @al: length of first memory range
+ * @b: pointer to second memory range
+ * @al: length of second memory range
+ */
+CONST_FUNCTION
+static inline bool memoverlaps(const void *a_, size_t al,
+                              const void *b_, size_t bl)
+{
+       const char *a = a_;
+       const char *b = b_;
+
+       return (a < (b + bl)) && (b < (a + al));
+}
+
+/*
+ * memswap - Exchange two memory regions
+ * @a: first region
+ * @b: second region
+ * @n: length of the regions
+ *
+ * Undefined results if the two memory regions overlap.
+ */
+void memswap(void *a, void *b, size_t n);
+
+#if HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+static inline void *memcheck_(const void *data, size_t len)
+{
+       VALGRIND_CHECK_MEM_IS_DEFINED(data, len);
+       return (void *)data;
+}
+#else
+static inline void *memcheck_(const void *data, size_t len)
+{
+       return (void *)data;
+}
+#endif
+
+#if HAVE_TYPEOF
+/**
+ * memcheck - check that a memory region is initialized
+ * @data: start of region
+ * @len: length in bytes
+ *
+ * When running under valgrind, this causes an error to be printed
+ * if the entire region is not defined.  Otherwise valgrind only
+ * reports an error when an undefined value is used for a branch, or
+ * written out.
+ *
+ * Example:
+ *     // Search for space, but make sure it's all initialized.
+ *     if (memchr(memcheck(somebytes, bytes_len), ' ', bytes_len)) {
+ *             printf("space was found!\n");
+ *     }
+ */
+#define memcheck(data, len) ((__typeof__((data)+0))memcheck_((data), (len)))
+#else
+#define memcheck(data, len) memcheck_((data), (len))
+#endif
+
+/**
+ * memtaint - mark a memory region unused
+ * @data: start of region
+ * @len: length in bytes
+ *
+ * This writes an "0xdeadbeef" eyecatcher repeatedly to the memory.
+ * When running under valgrind, it also tells valgrind that the memory is
+ * uninitialized, triggering valgrind errors if it is used for branches
+ * or written out (or passed to memcheck!) in future.
+ *
+ * Example:
+ *     // We'll reuse this buffer later, but be sure we don't access it.
+ *     memtaint(somebytes, bytes_len);
+ */
+void memtaint(void *data, size_t len);
 #endif /* CCAN_MEM_H */