Patchwork [07/10] mtd-utils: nandwrite: avoid NULL buffer pointers

login
register
mail settings
Submitter Brian Norris
Date Nov. 3, 2010, 8:27 a.m.
Message ID <1288772847-8120-7-git-send-email-computersforpeace@gmail.com>
Download mbox | patch
Permalink /patch/69957/
State Accepted
Commit a3d86d1d7679d082ba2b28b4ad2c57a4dd362df3
Headers show

Comments

Brian Norris - Nov. 3, 2010, 8:27 a.m.
Commit 07005d915d6a79dbdee14b0c4360df5058c3a98b made changes to the
buffer allocation in nandwrite and did not handle all affected code
areas properly. In particular, we were assigning:
	oob.ptr = noecc ? oobreadbuf : oobbuf;
However, since oobreadbuf and oobbuf are declared dynamically, they
are NULL at this point. If they aren't properly assigned later, we
unwittingly are passing a NULL pointer as oob buffer.

This assignment line is best moved after the buffer allocations and
pointer assignment.

Effects of this problem can be seen when writing oob data with the "-o"
flag and without the "-n" flag:
	$ ./nandwrite -o /dev/mtd0 img.bin
	Writing data to block 0 at offset 0x0
	ioctl(MEMWRITEOOB): Bad address
	Data was only partially written due to error
	: Bad address

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
---
 nandwrite.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)

Patch

diff --git a/nandwrite.c b/nandwrite.c
index b362c29..8ec5afe 100644
--- a/nandwrite.c
+++ b/nandwrite.c
@@ -391,7 +391,6 @@  int main(int argc, char * const argv[])
 	}
 
 	oob.length = mtd.oob_size;
-	oob.ptr = noecc ? oobreadbuf : oobbuf;
 
 	/* Determine if we are reading from standard input or from a file. */
 	if (strcmp(img, standard_input) == 0) {
@@ -594,9 +593,7 @@  int main(int argc, char * const argv[])
 				}
 			}
 
-			if (noecc) {
-				oob.ptr = oobreadbuf;
-			} else {
+			if (!noecc) {
 				int i, start, len;
 				int tags_pos = 0;
 				/*
@@ -630,6 +627,7 @@  int main(int argc, char * const argv[])
 			}
 			/* Write OOB data first, as ecc will be placed in there */
 			oob.start = mtdoffset;
+			oob.ptr = noecc ? oobreadbuf : oobbuf;
 			if (ioctl(fd, MEMWRITEOOB, &oob) != 0) {
 				perror("ioctl(MEMWRITEOOB)");
 				goto closeall;