Commit b9da8bae authored by Brian Norris's avatar Brian Norris

mtd: tests: fix more potential integer overflows

Caught by Coverity (CID #200625 and others)
Signed-off-by: default avatarBrian Norris <computersforpeace@gmail.com>
Cc: Akinobu Mita <akinobu.mita@gmail.com>
parent 7f2a7ce1
...@@ -70,7 +70,7 @@ static int write_eraseblock(int ebnum) ...@@ -70,7 +70,7 @@ static int write_eraseblock(int ebnum)
int i; int i;
struct mtd_oob_ops ops; struct mtd_oob_ops ops;
int err = 0; 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); prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) { for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
......
...@@ -185,7 +185,7 @@ static long calc_speed(void) ...@@ -185,7 +185,7 @@ static long calc_speed(void)
(finish.tv_usec - start.tv_usec) / 1000; (finish.tv_usec - start.tv_usec) / 1000;
if (ms == 0) if (ms == 0)
return 0; return 0;
k = goodebcnt * (mtd->erasesize / 1024) * 1000; k = (uint64_t)goodebcnt * (mtd->erasesize / 1024) * 1000;
do_div(k, ms); do_div(k, ms);
return k; return k;
} }
......
...@@ -96,7 +96,7 @@ static int do_read(void) ...@@ -96,7 +96,7 @@ static int do_read(void)
if (offs + len > mtd->erasesize) if (offs + len > mtd->erasesize)
len = mtd->erasesize - offs; len = mtd->erasesize - offs;
} }
addr = eb * mtd->erasesize + offs; addr = (loff_t)eb * mtd->erasesize + offs;
return mtdtest_read(mtd, addr, len, readbuf); return mtdtest_read(mtd, addr, len, readbuf);
} }
...@@ -124,7 +124,7 @@ static int do_write(void) ...@@ -124,7 +124,7 @@ static int do_write(void)
offsets[eb + 1] = 0; offsets[eb + 1] = 0;
} }
} }
addr = eb * mtd->erasesize + offs; addr = (loff_t)eb * mtd->erasesize + offs;
err = mtdtest_write(mtd, addr, len, writebuf); err = mtdtest_write(mtd, addr, len, writebuf);
if (unlikely(err)) if (unlikely(err))
return err; return err;
......
...@@ -95,7 +95,7 @@ static int write_eraseblock2(int ebnum) ...@@ -95,7 +95,7 @@ static int write_eraseblock2(int ebnum)
loff_t addr = (loff_t)ebnum * mtd->erasesize; loff_t addr = (loff_t)ebnum * mtd->erasesize;
for (k = 1; k < 33; ++k) { 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; break;
prandom_bytes_state(&rnd_state, writebuf, subpgsize * k); prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf); err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf);
...@@ -195,7 +195,7 @@ static int verify_eraseblock2(int ebnum) ...@@ -195,7 +195,7 @@ static int verify_eraseblock2(int ebnum)
loff_t addr = (loff_t)ebnum * mtd->erasesize; loff_t addr = (loff_t)ebnum * mtd->erasesize;
for (k = 1; k < 33; ++k) { 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; break;
prandom_bytes_state(&rnd_state, writebuf, subpgsize * k); prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
clear_data(readbuf, subpgsize * k); clear_data(readbuf, subpgsize * k);
......
...@@ -101,11 +101,11 @@ static inline int check_eraseblock(int ebnum, unsigned char *buf) ...@@ -101,11 +101,11 @@ static inline int check_eraseblock(int ebnum, unsigned char *buf)
{ {
int err, retries = 0; int err, retries = 0;
size_t read; size_t read;
loff_t addr = ebnum * mtd->erasesize; loff_t addr = (loff_t)ebnum * mtd->erasesize;
size_t len = mtd->erasesize; size_t len = mtd->erasesize;
if (pgcnt) { if (pgcnt) {
addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize; addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
len = pgcnt * pgsize; len = pgcnt * pgsize;
} }
...@@ -155,11 +155,11 @@ static inline int write_pattern(int ebnum, void *buf) ...@@ -155,11 +155,11 @@ static inline int write_pattern(int ebnum, void *buf)
{ {
int err; int err;
size_t written; size_t written;
loff_t addr = ebnum * mtd->erasesize; loff_t addr = (loff_t)ebnum * mtd->erasesize;
size_t len = mtd->erasesize; size_t len = mtd->erasesize;
if (pgcnt) { if (pgcnt) {
addr = (ebnum + 1) * mtd->erasesize - pgcnt * pgsize; addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
len = pgcnt * pgsize; len = pgcnt * pgsize;
} }
err = mtd_write(mtd, addr, len, &written, buf); err = mtd_write(mtd, addr, len, &written, buf);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment