]> dgit.raspbian.org Git - nextcloud-desktop.git/commitdiff
Operate directly on NSMutableData for incoming socket data, do not work with raw...
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 24 Jul 2023 10:02:15 +0000 (18:02 +0800)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 24 Jul 2023 10:12:29 +0000 (12:12 +0200)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/NCDesktopClientSocketKit/LocalSocketClient.m

index b2408cb62516b93d3fd0a352a1ada65348244108..c245ecf31faa7a896dbfee297eb0359df0c01d4c 100644 (file)
 {
     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) {
                                                            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);