]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kr_bitcmp: adjust semantics -> memcmp
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 15 Mar 2017 13:55:39 +0000 (14:55 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 21 Mar 2017 14:52:21 +0000 (15:52 +0100)
Motivation: allow bits=0 and consequently 0.0.0.0/0 matches in view
and renumber modules.
https://gitter.im/CZ-NIC/knot-resolver?at=58c940c721d548df2cdfda5e

We shouldn't mix up error codes with valid results from memcmp;
let's just segfault if someone passes a NULL, just as memcmp() itself.

lib/utils.c
lib/utils.h

index 90ddfdbe9287d8f0091e61d84b6347224c6cecb3..3e53c6cdbae734ce7fd77389998ba18ba994e41f 100644 (file)
@@ -364,9 +364,7 @@ int kr_straddr_subnet(void *dst, const char *addr)
 
 int kr_bitcmp(const char *a, const char *b, int bits)
 {
-       if (!a || !b || bits == 0) {
-               return kr_error(ENOMEM);
-       }
+       assert(a && b && bits >= 0  ||  bits == 0);
        /* Compare part byte-divisible part. */
        const size_t chunk = bits / 8;
        int ret = memcmp(a, b, chunk);
index eb82180248bd376d305ec23b64eef2b92483fd94..5100882b73b6a4556d5fd9ee34303e3cf94df2df 100644 (file)
@@ -169,7 +169,10 @@ struct sockaddr * kr_straddr_socket(const char *addr, int port);
   * @warning 'dst' must be at least `sizeof(struct in6_addr)` long. */
 KR_EXPORT
 int kr_straddr_subnet(void *dst, const char *addr);
-/** Compare memory bitwise. */
+
+/** Compare memory bitwise.  The semantics is "the same" as for memcmp().
+ *  The partial byte is considered with more-significant bits first,
+ *  so this is e.g. suitable for comparing IP prefixes. */
 KR_EXPORT KR_PURE
 int kr_bitcmp(const char *a, const char *b, int bits);