Fix more null termination bugs

This commit is contained in:
achmizs 2015-12-22 18:33:01 -05:00
parent 558399ad6c
commit 710aa0a465
3 changed files with 16 additions and 8 deletions

View File

@ -150,6 +150,7 @@ static void onNumericEvent(irc_session_t *session, unsigned int event, const cha
_version = [NSString stringWithFormat:@"IRCClient Framework v%s (Said Achmiz) - libirc v%d.%d (George Yunaev)", IRCCLIENTVERSION, high, low]; _version = [NSString stringWithFormat:@"IRCClient Framework v%s (Said Achmiz) - libirc v%d.%d (George Yunaev)", IRCCLIENTVERSION, high, low];
_channels = [[NSMutableDictionary alloc] init]; _channels = [[NSMutableDictionary alloc] init];
_encoding = NSUTF8StringEncoding;
} }
return self; return self;
} }
@ -585,7 +586,7 @@ static void onJoinChannel(irc_session_t *session, const char *event, const char
{ {
IRCClientSession* clientSession = (__bridge IRCClientSession *) irc_get_ctx(session); IRCClientSession* clientSession = (__bridge IRCClientSession *) irc_get_ctx(session);
NSString *nick = @(origin); NSString *nick = @(origin);
NSData *channelName = [NSData dataWithBytes:params[0] length:strlen(params[0])]; NSData *channelName = [NSData dataWithBytes:params[0] length:strlen(params[0]) + 1];
[clientSession userJoined:nick channel:channelName]; [clientSession userJoined:nick channel:channelName];
} }
@ -604,12 +605,12 @@ static void onPartChannel(irc_session_t *session, const char *event, const char
{ {
IRCClientSession *clientSession = (__bridge IRCClientSession *) irc_get_ctx(session); IRCClientSession *clientSession = (__bridge IRCClientSession *) irc_get_ctx(session);
NSString *nick = @(origin); NSString *nick = @(origin);
NSData *channelName = [NSData dataWithBytes:params[0] length:strlen(params[0])]; NSData *channelName = [NSData dataWithBytes:params[0] length:strlen(params[0]) + 1];
NSData *reason = nil; NSData *reason = nil;
if (count > 1) if (count > 1)
{ {
reason = [NSData dataWithBytes:params[1] length:strlen(params[1])]; reason = [NSData dataWithBytes:params[1] length:strlen(params[1]) + 1];
} }
[clientSession userParted:nick channel:channelName withReason:reason]; [clientSession userParted:nick channel:channelName withReason:reason];
@ -632,7 +633,7 @@ static void onMode(irc_session_t *session, const char *event, const char *origin
{ {
IRCClientSession *clientSession = (__bridge IRCClientSession *) irc_get_ctx(session); IRCClientSession *clientSession = (__bridge IRCClientSession *) irc_get_ctx(session);
NSString *nick = @(origin); NSString *nick = @(origin);
NSData *channelName = [NSData dataWithBytes:params[0] length:strlen(params[0])]; NSData *channelName = [NSData dataWithBytes:params[0] length:strlen(params[0]) + 1];
NSString *mode = @(params[1]); NSString *mode = @(params[1]);
NSString *modeParams = nil; NSString *modeParams = nil;
@ -674,7 +675,7 @@ static void onTopic(irc_session_t *session, const char *event, const char *origi
{ {
IRCClientSession *clientSession = (__bridge IRCClientSession *) irc_get_ctx(session); IRCClientSession *clientSession = (__bridge IRCClientSession *) irc_get_ctx(session);
NSString *nick = @(origin); NSString *nick = @(origin);
NSData *channelName = [NSData dataWithBytes:params[0] length:strlen(params[0])]; NSData *channelName = [NSData dataWithBytes:params[0] length:strlen(params[0]) + 1];
NSData *topic = nil; NSData *topic = nil;
if (count > 1) if (count > 1)

View File

@ -1,5 +1,5 @@
// //
// NSData+SA_NSDataExtensions.h // NSData+SA_NSDataExtensions.m
// //
// Copyright (c) 2015 Said Achmiz. // Copyright (c) 2015 Said Achmiz.
// //
@ -51,7 +51,7 @@
[self getBytes:terminated_string_buffer length:self.length]; [self getBytes:terminated_string_buffer length:self.length];
terminated_string_buffer[self.length] = '\0'; terminated_string_buffer[self.length] = '\0';
return [NSData dataWithBytesNoCopy:terminated_string_buffer length:self.length + 1 freeWhenDone:YES]; return [NSData dataWithBytesNoCopy:terminated_string_buffer length:(self.length + 1) freeWhenDone:YES];
} }
} }
@ -71,7 +71,7 @@
char* unterminated_string_buffer = malloc(self.length - 1); char* unterminated_string_buffer = malloc(self.length - 1);
[self getBytes:unterminated_string_buffer length:self.length - 1]; [self getBytes:unterminated_string_buffer length:self.length - 1];
return [NSData dataWithBytesNoCopy:unterminated_string_buffer length:self.length - 1 freeWhenDone:YES]; return [NSData dataWithBytesNoCopy:unterminated_string_buffer length:(self.length - 1) freeWhenDone:YES];
} }
} }

View File

@ -0,0 +1,7 @@
NSData+SA_NSDataExtensions
Adds utility functions to NSData, that help deal with null termination of C strings.
This category on NSData adds properties that allow you to get the null-terminated or non-null-terminated versions of byte arrays stored as NSData objects, and to easily check whether an NSData's byte array is, or is not, null-terminated (that is, whether its last byte is a null).
Copyright (c) 2015 Said Achmiz.