Browse Source

fix memory end/dec

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 1 year ago
parent
commit
6b03cc0770

+ 0 - 1
Nextcloud.xcodeproj/xcshareddata/xcschemes/Nextcloud.xcscheme

@@ -96,7 +96,6 @@
       buildConfiguration = "Debug"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
-      enableThreadSanitizer = "YES"
       launchStyle = "0"
       useCustomWorkingDirectory = "NO"
       ignoresPersistentStateOnLaunch = "NO"

+ 32 - 30
iOSClient/Networking/E2EE/NCEndToEndEncryption.m

@@ -861,27 +861,28 @@
 
     while ([inStream hasBytesAvailable]) {
 
-        NSInteger bytesRead = [inStream read:buffer maxLength:streamBuffer];
+        @autoreleasepool {
+            NSInteger bytesRead = [inStream read:buffer maxLength:streamBuffer];
+            if (bytesRead > 0) {
 
-        if (bytesRead > 0) {
-
-            cCipher = [[NSMutableData dataWithLength:bytesRead] mutableBytes];
-            status = EVP_EncryptUpdate(ctx, cCipher, &cCipherLen, [[NSData dataWithBytes:buffer length:bytesRead] bytes], (int)bytesRead);
-            if (status <= 0) {
-                [inStream close];
-                [outStream close];
-                EVP_CIPHER_CTX_free(ctx);
-                return NO;
-            }
-
-            if ([outStream hasSpaceAvailable]) {
-                totalNumberOfBytesWritten = [outStream write:cCipher maxLength:cCipherLen];
-                if (totalNumberOfBytesWritten != cCipherLen) {
+                cCipher = [[NSMutableData dataWithLength:bytesRead] mutableBytes];
+                status = EVP_EncryptUpdate(ctx, cCipher, &cCipherLen, [[NSData dataWithBytes:buffer length:bytesRead] bytes], (int)bytesRead);
+                if (status <= 0) {
                     [inStream close];
                     [outStream close];
                     EVP_CIPHER_CTX_free(ctx);
                     return NO;
                 }
+
+                if ([outStream hasSpaceAvailable]) {
+                    totalNumberOfBytesWritten = [outStream write:cCipher maxLength:cCipherLen];
+                    if (totalNumberOfBytesWritten != cCipherLen) {
+                        [inStream close];
+                        [outStream close];
+                        EVP_CIPHER_CTX_free(ctx);
+                        return NO;
+                    }
+                }
             }
         }
     }
@@ -1068,27 +1069,28 @@
 
     while ([inStream hasBytesAvailable]) {
 
-        NSInteger bytesRead = [inStream read:buffer maxLength:streamBuffer];
+        @autoreleasepool {
+            NSInteger bytesRead = [inStream read:buffer maxLength:streamBuffer];
+            if (bytesRead > 0) {
 
-        if (bytesRead > 0) {
-
-            cPlain = [[NSMutableData dataWithLength:bytesRead] mutableBytes];
-            status = EVP_DecryptUpdate(ctx, cPlain, &cPlainLen, [[NSData dataWithBytes:buffer length:bytesRead] bytes], (int)bytesRead);
-            if (status <= 0) {
-                [inStream close];
-                [outStream close];
-                EVP_CIPHER_CTX_free(ctx);
-                return NO;
-            }
-
-            if ([outStream hasSpaceAvailable]) {
-                totalNumberOfBytesWritten = [outStream write:cPlain maxLength:cPlainLen];
-                if (totalNumberOfBytesWritten != cPlainLen) {
+                cPlain = [[NSMutableData dataWithLength:bytesRead] mutableBytes];
+                status = EVP_DecryptUpdate(ctx, cPlain, &cPlainLen, [[NSData dataWithBytes:buffer length:bytesRead] bytes], (int)bytesRead);
+                if (status <= 0) {
                     [inStream close];
                     [outStream close];
                     EVP_CIPHER_CTX_free(ctx);
                     return NO;
                 }
+
+                if ([outStream hasSpaceAvailable]) {
+                    totalNumberOfBytesWritten = [outStream write:cPlain maxLength:cPlainLen];
+                    if (totalNumberOfBytesWritten != cPlainLen) {
+                        [inStream close];
+                        [outStream close];
+                        EVP_CIPHER_CTX_free(ctx);
+                        return NO;
+                    }
+                }
             }
         }
     }