ソースを参照

fix comment and migrate to calloc for session (#14295)

themiswang 1 年間 前
コミット
f53dfc347d

+ 8 - 8
Crashlytics/Crashlytics/Helpers/FIRCLSAllocate.c

@@ -56,7 +56,7 @@ FIRCLSAllocatorRef FIRCLSAllocatorCreate(size_t writableSpace, size_t readableSp
   }
 
   // Make one big, continuous allocation, adding additional pages for our guards.  Note
-  // that we cannot use malloc (or valloc) in this case, because we need to assert full
+  // that we cannot use malloc, calloc (or valloc) in this case, because we need to assert full
   // ownership over these allocations.  mmap is a much better choice.  We also mark these
   // pages as MAP_NOCACHE.
   allocationSize = writableRegion.size + readableRegion.size + pageSize * 3;
@@ -174,10 +174,10 @@ void* FIRCLSAllocatorSafeAllocateFromRegion(FIRCLSAllocationRegion* region, size
 
     // this shouldn't happen unless we make a mistake with our size pre-computations
     if ((uintptr_t)originalCursor - (uintptr_t)region->start + size > region->size) {
-      FIRCLSSDKLog("Unable to allocate sufficient memory, falling back to malloc\n");
+      FIRCLSSDKLog("Unable to allocate sufficient memory, falling back to calloc\n");
       void* ptr = calloc(1, size);
       if (!ptr) {
-        FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocateFromRegion\n");
+        FIRCLSSDKLog("Unable to calloc in FIRCLSAllocatorSafeAllocateFromRegion\n");
         return NULL;
       }
       return ptr;
@@ -195,21 +195,21 @@ void* FIRCLSAllocatorSafeAllocate(FIRCLSAllocatorRef allocator,
   FIRCLSAllocationRegion* region;
 
   if (!allocator) {
-    // fall back to malloc in this case
-    FIRCLSSDKLog("Allocator invalid, falling back to malloc\n");
+    // fall back to calloc in this case
+    FIRCLSSDKLog("Allocator invalid, falling back to calloc\n");
     void* ptr = calloc(1, size);
     if (!ptr) {
-      FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocate\n");
+      FIRCLSSDKLog("Unable to calloc in FIRCLSAllocatorSafeAllocate\n");
       return NULL;
     }
     return ptr;
   }
 
   if (allocator->protectionEnabled) {
-    FIRCLSSDKLog("Allocator already protected, falling back to malloc\n");
+    FIRCLSSDKLog("Allocator already protected, falling back to calloc\n");
     void* ptr = calloc(1, size);
     if (!ptr) {
-      FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocate\n");
+      FIRCLSSDKLog("Unable to calloc in FIRCLSAllocatorSafeAllocate\n");
       return NULL;
     }
     return ptr;

+ 3 - 3
Crashlytics/Crashlytics/Helpers/FIRCLSFile.m

@@ -76,7 +76,7 @@ static bool FIRCLSFileInit(
   if (bufferWrites) {
     file->writeBuffer = calloc(1, FIRCLSWriteBufferLength * sizeof(char));
     if (!file->writeBuffer) {
-      FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileInit");
+      FIRCLSErrorLog(@"Unable to calloc in FIRCLSFileInit");
       return false;
     }
 
@@ -671,7 +671,7 @@ NSString* FIRCLSFileHexEncodeString(const char* string) {
   char* encodedBuffer = calloc(1, length * 2 + 1);
 
   if (!encodedBuffer) {
-    FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileHexEncodeString");
+    FIRCLSErrorLog(@"Unable to calloc in FIRCLSFileHexEncodeString");
     return nil;
   }
 
@@ -695,7 +695,7 @@ NSString* FIRCLSFileHexDecodeString(const char* string) {
   size_t length = strlen(string);
   char* decodedBuffer = calloc(1, length);  // too long, but safe
   if (!decodedBuffer) {
-    FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileHexDecodeString");
+    FIRCLSErrorLog(@"Unable to calloc in FIRCLSFileHexDecodeString");
     return nil;
   }
 

+ 1 - 1
Crashlytics/Crashlytics/Helpers/FIRCLSUtility.m

@@ -188,7 +188,7 @@ NSString* FIRCLSNSDataToNSString(NSData* data) {
   buffer = calloc(1, sizeof(char) * size);
 
   if (!buffer) {
-    FIRCLSErrorLog(@"Unable to malloc in FIRCLSNSDataToNSString");
+    FIRCLSErrorLog(@"Unable to calloc in FIRCLSNSDataToNSString");
     return nil;
   }
 

+ 3 - 3
Crashlytics/Crashlytics/Models/Record/FIRCLSReportAdapter.m

@@ -236,7 +236,7 @@
   }
 }
 
-/** Mallocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
+/** Callocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
  * @note Memory needs to be freed manually, through pb_free or pb_release.
  * @param string The string to encode as pb_bytes.
  */
@@ -251,12 +251,12 @@ pb_bytes_array_t *FIRCLSEncodeString(NSString *string) {
   return FIRCLSEncodeData(stringBytes);
 }
 
-/** Mallocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
+/** Callocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
  * @note Memory needs to be free manually, through pb_free or pb_release.
  * @param data The data to copy into the new bytes array.
  */
 pb_bytes_array_t *FIRCLSEncodeData(NSData *data) {
-  // We have received couple security tickets before for using malloc here.
+  // We have received couple security tickets before for using calloc here.
   // Here is a short explaination on how it is calculated so buffer overflow is prevented:
   // We will alloc an amount of memeory for struct `pb_bytes_array_t`, this struct contains two
   // attributes:

+ 2 - 2
FirebaseSessions/SourcesObjC/NanoPB/FIRSESNanoPBHelpers.h

@@ -49,12 +49,12 @@ NSData* _Nullable FIRSESEncodeProto(const pb_field_t fields[],
                                     NSError** error);
 #pragma clang diagnostic pop
 
-/// Mallocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
+/// Callocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
 /// @note Memory needs to be freed manually, through pb_free or pb_release.
 /// @param data The data to copy into the new bytes array.
 pb_bytes_array_t* _Nullable FIRSESEncodeData(NSData* _Nullable data);
 
-/// Mallocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
+/// Callocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
 /// @note Memory needs to be freed manually, through pb_free or pb_release.
 /// @param string The string to encode as pb_bytes.
 pb_bytes_array_t* _Nullable FIRSESEncodeString(NSString* _Nullable string);

+ 4 - 4
FirebaseSessions/SourcesObjC/NanoPB/FIRSESNanoPBHelpers.m

@@ -87,12 +87,12 @@ NSData *_Nullable FIRSESEncodeProto(const pb_field_t fields[],
 }
 #pragma clang diagnostic pop
 
-/** Mallocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
+/** Callocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
  * @note Memory needs to be free manually, through pb_free or pb_release.
  * @param data The data to copy into the new bytes array.
  */
 pb_bytes_array_t *_Nullable FIRSESEncodeData(NSData *_Nullable data) {
-  pb_bytes_array_t *pbBytes = malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
+  pb_bytes_array_t *pbBytes = calloc(1, PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
   if (pbBytes == NULL) {
     return NULL;
   }
@@ -101,7 +101,7 @@ pb_bytes_array_t *_Nullable FIRSESEncodeData(NSData *_Nullable data) {
   return pbBytes;
 }
 
-/** Mallocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
+/** Callocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
  * @note Memory needs to be freed manually, through pb_free or pb_release.
  * @param string The string to encode as pb_bytes.
  */
@@ -174,7 +174,7 @@ NSString *_Nullable FIRSESGetSysctlEntry(const char *sysctlKey) {
   size_t size;
   sysctlbyname(sysctlKey, NULL, &size, NULL, 0);
   if (size > 0) {
-    char *entryValueCStr = malloc(size);
+    char *entryValueCStr = calloc(1, size);
     sysctlbyname(sysctlKey, entryValueCStr, &size, NULL, 0);
     entryValue = [NSString stringWithCString:entryValueCStr encoding:NSUTF8StringEncoding];
     free(entryValueCStr);