]> granicus.if.org Git - zziplib/commitdiff
zzip: string: fix comparison with different signs
authorPatrick Steinhardt <ps@pks.im>
Tue, 21 May 2019 11:10:22 +0000 (13:10 +0200)
committerPatrick Steinhardt <ps@pks.im>
Thu, 1 Aug 2019 06:33:34 +0000 (08:33 +0200)
When subtracting two pointers, the result is signed as the subtrahend
may point at a position after the minuend, in which case it is bigger
and the result would be negative. This causes a warning in
`_zzip_strnlen`, which is supposed to return an unsigned `size_t`.

As the minuend is computed by using `memchr` on the subtrahend, we know
that it is always either `NULL` or bigger than the subtrahend. And due
to the subtraction only being performed when it is non-`NULL`, we may
simply cast the result to `size_t` to fix the issue, fixing the warning.

zzip/__string.h

index 4424348aa6531ba3c8b86d144a1a5ebb1622bb2d..e62fb06564cde9155a6fcb36d191230ebaab960f 100644 (file)
@@ -23,7 +23,7 @@ static size_t
 _zzip_strnlen(const char *p, size_t maxlen)
 {
     const char * stop = (char *)memchr(p, '\0', maxlen);
-    return stop ? stop - p : maxlen;
+    return stop ? (size_t)(stop - p) : maxlen;
 }
 #endif