]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kr_bitcmp: add meaning to NULL inputs
authorVladimír Čunát <vladimir.cunat@nic.cz>
Fri, 17 Mar 2017 11:51:42 +0000 (12:51 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 21 Mar 2017 14:52:21 +0000 (15:52 +0100)
Reasoning: we currently only use the function from lua modules and nil
values are very common there; I want to pick these changes to a bugfix
update without extensive checking whether the modules might pass
invalid input if user passes invalid config and thus introduce new
crashes.  The checks also seem cheap performance-wise.

lib/utils.c

index 3e53c6cdbae734ce7fd77389998ba18ba994e41f..b1c226ab8fac6a1fd0a8ee7045f7ddb7f3445a74 100644 (file)
@@ -364,6 +364,18 @@ int kr_straddr_subnet(void *dst, const char *addr)
 
 int kr_bitcmp(const char *a, const char *b, int bits)
 {
+       /* We're using the function from lua directly, so at least for now
+        * we avoid crashing on bogus inputs.  Meaning: NULL is ordered before
+        * anything else, and negative length is the same as zero.
+        * TODO: review the call sites and probably remove the checks. */
+       if (bits <= 0 || (!a && !b)) {
+               return 0;
+       } else if (!a) {
+               return -1;
+       } else if (!b) {
+               return 1;
+       }
+
        assert(a && b && bits >= 0  ||  bits == 0);
        /* Compare part byte-divisible part. */
        const size_t chunk = bits / 8;