mtd: tests: fix more potential integer overflows
authorBrian Norris <computersforpeace@gmail.com>
Sat, 28 Feb 2015 10:02:26 +0000 (02:02 -0800)
committerBrian Norris <computersforpeace@gmail.com>
Wed, 11 Mar 2015 22:21:47 +0000 (15:21 -0700)
Caught by Coverity (CID #200625 and others)

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Cc: Akinobu Mita <akinobu.mita@gmail.com>
drivers/mtd/tests/oobtest.c
drivers/mtd/tests/speedtest.c
drivers/mtd/tests/stresstest.c
drivers/mtd/tests/subpagetest.c
drivers/mtd/tests/torturetest.c

index 5e061186eab181c927d41568dd73b7cd9e486b25..64390ab88a802cca6e2cbea264f2a6691273bf01 100644 (file)
@@ -70,7 +70,7 @@ static int write_eraseblock(int ebnum)
        int i;
        struct mtd_oob_ops ops;
        int err = 0;
-       loff_t addr = ebnum * mtd->erasesize;
+       loff_t addr = (loff_t)ebnum * mtd->erasesize;
 
        prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
        for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
index 5ee9f7021020dae7a45a90e646a7afa12deeeb7d..a77019cb55101480b89ba96273ad303f9e997d53 100644 (file)
@@ -185,7 +185,7 @@ static long calc_speed(void)
             (finish.tv_usec - start.tv_usec) / 1000;
        if (ms == 0)
                return 0;
-       k = goodebcnt * (mtd->erasesize / 1024) * 1000;
+       k = (uint64_t)goodebcnt * (mtd->erasesize / 1024) * 1000;
        do_div(k, ms);
        return k;
 }
index c9d42cc2df1b5303804217f7621af9539cffc28f..d90c1c01dac6b957c1ae1d8cde43e62483cc852e 100644 (file)
@@ -96,7 +96,7 @@ static int do_read(void)
                if (offs + len > mtd->erasesize)
                        len = mtd->erasesize - offs;
        }
-       addr = eb * mtd->erasesize + offs;
+       addr = (loff_t)eb * mtd->erasesize + offs;
        return mtdtest_read(mtd, addr, len, readbuf);
 }
 
@@ -124,7 +124,7 @@ static int do_write(void)
                        offsets[eb + 1] = 0;
                }
        }
-       addr = eb * mtd->erasesize + offs;
+       addr = (loff_t)eb * mtd->erasesize + offs;
        err = mtdtest_write(mtd, addr, len, writebuf);
        if (unlikely(err))
                return err;
index 7b59ef522d5ea9b98431d376b4095451ba9b7f8b..0b835c631f9175301f6c3c1809930b37679d1189 100644 (file)
@@ -95,7 +95,7 @@ static int write_eraseblock2(int ebnum)
        loff_t addr = (loff_t)ebnum * mtd->erasesize;
 
        for (k = 1; k < 33; ++k) {
-               if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
+               if (addr + (subpgsize * k) > (loff_t)(ebnum + 1) * mtd->erasesize)
                        break;
                prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
                err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf);
@@ -195,7 +195,7 @@ static int verify_eraseblock2(int ebnum)
        loff_t addr = (loff_t)ebnum * mtd->erasesize;
 
        for (k = 1; k < 33; ++k) {
-               if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
+               if (addr + (subpgsize * k) > (loff_t)(ebnum + 1) * mtd->erasesize)
                        break;
                prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
                clear_data(readbuf, subpgsize * k);
index b55bc52a134084759b5dd61d22554faa1f0bc032..5045cf1b3160a892c5cedc7918ddbf3b8e60aef9 100644 (file)
@@ -101,11 +101,11 @@ static inline int check_eraseblock(int ebnum, unsigned char *buf)
 {
        int err, retries = 0;
        size_t read;
-       loff_t addr = ebnum * mtd->erasesize;
+       loff_t addr = (loff_t)ebnum * mtd->erasesize;
        size_t len = mtd->erasesize;
 
        if (pgcnt) {
-               addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
+               addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
                len = pgcnt * pgsize;
        }
 
@@ -155,11 +155,11 @@ static inline int write_pattern(int ebnum, void *buf)
 {
        int err;
        size_t written;
-       loff_t addr = ebnum * mtd->erasesize;
+       loff_t addr = (loff_t)ebnum * mtd->erasesize;
        size_t len = mtd->erasesize;
 
        if (pgcnt) {
-               addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
+               addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
                len = pgcnt * pgsize;
        }
        err = mtd_write(mtd, addr, len, &written, buf);