]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Fixed bad shift operation warning in deflatePrime.
authorNathan Moinvaziri <nathan@nathanm.com>
Sun, 28 Jun 2020 20:05:11 +0000 (13:05 -0700)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 1 Jul 2020 21:06:53 +0000 (23:06 +0200)
Check that bits value is not greater than bits allowed by value type.

  CID 293475 (#2-4 of 4): Bad bit shift operation (BAD_SHIFT)
  In expression 1UL << put, left shifting by more than 63 bits has undefined behavior.

deflate.c

index f224952ca7c51be1c2cb5e6606b89f957e6354c7..2a77a9f751c2b4968a6776a9f56b97a4ac04edb6 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -586,14 +586,17 @@ int32_t ZEXPORT PREFIX(deflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_
     if (deflateStateCheck(strm))
         return Z_STREAM_ERROR;
     s = strm->state;
-    if (bits < 0 || bits > BIT_BUF_SIZE ||
+    if (bits < 0 || bits > BIT_BUF_SIZE || bits > (sizeof(value) << 3) ||
         s->sym_buf < s->pending_out + ((BIT_BUF_SIZE + 7) >> 3))
         return Z_BUF_ERROR;
     do {
         put = BIT_BUF_SIZE - s->bi_valid;
         if (put > bits)
             put = bits;
-        s->bi_buf |= (((uint64_t)value & ((UINT64_C(1) << put) - 1)) << s->bi_valid);
+        if (s->bi_valid == 0)
+            s->bi_buf = (uint64_t)value;
+        else
+            s->bi_buf |= (((uint64_t)value & ((UINT64_C(1) << put) - 1)) << s->bi_valid);
         s->bi_valid += put;
         zng_tr_flush_bits(s);
         value >>= put;