]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: http: silence a cppcheck warning in get_http_auth()
authorWilly Tarreau <w@1wt.eu>
Wed, 2 Sep 2020 05:08:47 +0000 (07:08 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 2 Sep 2020 05:18:01 +0000 (07:18 +0200)
In issue #777, cppcheck wrongly assumes a useless null pointer check
in the expression below while it's obvious that in a 3G/1G split on
32-bit, len can become positive if p is NULL:

     p = memchr(ctx.value.ptr, ' ', ctx.value.len);
     len = p - ctx.value.ptr;
     if (!p || len <= 0)
           return 0;

In addition, on 64 bits you never know given that len is a 32-bit signed
int thus the sign of the result in case of a null p will always be the
opposite of the 32th bit of ctx.value.ptr. Admittedly the test is ugly.

Tim proposed this fix consisting in checking for p == ctx.value.ptr
instead when checking for first character only, which Ilya confirmed is
enough to shut cppcheck up. No backport is needed.

src/http_fetch.c

index 460bfbd349404563045efbce99d1f44366f332d5..2149b7fa2f81f16b363ab624e2c9dd32ed77eb73 100644 (file)
@@ -113,10 +113,10 @@ static int get_http_auth(struct sample *smp, struct htx *htx)
        if (!http_find_header(htx, hdr, &ctx, 0))
                return 0;
 
-       p   = memchr(ctx.value.ptr, ' ', ctx.value.len);
-       len = p - ctx.value.ptr;
-       if (!p || len <= 0)
+       p = memchr(ctx.value.ptr, ' ', ctx.value.len);
+       if (!p || p == ctx.value.ptr) /* if no space was found or if the space is the first character */
                return 0;
+       len = p - ctx.value.ptr;
 
        if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
                return 0;