From 2acec8f4a04c2c054de4fdb56790e52db9c6a383 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Wed, 19 Jul 2023 22:03:16 +0800 Subject: [PATCH] Clean up FinderSyncExt processInBuffer Signed-off-by: Claudio Cambra --- .../LocalSocketClient.m | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m b/shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m index 7e62fc0f3..55b31b83b 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m +++ b/shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m @@ -326,21 +326,29 @@ - (void)processInBuffer { - NSLog(@"Processing in buffer. In buffer length %li", [_inBuffer length]); + const NSUInteger inBufferLength = _inBuffer.length; + NSLog(@"Processing in buffer. In buffer length %li", inBufferLength); UInt8 separator[] = {0xa}; // Byte value for "\n" + NSData * const separatorData = [NSData dataWithBytes:separator length:1]; + const NSRange inBufferLengthRange = NSMakeRange(0, inBufferLength); + while(true) { - NSRange firstSeparatorIndex = [_inBuffer rangeOfData:[NSData dataWithBytes:separator length:1] options:0 range:NSMakeRange(0, [_inBuffer length])]; - + const NSRange firstSeparatorIndex = [_inBuffer rangeOfData:separatorData + options:0 + range:inBufferLengthRange]; + if(firstSeparatorIndex.location == NSNotFound) { NSLog(@"No separator found. Stopping."); return; // No separator, nope out } else { - unsigned char *buffer = [_inBuffer mutableBytes]; + unsigned char * const buffer = [_inBuffer mutableBytes]; buffer[firstSeparatorIndex.location] = 0; // Add NULL terminator, so we can use C string methods - - NSString *newLine = [NSString stringWithUTF8String:[_inBuffer bytes]]; + const NSUInteger nullTerminatorIndex = firstSeparatorIndex.location + 1; + const NSRange nullTerminatorRange = NSMakeRange(0, nullTerminatorIndex); + + NSString * const newLine = [NSString stringWithUTF8String:[_inBuffer bytes]]; - [_inBuffer replaceBytesInRange:NSMakeRange(0, firstSeparatorIndex.location + 1) withBytes:NULL length:0]; + [_inBuffer replaceBytesInRange:nullTerminatorRange withBytes:NULL length:0]; [_lineProcessor process:newLine]; } } -- 2.39.5