Skip to content

Commit f53dfc3

Browse files
authored
fix comment and migrate to calloc for session (firebase#14295)
1 parent 93bd881 commit f53dfc3

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

Crashlytics/Crashlytics/Helpers/FIRCLSAllocate.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ FIRCLSAllocatorRef FIRCLSAllocatorCreate(size_t writableSpace, size_t readableSp
5656
}
5757

5858
// Make one big, continuous allocation, adding additional pages for our guards. Note
59-
// that we cannot use malloc (or valloc) in this case, because we need to assert full
59+
// that we cannot use malloc, calloc (or valloc) in this case, because we need to assert full
6060
// ownership over these allocations. mmap is a much better choice. We also mark these
6161
// pages as MAP_NOCACHE.
6262
allocationSize = writableRegion.size + readableRegion.size + pageSize * 3;
@@ -174,10 +174,10 @@ void* FIRCLSAllocatorSafeAllocateFromRegion(FIRCLSAllocationRegion* region, size
174174

175175
// this shouldn't happen unless we make a mistake with our size pre-computations
176176
if ((uintptr_t)originalCursor - (uintptr_t)region->start + size > region->size) {
177-
FIRCLSSDKLog("Unable to allocate sufficient memory, falling back to malloc\n");
177+
FIRCLSSDKLog("Unable to allocate sufficient memory, falling back to calloc\n");
178178
void* ptr = calloc(1, size);
179179
if (!ptr) {
180-
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocateFromRegion\n");
180+
FIRCLSSDKLog("Unable to calloc in FIRCLSAllocatorSafeAllocateFromRegion\n");
181181
return NULL;
182182
}
183183
return ptr;
@@ -195,21 +195,21 @@ void* FIRCLSAllocatorSafeAllocate(FIRCLSAllocatorRef allocator,
195195
FIRCLSAllocationRegion* region;
196196

197197
if (!allocator) {
198-
// fall back to malloc in this case
199-
FIRCLSSDKLog("Allocator invalid, falling back to malloc\n");
198+
// fall back to calloc in this case
199+
FIRCLSSDKLog("Allocator invalid, falling back to calloc\n");
200200
void* ptr = calloc(1, size);
201201
if (!ptr) {
202-
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocate\n");
202+
FIRCLSSDKLog("Unable to calloc in FIRCLSAllocatorSafeAllocate\n");
203203
return NULL;
204204
}
205205
return ptr;
206206
}
207207

208208
if (allocator->protectionEnabled) {
209-
FIRCLSSDKLog("Allocator already protected, falling back to malloc\n");
209+
FIRCLSSDKLog("Allocator already protected, falling back to calloc\n");
210210
void* ptr = calloc(1, size);
211211
if (!ptr) {
212-
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocate\n");
212+
FIRCLSSDKLog("Unable to calloc in FIRCLSAllocatorSafeAllocate\n");
213213
return NULL;
214214
}
215215
return ptr;

Crashlytics/Crashlytics/Helpers/FIRCLSFile.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static bool FIRCLSFileInit(
7676
if (bufferWrites) {
7777
file->writeBuffer = calloc(1, FIRCLSWriteBufferLength * sizeof(char));
7878
if (!file->writeBuffer) {
79-
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileInit");
79+
FIRCLSErrorLog(@"Unable to calloc in FIRCLSFileInit");
8080
return false;
8181
}
8282

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

673673
if (!encodedBuffer) {
674-
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileHexEncodeString");
674+
FIRCLSErrorLog(@"Unable to calloc in FIRCLSFileHexEncodeString");
675675
return nil;
676676
}
677677

@@ -695,7 +695,7 @@ void FIRCLSFileWriteArrayEntryHexEncodedString(FIRCLSFile* file, const char* val
695695
size_t length = strlen(string);
696696
char* decodedBuffer = calloc(1, length); // too long, but safe
697697
if (!decodedBuffer) {
698-
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileHexDecodeString");
698+
FIRCLSErrorLog(@"Unable to calloc in FIRCLSFileHexDecodeString");
699699
return nil;
700700
}
701701

Crashlytics/Crashlytics/Helpers/FIRCLSUtility.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void FIRCLSRedactUUID(char* value) {
188188
buffer = calloc(1, sizeof(char) * size);
189189

190190
if (!buffer) {
191-
FIRCLSErrorLog(@"Unable to malloc in FIRCLSNSDataToNSString");
191+
FIRCLSErrorLog(@"Unable to calloc in FIRCLSNSDataToNSString");
192192
return nil;
193193
}
194194

Crashlytics/Crashlytics/Models/Record/FIRCLSReportAdapter.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ - (google_crashlytics_Platforms)protoPlatformFromString:(NSString *)str {
236236
}
237237
}
238238

239-
/** Mallocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
239+
/** Callocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
240240
* @note Memory needs to be freed manually, through pb_free or pb_release.
241241
* @param string The string to encode as pb_bytes.
242242
*/
@@ -251,12 +251,12 @@ - (google_crashlytics_Platforms)protoPlatformFromString:(NSString *)str {
251251
return FIRCLSEncodeData(stringBytes);
252252
}
253253

254-
/** Mallocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
254+
/** Callocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
255255
* @note Memory needs to be free manually, through pb_free or pb_release.
256256
* @param data The data to copy into the new bytes array.
257257
*/
258258
pb_bytes_array_t *FIRCLSEncodeData(NSData *data) {
259-
// We have received couple security tickets before for using malloc here.
259+
// We have received couple security tickets before for using calloc here.
260260
// Here is a short explaination on how it is calculated so buffer overflow is prevented:
261261
// We will alloc an amount of memeory for struct `pb_bytes_array_t`, this struct contains two
262262
// attributes:

FirebaseSessions/SourcesObjC/NanoPB/FIRSESNanoPBHelpers.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ NSData* _Nullable FIRSESEncodeProto(const pb_field_t fields[],
4949
NSError** error);
5050
#pragma clang diagnostic pop
5151

52-
/// Mallocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
52+
/// Callocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
5353
/// @note Memory needs to be freed manually, through pb_free or pb_release.
5454
/// @param data The data to copy into the new bytes array.
5555
pb_bytes_array_t* _Nullable FIRSESEncodeData(NSData* _Nullable data);
5656

57-
/// Mallocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
57+
/// Callocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
5858
/// @note Memory needs to be freed manually, through pb_free or pb_release.
5959
/// @param string The string to encode as pb_bytes.
6060
pb_bytes_array_t* _Nullable FIRSESEncodeString(NSString* _Nullable string);

FirebaseSessions/SourcesObjC/NanoPB/FIRSESNanoPBHelpers.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ void nanopb_free(void *_Nullable ptr) {
8787
}
8888
#pragma clang diagnostic pop
8989

90-
/** Mallocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
90+
/** Callocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
9191
* @note Memory needs to be free manually, through pb_free or pb_release.
9292
* @param data The data to copy into the new bytes array.
9393
*/
9494
pb_bytes_array_t *_Nullable FIRSESEncodeData(NSData *_Nullable data) {
95-
pb_bytes_array_t *pbBytes = malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
95+
pb_bytes_array_t *pbBytes = calloc(1, PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
9696
if (pbBytes == NULL) {
9797
return NULL;
9898
}
@@ -101,7 +101,7 @@ void nanopb_free(void *_Nullable ptr) {
101101
return pbBytes;
102102
}
103103

104-
/** Mallocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
104+
/** Callocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
105105
* @note Memory needs to be freed manually, through pb_free or pb_release.
106106
* @param string The string to encode as pb_bytes.
107107
*/
@@ -174,7 +174,7 @@ pb_size_t FIRSESGetAppleApplicationInfoTag(void) {
174174
size_t size;
175175
sysctlbyname(sysctlKey, NULL, &size, NULL, 0);
176176
if (size > 0) {
177-
char *entryValueCStr = malloc(size);
177+
char *entryValueCStr = calloc(1, size);
178178
sysctlbyname(sysctlKey, entryValueCStr, &size, NULL, 0);
179179
entryValue = [NSString stringWithCString:entryValueCStr encoding:NSUTF8StringEncoding];
180180
free(entryValueCStr);

0 commit comments

Comments
 (0)