diff --git a/BPSampleApp/BPSampleAppHangingTests/BPSampleAppHangingTests.m b/BPSampleApp/BPSampleAppHangingTests/BPSampleAppHangingTests.m index bf967460..844146e0 100644 --- a/BPSampleApp/BPSampleAppHangingTests/BPSampleAppHangingTests.m +++ b/BPSampleApp/BPSampleAppHangingTests/BPSampleAppHangingTests.m @@ -26,13 +26,8 @@ - (void)tearDown { [super tearDown]; } -- (void)testAppHangingWithOutput { - while(TRUE){printf("\n");} -} - - (void)testAppHanging { while(TRUE){}; } - @end diff --git a/Bluepill-cli/Bluepill-cli/Bluepill/Reporters/BPTreeParser.m b/Bluepill-cli/Bluepill-cli/Bluepill/Reporters/BPTreeParser.m index cc32c672..9970408c 100644 --- a/Bluepill-cli/Bluepill-cli/Bluepill/Reporters/BPTreeParser.m +++ b/Bluepill-cli/Bluepill-cli/Bluepill/Reporters/BPTreeParser.m @@ -93,11 +93,29 @@ - (void)writeHeader { - (void)handleChunkData:(nonnull NSData *)chunk { if ([chunk length] > 0) { NSString *str = [[NSString alloc] initWithData:chunk encoding:NSUTF8StringEncoding]; + if (!str) { + [BPUtils printInfo:WARNING withString:@"Failed to UTF8 decode chunk: %@", chunk]; + str = [[NSString alloc] initWithData:chunk encoding:NSASCIIStringEncoding]; + [BPUtils printInfo:WARNING withString:@"ASCII: %@", str]; + } + if (!str) { + [BPUtils printInfo:ERROR withString:@"Failed to ASCII decode chunk: %@", chunk]; + exit(1); + } NSRange range = [str rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]]; + unsigned long numLines = 0; + unsigned long maxLines = [chunk length]; while (range.location != NSNotFound) { self.line = [self.line stringByAppendingString:[str substringToIndex:range.location] ?: @""]; [self.log writeLine:@"%@", self.line]; [self parseLine:self.line]; + if (numLines++ > maxLines) { + [BPUtils printInfo:ERROR withString:@"Infinite Loop Averted!: range {%d, %d}, %d, %@", + range.length, range.location, [chunk length], str]; + [BPUtils printInfo:ERROR withString:@"Data: %@", chunk]; + // Fail hard. + exit(1); + } self.line = @""; str = [str substringFromIndex:range.location+range.length]; range = [str rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]]; @@ -108,15 +126,11 @@ - (void)handleChunkData:(nonnull NSData *)chunk { - (void)parseLine:(nullable NSString *)line { [BPUtils printInfo:DEBUGINFO withString:@"[OUTPUT] %@", line]; - + [self onOutputReceived:line]; if (!line || ![line length]) { return; } - // We've seen hangs where the app just prints endless \n's so we - // don't count \n's as output. - [self onOutputReceived:line]; - NSRange lineRange = NSMakeRange(0, [line length]); BOOL logLine = YES; NSRegularExpression *regex;