changeset 249:2e6d0dd55c20

Fix chunk start problem properly
author charles
date Fri, 09 Jan 2009 02:52:28 +0000
parents a8738fee77c1
children 131343232124
files yaffs_guts.c
diffstat 1 files changed, 12 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/yaffs_guts.c
+++ b/yaffs_guts.c
@@ -4847,7 +4847,8 @@ int yaffs_WriteDataToFile(yaffs_Object *
         int nToWriteBack;
         int startOfWrite = offset;
         int chunkWritten = 0;
-        int nBytesRead;
+        __u32 nBytesRead;
+        __u32 chunkStart;
 
 	yaffs_Device *dev;
 
@@ -4877,9 +4878,12 @@ int yaffs_WriteDataToFile(yaffs_Object *
 			 * we need to write back as much as was there before.
 			 */
 
-			nBytesRead =
-			    in->variant.fileVariant.fileSize -
-			    ((chunk - 1) * dev->nDataBytesPerChunk);
+			chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
+
+			if(chunkStart > in->variant.fileVariant.fileSize)
+				nBytesRead = 0; /* Past end of file */
+			else
+				nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
 
 			if (nBytesRead > dev->nDataBytesPerChunk) {
 				nBytesRead = dev->nDataBytesPerChunk;
@@ -4887,7 +4891,10 @@ int yaffs_WriteDataToFile(yaffs_Object *
 
 			nToWriteBack =
 			    (nBytesRead >
-			     ((int)start + n)) ? nBytesRead : (start + n);
+			     (start + n)) ? nBytesRead : (start + n);
+			
+			if(nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
+				YBUG();
 
 		} else {
 			nToCopy = dev->nDataBytesPerChunk - start;