Skip to content

Commit

Permalink
fix comment and migrate to calloc for session (#14295)
Browse files Browse the repository at this point in the history
  • Loading branch information
themiswang authored Dec 30, 2024
1 parent 93bd881 commit f53dfc3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
16 changes: 8 additions & 8 deletions Crashlytics/Crashlytics/Helpers/FIRCLSAllocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions Crashlytics/Crashlytics/Helpers/FIRCLSFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -671,7 +671,7 @@ void FIRCLSFileWriteArrayEntryHexEncodedString(FIRCLSFile* file, const char* val
char* encodedBuffer = calloc(1, length * 2 + 1);

if (!encodedBuffer) {
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileHexEncodeString");
FIRCLSErrorLog(@"Unable to calloc in FIRCLSFileHexEncodeString");
return nil;
}

Expand All @@ -695,7 +695,7 @@ void FIRCLSFileWriteArrayEntryHexEncodedString(FIRCLSFile* file, const char* val
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;
}

Expand Down
2 changes: 1 addition & 1 deletion Crashlytics/Crashlytics/Helpers/FIRCLSUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void FIRCLSRedactUUID(char* value) {
buffer = calloc(1, sizeof(char) * size);

if (!buffer) {
FIRCLSErrorLog(@"Unable to malloc in FIRCLSNSDataToNSString");
FIRCLSErrorLog(@"Unable to calloc in FIRCLSNSDataToNSString");
return nil;
}

Expand Down
6 changes: 3 additions & 3 deletions Crashlytics/Crashlytics/Models/Record/FIRCLSReportAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ - (google_crashlytics_Platforms)protoPlatformFromString:(NSString *)str {
}
}

/** 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.
*/
Expand All @@ -251,12 +251,12 @@ - (google_crashlytics_Platforms)protoPlatformFromString:(NSString *)str {
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:
Expand Down
4 changes: 2 additions & 2 deletions FirebaseSessions/SourcesObjC/NanoPB/FIRSESNanoPBHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions FirebaseSessions/SourcesObjC/NanoPB/FIRSESNanoPBHelpers.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ void nanopb_free(void *_Nullable ptr) {
}
#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;
}
Expand All @@ -101,7 +101,7 @@ void nanopb_free(void *_Nullable ptr) {
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.
*/
Expand Down Expand Up @@ -174,7 +174,7 @@ pb_size_t FIRSESGetAppleApplicationInfoTag(void) {
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);
Expand Down

0 comments on commit f53dfc3

Please sign in to comment.