base64: Avoid false positive warning from Coverity.
authorBruno Haible <bruno@clisp.org>
Fri, 10 May 2019 14:08:38 +0000 (16:08 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 10 May 2019 14:09:25 +0000 (16:09 +0200)
Reported by Kamil Dudka <kdudka@redhat.com>.

Idea by Paul Eggert.

* lib/base64.c (base64_encode_fast, base64_encode): Add a no-op
'& 0x3f' to the array index expressions. This convinces Coverity that
there is no out-of-bounds array reference, regardless of the input.

ChangeLog
lib/base64.c

index afaf50787e986168cf0615d61401b4f6e67cf4fd..c9cb8930653b61a1d79a0778e230ba1cf8b9c6aa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2019-05-10  Bruno Haible  <bruno@clisp.org>
+
+       base64: Avoid false positive warning from Coverity.
+       Reported by Kamil Dudka <kdudka@redhat.com>.
+       Idea by Paul Eggert.
+       * lib/base64.c (base64_encode_fast, base64_encode): Add a no-op
+       '& 0x3f' to the array index expressions. This convinces Coverity that
+       there is no out-of-bounds array reference, regardless of the input.
+
 2019-05-09  Bruno Haible  <bruno@clisp.org>
 
        gettext: Update to gettext 0.20.
index f3f7298aa837b4bc523ed578fe0b4c2bd6262ba5..a00e0f46edd582270fbf06f70da387ff10aeb5b9 100644 (file)
@@ -70,7 +70,7 @@ base64_encode_fast (const char *restrict in, size_t inlen, char *restrict out)
 {
   while (inlen)
     {
-      *out++ = b64c[to_uchar (in[0]) >> 2];
+      *out++ = b64c[(to_uchar (in[0]) >> 2) & 0x3f];
       *out++ = b64c[((to_uchar (in[0]) << 4) + (to_uchar (in[1]) >> 4)) & 0x3f];
       *out++ = b64c[((to_uchar (in[1]) << 2) + (to_uchar (in[2]) >> 6)) & 0x3f];
       *out++ = b64c[to_uchar (in[2]) & 0x3f];
@@ -103,7 +103,7 @@ base64_encode (const char *restrict in, size_t inlen,
 
   while (inlen && outlen)
     {
-      *out++ = b64c[to_uchar (in[0]) >> 2];
+      *out++ = b64c[(to_uchar (in[0]) >> 2) & 0x3f];
       if (!--outlen)
         break;
       *out++ = b64c[((to_uchar (in[0]) << 4)