From 60db4c4e330cf311eed8f5aecca514f62fde4892 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 17 Jun 2008 15:56:54 +0200 Subject: [PATCH] Make the revlist parsing more robust This uses an NSMutableArray to increase the currentLine if the buffer is full. This prevents very long messages from breaking the parsing. If we cannot parse the log line, simple ignore it. --- PBGitRepository.m | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/PBGitRepository.m b/PBGitRepository.m index e17adcd..593cdfc 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -80,17 +80,32 @@ static NSString* gitPath = @"/usr/bin/env"; int fd = [handle fileDescriptor]; FILE* f = fdopen(fd, "r"); - char buffer[2050]; - buffer[2049] = 0; + int BUFFERSIZE = 2048; + char buffer[BUFFERSIZE]; + buffer[BUFFERSIZE - 2] = 0; char* l; int num = 0; - while (l = fgets(buffer, 2048, f)) { - NSString* currentLine = [NSString stringWithCString:(const char *)l encoding:NSUTF8StringEncoding]; - if ([currentLine length] == 0) - currentLine = [NSString stringWithCString:(const char *)l encoding:NSASCIIStringEncoding]; + NSMutableString* currentLine = [NSMutableString string]; + while (l = fgets(buffer, BUFFERSIZE, f)) { + NSString *s = [NSString stringWithCString:(const char *)l encoding:NSUTF8StringEncoding]; + if ([s length] == 0) + s = [NSString stringWithCString:(const char *)l encoding:NSASCIIStringEncoding]; + [currentLine appendString: s]; + + // If buffer is full, we go for another round + if (buffer[BUFFERSIZE - 2] != 0) { + //NSLog(@"Line too long!"); + buffer[BUFFERSIZE - 2] = 0; + continue; + } + + // If we are here, we currentLine is a full line. NSArray* components = [currentLine componentsSeparatedByString:@"\01"]; - + if ([components count] < 4) { + NSLog(@"Can't split string: %@", currentLine); + continue; + } PBGitCommit* newCommit = [[PBGitCommit alloc] initWithRepository: self andSha: [components objectAtIndex:0]]; newCommit.subject = [components objectAtIndex:2]; newCommit.author = [components objectAtIndex:1]; @@ -100,6 +115,7 @@ static NSString* gitPath = @"/usr/bin/env"; num++; if (num % 10000 == 0) [self performSelectorOnMainThread:@selector(setCommits:) withObject:newArray waitUntilDone:NO]; + currentLine = [NSMutableString string]; } [self performSelectorOnMainThread:@selector(setCommits:) withObject:newArray waitUntilDone:YES];