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.
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;