Replace int -> uint to avoid signed integer overflow
authorRostislav Skudnov <rostislav@tuxera.com>
Wed, 1 Feb 2017 18:35:13 +0000 (18:35 +0000)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 4 Feb 2017 22:10:22 +0000 (23:10 +0100)
An example of such an error (should be compiled with DEBUG_SANITIZE):

runtime error: left shift of 1 by 31 places cannot be represented in
type 'int'

Signed-off-by: Rostislav Skudnov <rostislav@tuxera.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
archival/libarchive/decompress_bunzip2.c
libbb/crc32.c
libbb/getopt32.c
libbb/pw_encrypt.c
miscutils/rx.c

index fe5953da2412df5c428e281c3c50a86d735db735..4fb989c29fe47930b25f1a1b22f259c5f57b75bc 100644 (file)
@@ -134,7 +134,7 @@ static unsigned get_bits(bunzip_data *bd, int bits_wanted)
 
                /* Avoid 32-bit overflow (dump bit buffer to top of output) */
                if (bit_count >= 24) {
-                       bits = bd->inbufBits & ((1 << bit_count) - 1);
+                       bits = bd->inbufBits & ((1U << bit_count) - 1);
                        bits_wanted -= bit_count;
                        bits <<= bits_wanted;
                        bit_count = 0;
@@ -158,11 +158,11 @@ static int get_next_block(bunzip_data *bd)
 {
        struct group_data *hufGroup;
        int dbufCount, dbufSize, groupCount, *base, *limit, selector,
-               i, j, t, runPos, symCount, symTotal, nSelectors, byteCount[256];
+               i, j, runPos, symCount, symTotal, nSelectors, byteCount[256];
        int runCnt = runCnt; /* for compiler */
        uint8_t uc, symToByte[256], mtfSymbol[256], *selectors;
        uint32_t *dbuf;
-       unsigned origPtr;
+       unsigned origPtr, t;
 
        dbuf = bd->dbuf;
        dbufSize = bd->dbufSize;
index ac9836cc9e61b185e08687e75c73c380fc751762..0711ca84eac4f87cbd841d8e0e6bd68b3e31f1bd 100644 (file)
@@ -24,7 +24,7 @@ uint32_t* FAST_FUNC crc32_filltable(uint32_t *crc_table, int endian)
 {
        uint32_t polynomial = endian ? 0x04c11db7 : 0xedb88320;
        uint32_t c;
-       int i, j;
+       unsigned i, j;
 
        if (!crc_table)
                crc_table = xmalloc(256 * sizeof(uint32_t));
index 15b6efc096ba0ef04f409bcd27f37a6bfece232c..497fc016f5365d5337c455e47a0167ee22656af3 100644 (file)
@@ -404,7 +404,7 @@ getopt32(char **argv, const char *applet_opts, ...)
                if (c >= 32)
                        break;
                on_off->opt_char = *s;
-               on_off->switch_on = (1 << c);
+               on_off->switch_on = (1U << c);
                if (*++s == ':') {
                        on_off->optarg = va_arg(p, void **);
                        if (s[1] == '+' || s[1] == '*') {
@@ -454,7 +454,7 @@ getopt32(char **argv, const char *applet_opts, ...)
                        if (c >= 32)
                                break;
                        on_off->opt_char = l_o->val;
-                       on_off->switch_on = (1 << c);
+                       on_off->switch_on = (1U << c);
                        if (l_o->has_arg != no_argument)
                                on_off->optarg = va_arg(p, void **);
                        c++;
index 4cdc2de7635f101111244040ac2e5c16d5c10513..fe06a8fe68f920d40239b9807d64591fc09853fb 100644 (file)
@@ -30,7 +30,7 @@ static int i64c(int i)
 int FAST_FUNC crypt_make_salt(char *p, int cnt /*, int x */)
 {
        /* was: x += ... */
-       int x = getpid() + monotonic_us();
+       unsigned x = getpid() + monotonic_us();
        do {
                /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
                 * (low-order bit is not "random", etc...),
index 36fc20a72e978270ad3e158475d0bf4ccd0ec87c..1f6f2825ccc269b86c6b4acb79b4c4cbfc69ab11 100644 (file)
@@ -94,7 +94,7 @@ static int receive(/*int read_fd, */int file_fd)
                int blockBegin;
                int blockNo, blockNoOnesCompl;
                int cksum_or_crc;
-               int expected;
+               unsigned expected;
                int i, j;
 
                blockBegin = read_byte(timeout);