mtdutils: fix crash on large erase block

allocate heap instead of stack for verify[]. otherwise it crashes on large erase block size e.g. 8MiB.

Change-Id: Ifaeb1e381af2b4bbdbeb7337848818299d7f441b
This commit is contained in:
FUKAUMI Naoki 2012-01-17 20:00:18 +09:00 committed by Koushik Dutta
parent c9d07dc238
commit 35ced47771

View File

@ -406,6 +406,11 @@ static int write_block(MtdWriteContext *ctx, const char *data)
if (pos == (off_t) -1) return 1;
ssize_t size = partition->erase_size;
char *verify = malloc(size);
if (verify == NULL)
return 1;
while (pos + size <= (int) partition->size) {
loff_t bpos = pos;
int ret = ioctl(fd, MEMGETBADBLOCK, &bpos);
@ -434,7 +439,6 @@ static int write_block(MtdWriteContext *ctx, const char *data)
pos, strerror(errno));
}
char verify[size];
if (lseek(fd, pos, SEEK_SET) != pos ||
read(fd, verify, size) != size) {
fprintf(stderr, "mtd: re-read error at 0x%08lx (%s)\n",
@ -451,6 +455,7 @@ static int write_block(MtdWriteContext *ctx, const char *data)
fprintf(stderr, "mtd: wrote block after %d retries\n", retry);
}
fprintf(stderr, "mtd: successfully wrote block at %llx\n", pos);
free(verify);
return 0; // Success!
}
@ -461,6 +466,8 @@ static int write_block(MtdWriteContext *ctx, const char *data)
pos += partition->erase_size;
}
free(verify);
// Ran out of space on the device
errno = ENOSPC;
return -1;