From b35e3d674ea3352e53011fe7faec4f7d1587fef9 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Mon, 24 Jul 2023 18:02:15 +0800 Subject: [PATCH] Operate directly on NSMutableData for incoming socket data, do not work with raw buffers Signed-off-by: Claudio Cambra --- .../NCDesktopClientSocketKit/LocalSocketClient.m | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m b/shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m index b2408cb62516b..c245ecf31faa7 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m +++ b/shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m @@ -328,7 +328,8 @@ - (void)processInBuffer { NSLog(@"Processing in buffer. In buffer length %li", _inBuffer.length); - UInt8 separator[] = {0xa}; // Byte value for "\n" + static const UInt8 separator[] = {0xa}; // Byte value for "\n" + static const char terminator[] = {0}; NSData * const separatorData = [NSData dataWithBytes:separator length:1]; while(_inBuffer.length > 0) { @@ -338,24 +339,21 @@ - (void)processInBuffer options:0 range:inBufferLengthRange]; - unsigned char *buffer = _inBuffer.mutableBytes; NSUInteger nullTerminatorIndex = NSUIntegerMax; + // Add NULL terminator, so we can use C string methods if (firstSeparatorIndex.location == NSNotFound) { NSLog(@"No separator found. Creating new buffer qith space for null terminator."); - unsigned char *newBuffer = malloc(sizeof(unsigned char) * (inBufferLength + 1)); - memcpy(newBuffer, buffer, inBufferLength); - buffer = newBuffer; + [_inBuffer appendBytes:terminator length:1]; nullTerminatorIndex = inBufferLength; } else { nullTerminatorIndex = firstSeparatorIndex.location; + [_inBuffer replaceBytesInRange:NSMakeRange(nullTerminatorIndex, 1) withBytes:terminator]; } NSAssert(nullTerminatorIndex != NSUIntegerMax, @"Null terminator index should be valid."); - buffer[nullTerminatorIndex] = 0; // Add NULL terminator, so we can use C string methods - NSString * const newLine = [NSString stringWithUTF8String:_inBuffer.bytes]; const NSRange nullTerminatorRange = NSMakeRange(0, nullTerminatorIndex + 1);