adium 2980:119179e69464: Fix a few more issues with context read...

commits at adium.im commits at adium.im
Thu Nov 26 16:09:19 UTC 2009


details:	http://hg.adium.im/adium/rev/119179e69464
revision:	2980:119179e69464
author:		Zachary West <zacw at adium.im>
date:		Thu Nov 26 10:33:53 2009 -0500

Fix a few more issues with context reading; if we're reading less than readSize, we need to decrement readSize too. Refs #13362.
(transplanted from 51687f71fc6384eeed8507fd8d287e184071d2e2)

diffs (74 lines):

diff -r 079ffd9b2a95 -r 119179e69464 Source/DCMessageContextDisplayPlugin.m
--- a/Source/DCMessageContextDisplayPlugin.m	Thu Nov 26 01:15:12 2009 -0500
+++ b/Source/DCMessageContextDisplayPlugin.m	Thu Nov 26 10:33:53 2009 -0500
@@ -230,7 +230,6 @@
 		NSInteger readSize = 4 * getpagesize(); //Read 4 pages at a time.
 		NSMutableData *chunk = [NSMutableData dataWithLength:readSize];
 		NSInteger fd = [file fileDescriptor];
-		char *buf = [chunk mutableBytes];
 		off_t offset = [file offsetInFile];
 		enum LMXParseResult result = LMXParsedIncomplete;
 
@@ -238,17 +237,39 @@
 		NSAutoreleasePool *parsingAutoreleasePool = [[NSAutoreleasePool alloc] init];
 		
 		do {
-			//Calculate the new offset. This also leaves offset where it needs to be
-			//for the next iteration, since we modify it for the round every time.
-			offset = (offset <= readSize) ? 0 : offset - readSize;
+			// The location we're going to read for *this* set of reads.
+			off_t readOffset = offset - readSize;
+
+			if (readOffset < 0) {
+				// Decrease it by the amount we're over.
+				readSize += readOffset;
+				// Start from the beginning.
+				readOffset = 0;
+			}
 			
+			if (chunk.length != readSize) {
+				// In case we short-read last time, or we're reading a smaller amount this time.
+				[chunk setLength:readSize];				
+			}
+			
+			char *buf = [chunk mutableBytes];
+
 			//Seek to it and read greedily until we hit readSize or run out of file.
 			NSInteger idx = 0;
-			for (ssize_t amountRead = 0; idx < readSize; idx += amountRead) { 
-				amountRead = pread(fd, buf + idx, readSize, offset + idx); 
+			ssize_t amountRead = 0;
+			for (amountRead = 0; idx < readSize; idx += amountRead) { 
+				amountRead = pread(fd, buf + idx, readSize, readOffset + idx); 
 			   if (amountRead <= 0) break;
 			}
 			
+			if (idx != readSize) {
+				// If we short read, we don't want to read unknown buffer contents.
+				[chunk setLength:idx];
+			}
+			
+			// Adjust the real offset
+			offset -= idx;
+
 			//Parse
 			result = [parser parseChunk:chunk];
 			
@@ -265,7 +286,7 @@
 		[outerFoundContentContexts replaceObjectsInRange:NSMakeRange(0, 0) withObjectsFromArray:foundMessages];
 		linesLeftToFind -= [outerFoundContentContexts count];
 	}
-		
+	
 	if (linesLeftToFind > 0) {
 		AILogWithSignature(@"Unable to find %d logs for %@", linesLeftToFind, chat);
 	}
@@ -358,7 +379,7 @@
 				//Don't log this object
 				[message setPostProcessContent:NO];
 				[message setTrackContent:NO];
-
+				
 				//Add it to the array (in front, since we're working backwards, and we want the array in forward order)
 				[foundMessages insertObject:message atIndex:0];
 			} else {




More information about the commits mailing list