Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Preventing crash when the token is NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
candotti authored and Daniele Candotti committed May 14, 2016
1 parent d465624 commit d6cf084
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
1 change: 1 addition & 0 deletions lelib/lecore.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void le_poke(void);
void le_log(const char* message);
void le_write_string(NSString* string);
void le_set_token(const char* token);
bool is_valid_token(const char* token,size_t *token_length);
void le_set_debug_logs(bool verbose);

#endif
59 changes: 40 additions & 19 deletions lelib/lecore.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ void le_log(const char* message)
{
dispatch_sync(le_write_queue, ^{

size_t token_length = strlen(le_token);
size_t token_length;

if(!is_valid_token(le_token,&token_length))
return ;


size_t max_length = MAXIMUM_LOGENTRY_SIZE - token_length - 2; // minus token length, space separator and lf

size_t length = strlen(message);
Expand All @@ -199,59 +204,75 @@ void le_write_string(NSString* string)
{
dispatch_sync(le_write_queue, ^{

NSUInteger tokenLength = strlen(le_token);
size_t token_length;
if(!is_valid_token(le_token,&token_length))
return ;

NSUInteger maxLength = MAXIMUM_LOGENTRY_SIZE - tokenLength - 2; // minus token length, space separator and \n
NSUInteger maxLength = MAXIMUM_LOGENTRY_SIZE - token_length - 2; // minus token length, space separator and \n
if ([string length] > maxLength) {
LE_DEBUG(@"Too large message, it will be truncated");
}

memcpy(buffer, le_token, tokenLength);
buffer[tokenLength] = ' ';
memcpy(buffer, le_token, token_length);
buffer[token_length] = ' ';

NSRange range = {.location = 0, .length = [string length]};

NSUInteger usedLength = 0;
BOOL r = [string getBytes:(buffer + tokenLength + 1) maxLength:maxLength usedLength:&usedLength encoding:NSUTF8StringEncoding options:NSStringEncodingConversionAllowLossy range:range remainingRange:NULL];
BOOL r = [string getBytes:(buffer + token_length + 1) maxLength:maxLength usedLength:&usedLength encoding:NSUTF8StringEncoding options:NSStringEncodingConversionAllowLossy range:range remainingRange:NULL];

if (!r) {
LE_DEBUG(@"Error converting message characters.");
return;
}

NSUInteger totalLength = tokenLength + 1 + usedLength;
NSUInteger totalLength = token_length + 1 + usedLength;
buffer[totalLength++] = '\n';
write_buffer((size_t)totalLength);
});
}

void le_set_token(const char* token)
{
size_t length = strlen(token);

if (length < TOKEN_LENGTH) {
LE_DEBUG(@"Invalid token length, it will not be used.");
return;
}

if (length >= MAXIMUM_LOGENTRY_SIZE) {
LE_DEBUG(@"Token too large, it will not be used.");
size_t length ;
if(!is_valid_token(token,&length))
return;
}

char* local_buffer = malloc(length + 1);
if (!local_buffer) {
LE_DEBUG(@"Can't allocate token buffer.");
return;
return ;
}

strlcpy(local_buffer, token, length + 1);

dispatch_sync(le_write_queue, ^{
le_token = local_buffer;
});
}

bool is_valid_token(const char * token,size_t* token_length)
{
size_t length = 0;

if (token == NULL) {
NSLog(@"nil token\n");
LE_DEBUG(@"nil token");
return false;
}

length = strlen(token);

if(token_length != NULL)
*token_length = length;

if (length < TOKEN_LENGTH) {
LE_DEBUG(@"Invalid token length, it will not be used.");
return false;
}

return true;
}

void le_set_debug_logs(bool debug) {
le_debug_logs = debug;
}
7 changes: 7 additions & 0 deletions lelibTests/lelibTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,12 @@ - (void)testLog

XCTFail(@"Test exited runloop");
}
- (void)testNilToken{

LELog* log = [LELog sharedInstance];
log.token = nil;
[log log:@"try to log without token"];

}

@end

0 comments on commit d6cf084

Please sign in to comment.