Skip to content

Commit

Permalink
Bug fix for issue CocoaLumberjack#38 - DDTTYLogger can cause a crash …
Browse files Browse the repository at this point in the history
…if the log message is too big for the stack
  • Loading branch information
robbiehanson committed May 31, 2012
1 parent 418c5b1 commit eed0e0b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Lumberjack/DDTTYLogger.m
Expand Up @@ -1088,10 +1088,15 @@ - (void)logMessage:(DDLogMessage *)logMessage
}

// Convert log message to C string.
// The technique below is faster than using the UTF8String method.
//
// We use the stack instead of the heap for speed if possible.
// But we're extra cautious to avoid a stack overflow.

NSUInteger msgLen = [logMsg lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char msg[msgLen + 1];
const BOOL useStack = msgLen < (1024 * 4);

char msgStack[useStack ? (msgLen + 1) : 0];
char *msg = useStack ? msgStack : (char *)malloc(msgLen + 1);

[logMsg getCString:msg maxLength:(msgLen + 1) encoding:NSUTF8StringEncoding];

Expand Down Expand Up @@ -1219,6 +1224,10 @@ - (void)logMessage:(DDLogMessage *)logMessage

writev(STDERR_FILENO, v, 12);
}

if (!useStack) {
free(msg);
}
}
}

Expand Down

0 comments on commit eed0e0b

Please sign in to comment.