Skip to content

Commit

Permalink
Change malloc to calloc (#14289)
Browse files Browse the repository at this point in the history
  • Loading branch information
themiswang authored Dec 27, 2024
1 parent 4de32c7 commit 93bd881
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions Crashlytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased
- [fixed] Updated `upload-symbols` to version 3.20, wait for `debug.dylib` DWARF content getting generated when build with `--build-phase` option. Added `debug.dylib` DWARF content to run script input file list for user who enabled user script sandboxing (#14054).
- [fixed] Updated all memory allocation from `malloc()` to `calloc()` (#14209).

# 11.5.0
- [changed] Updated `upload-symbols` to version 3.19, removed all methods require CFRelease and switch to modern classes (#13420).
Expand Down
2 changes: 1 addition & 1 deletion Crashlytics/Crashlytics/Components/FIRCLSBinaryImage.m
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ static void FIRCLSBinaryImageChanged(bool added,
// fill imageDetails fields using slice & vmaddr_slide
FIRCLSBinaryImageFillInImageDetails(&imageDetails);

FIRCLSImageChange* change = malloc(sizeof(FIRCLSImageChange));
FIRCLSImageChange* change = calloc(1, sizeof(FIRCLSImageChange));
if (!change) return;
change->added = added;
change->details = imageDetails;
Expand Down
6 changes: 3 additions & 3 deletions Crashlytics/Crashlytics/Helpers/FIRCLSAllocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ 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");
void* ptr = malloc(size);
void* ptr = calloc(1, size);
if (!ptr) {
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocateFromRegion\n");
return NULL;
Expand All @@ -197,7 +197,7 @@ void* FIRCLSAllocatorSafeAllocate(FIRCLSAllocatorRef allocator,
if (!allocator) {
// fall back to malloc in this case
FIRCLSSDKLog("Allocator invalid, falling back to malloc\n");
void* ptr = malloc(size);
void* ptr = calloc(1, size);
if (!ptr) {
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocate\n");
return NULL;
Expand All @@ -207,7 +207,7 @@ void* FIRCLSAllocatorSafeAllocate(FIRCLSAllocatorRef allocator,

if (allocator->protectionEnabled) {
FIRCLSSDKLog("Allocator already protected, falling back to malloc\n");
void* ptr = malloc(size);
void* ptr = calloc(1, size);
if (!ptr) {
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocate\n");
return NULL;
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 @@ -74,7 +74,7 @@ static bool FIRCLSFileInit(

file->bufferWrites = bufferWrites;
if (bufferWrites) {
file->writeBuffer = malloc(FIRCLSWriteBufferLength * sizeof(char));
file->writeBuffer = calloc(1, FIRCLSWriteBufferLength * sizeof(char));
if (!file->writeBuffer) {
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileInit");
return false;
Expand Down Expand Up @@ -668,7 +668,7 @@ void FIRCLSFileWriteArrayEntryHexEncodedString(FIRCLSFile* file, const char* val

NSString* FIRCLSFileHexEncodeString(const char* string) {
size_t length = strlen(string);
char* encodedBuffer = malloc(length * 2 + 1);
char* encodedBuffer = calloc(1, length * 2 + 1);

if (!encodedBuffer) {
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileHexEncodeString");
Expand All @@ -693,7 +693,7 @@ void FIRCLSFileWriteArrayEntryHexEncodedString(FIRCLSFile* file, const char* val

NSString* FIRCLSFileHexDecodeString(const char* string) {
size_t length = strlen(string);
char* decodedBuffer = malloc(length); // too long, but safe
char* decodedBuffer = calloc(1, length); // too long, but safe
if (!decodedBuffer) {
FIRCLSErrorLog(@"Unable to malloc 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 @@ -185,7 +185,7 @@ void FIRCLSRedactUUID(char* value) {
// null terminator
length = [data length];
size = (length * 2) + 1;
buffer = malloc(sizeof(char) * size);
buffer = calloc(1, sizeof(char) * size);

if (!buffer) {
FIRCLSErrorLog(@"Unable to malloc in FIRCLSNSDataToNSString");
Expand Down
4 changes: 2 additions & 2 deletions Crashlytics/Crashlytics/Models/Record/FIRCLSReportAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ - (google_crashlytics_FilesPayload)protoFilesPayload {

NSArray<NSString *> *clsRecords = [self clsRecordFilePaths];
google_crashlytics_FilesPayload_File *files =
malloc(sizeof(google_crashlytics_FilesPayload_File) * clsRecords.count);
calloc(1, sizeof(google_crashlytics_FilesPayload_File) * clsRecords.count);

if (files == NULL) {
// files and files_count are initialized to NULL and 0 by default.
Expand Down Expand Up @@ -271,7 +271,7 @@ - (google_crashlytics_Platforms)protoPlatformFromString:(NSString *)str {
// For bytes, it is just a strict memeory copy of the data in NSData.
// The whole structure will be freed as a part of process for deallocing report in dealloc() of
// this class
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 Down
2 changes: 1 addition & 1 deletion Crashlytics/Shared/FIRCLSByteUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void FIRCLSSafeHexToString(const uint8_t *value, size_t length, char *outputBuff
// null terminator
length = data.length;
size = (length * 2) + 1;
buffer = malloc(sizeof(char) * size);
buffer = calloc(1, sizeof(char) * size);

if (!buffer) {
return nil;
Expand Down
2 changes: 1 addition & 1 deletion Crashlytics/Shared/FIRCLSMachO/FIRCLSMachOBinary.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static void FIRCLSSafeHexToString(const uint8_t* value, size_t length, char* out
// null terminator
length = [data length];
size = (length * 2) + 1;
buffer = malloc(sizeof(char) * size);
buffer = calloc(1, sizeof(char) * size);

if (!buffer) {
return nil;
Expand Down
2 changes: 1 addition & 1 deletion Crashlytics/UnitTests/FIRCLSCompactUnwindTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ @implementation FIRCLSCompactUnwindTests
- (void)setUp {
[super setUp];

_firclsContext.readonly = malloc(sizeof(FIRCLSReadOnlyContext));
_firclsContext.readonly = calloc(1, sizeof(FIRCLSReadOnlyContext));
_firclsContext.readonly->logPath = "/tmp/test.log";
}

Expand Down
2 changes: 1 addition & 1 deletion Crashlytics/UnitTests/FIRCLSDwarfTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ @implementation FIRCLSDwarfTests
- (void)setUp {
[super setUp];

_firclsContext.readonly = malloc(sizeof(FIRCLSReadOnlyContext));
_firclsContext.readonly = calloc(1, sizeof(FIRCLSReadOnlyContext));
_firclsContext.readonly->logPath = "/tmp/test.log";
}

Expand Down
4 changes: 2 additions & 2 deletions Crashlytics/UnitTests/FIRCLSFileTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ - (void)hexEncodingLongStringWithFile:(FIRCLSFile *)file
filePath:(NSString *)filePath
length:(size_t)length
buffered:(BOOL)buffered {
char *longString = malloc(length * sizeof(char));
char *longString = calloc(1, length * sizeof(char));

memset(longString, 'a', length); // fill it with 'a' characters
longString[length - 1] = 0; // null terminate
Expand Down Expand Up @@ -432,7 +432,7 @@ - (void)closeAndOpenAlternatingBufferedOptionWithFile:(FIRCLSFile *)file

- (void)testLoggingInputLongerThanBuffer {
size_t inputLength = (FIRCLSWriteBufferLength + 2) * sizeof(char);
char *input = malloc(inputLength);
char *input = calloc(1, inputLength);
for (size_t i = 0; i < inputLength - 1; i++) {
input[i] = i % 26 + 'a';
}
Expand Down
2 changes: 1 addition & 1 deletion Crashlytics/UnitTests/FIRCLSLoggingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ - (void)testUserLogNil {

- (void)testLargeLogLine {
size_t strLength = 100 * 1024; // Attempt to write 100k of data
char* longLine = malloc(strLength + 1);
char* longLine = calloc(1, strLength + 1);
memset(longLine, 'a', strLength);
longLine[strLength] = '\0';
NSString* longStr = [[NSString alloc] initWithBytesNoCopy:longLine
Expand Down

0 comments on commit 93bd881

Please sign in to comment.