Skip to content

Commit

Permalink
Merge pull request #2117 from smartdevicelink/bugfix/issue-2112-sdl-i…
Browse files Browse the repository at this point in the history
…apdatasession-app-crash

Fixed SDL App Crash - index out of range in replaceBytesInRange
  • Loading branch information
jshivabeharry committed Jun 12, 2023
2 parents 4df33af + e5080bf commit 0e93e7f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions SmartDeviceLink/private/SDLIAPDataSession.m
Expand Up @@ -58,10 +58,24 @@ - (void)writeDataToSessionStream {
if (bytesWritten >= 0) {
if (bytesWritten == bytesRemaining) {
[self.sendDataQueue popBuffer];
} else if (bytesRemaining > bytesWritten) {
} else if (bytesRemaining > bytesWritten && remainder.length > bytesWritten) {
// Cleave the sent bytes from the data, the remainder will sit at the head of the queue
SDLLogV(@"SDLIAPDataSession writeDataToSessionStream bytes written %ld", (long)bytesWritten);
[remainder replaceBytesInRange:NSMakeRange(0, (NSUInteger)bytesWritten) withBytes:NULL length:0];
@try {
SDLLogV(@"SDLIAPDataSession writeDataToSessionStream bytes written %ld", (long)bytesWritten);
NSData *rem = [remainder subdataWithRange:NSMakeRange((NSUInteger)bytesWritten, remainder.length - (NSUInteger)bytesWritten)];
[remainder setLength:[rem length]];
[remainder setData:rem];
} @catch (NSException __unused *exception) {
SDLLogE(@"Unable to remove sent bytes using the subDataWithRange method. Will try with replaceBytesInRange:");
@try {
SDLLogV(@"SDLIAPDataSession writeDataToSessionStream bytes written %ld", (long)bytesWritten);
[remainder replaceBytesInRange:NSMakeRange(0, (NSUInteger)bytesWritten) withBytes:NULL length:0];
} @catch (NSException *exception) {
// Error processing current data. Remove corrupted buffer
SDLLogE(@"Unable to remove sent bytes. Bytes remaining is less than bytes written %lu < %lu. Clearing buffer", bytesRemaining, bytesWritten);
[self.sendDataQueue popBuffer];
}
}
} else {
// Error processing current data. Remove corrupted buffer
SDLLogE(@"Unable to remove sent bytes. Bytes remaining is less than bytes written %lu < %lu. Clearing buffer", bytesRemaining, bytesWritten);
Expand Down

0 comments on commit 0e93e7f

Please sign in to comment.