summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2015-09-09 18:04:33 +1000
committerDavid Gibson <david@gibson.dropbear.id.au>2015-09-13 19:17:14 +1000
commit8f6126000bfebc71f566ce56387f86fcd567e2c7 (patch)
treef1371638dbe3623805f815557d086605e14dd534
parenteeaa2c8b0d5db1128c873cad3bfcc6fd6b1858fe (diff)
mem: Add function to check whether memory ranges overlap
The test is simple, but every time I do it by hand, I always spend ages convincing myself it's actually correct. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r--ccan/mem/mem.h17
-rw-r--r--ccan/mem/test/api.c23
2 files changed, 39 insertions, 1 deletions
diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h
index 1afe508b..99c34c0a 100644
--- a/ccan/mem/mem.h
+++ b/ccan/mem/mem.h
@@ -200,4 +200,21 @@ 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));
+}
+
#endif /* CCAN_MEM_H */
diff --git a/ccan/mem/test/api.c b/ccan/mem/test/api.c
index d0178c35..a584c0f3 100644
--- a/ccan/mem/test/api.c
+++ b/ccan/mem/test/api.c
@@ -11,7 +11,7 @@ int main(void)
char scan2[] = "\0\0\0b";
/* This is how many tests you plan to run */
- plan_tests(46);
+ plan_tests(60);
ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
@@ -75,6 +75,27 @@ int main(void)
ok1(!memends_str(S("a\0bcdef"), "a"));
ok1(memends_str(S("a\0bcdef"), "ef"));
+ ok1(!memoverlaps(haystack1, sizeof(haystack1),
+ haystack2, sizeof(haystack2)));
+ ok1(!memoverlaps(haystack2, sizeof(haystack2),
+ haystack1, sizeof(haystack1)));
+ ok1(memoverlaps(haystack1, sizeof(haystack1), haystack1, 1));
+ ok1(memoverlaps(haystack1, 1, haystack1, sizeof(haystack1)));
+ ok1(memoverlaps(haystack1, sizeof(haystack1),
+ haystack1 + sizeof(haystack1) - 1, 1));
+ ok1(memoverlaps(haystack1 + sizeof(haystack1) - 1, 1,
+ haystack1, sizeof(haystack1)));
+ ok1(!memoverlaps(haystack1, sizeof(haystack1),
+ haystack1 + sizeof(haystack1), 1));
+ ok1(!memoverlaps(haystack1 + sizeof(haystack1), 1,
+ haystack1, sizeof(haystack1)));
+ ok1(!memoverlaps(haystack1, sizeof(haystack1), haystack1 - 1, 1));
+ ok1(!memoverlaps(haystack1 - 1, 1, haystack1, sizeof(haystack1)));
+ ok1(memoverlaps(haystack1, 5, haystack1 + 4, 7));
+ ok1(!memoverlaps(haystack1, 5, haystack1 + 5, 6));
+ ok1(memoverlaps(haystack1 + 4, 7, haystack1, 5));
+ ok1(!memoverlaps(haystack1 + 5, 6, haystack1, 5));
+
/* This exits depending on whether all tests passed */
return exit_status();
}