]> git.ozlabs.org Git - ccan/commitdiff
mem: Add function to check whether memory ranges overlap
authorDavid Gibson <david@gibson.dropbear.id.au>
Wed, 9 Sep 2015 08:04:33 +0000 (18:04 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Sun, 13 Sep 2015 09:17:14 +0000 (19:17 +1000)
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>
ccan/mem/mem.h
ccan/mem/test/api.c

index 1afe508b3a097beefdbf93660165c7146fdb3f25..99c34c0a39719ec0a262add21e74bab5b99a8e56 100644 (file)
@@ -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 */
index d0178c355e06f7c139a94151f6c03fb083811a35..a584c0f33e3b97a11974e55e51670991db4a6bdd 100644 (file)
@@ -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();
 }