Skip to content

Commit 9f7dc5b

Browse files
authored
(Crashlytics) Fix VeraCode scanner issue for: "LOW: Unchecked Error Condition" (#4669)
1 parent f6e1a6c commit 9f7dc5b

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

Crashlytics/Crashlytics/Helpers/FIRCLSAllocate.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ void* FIRCLSAllocatorSafeAllocateFromRegion(FIRCLSAllocationRegion* region, size
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) {
177177
FIRCLSSDKLog("Unable to allocate sufficient memory, falling back to malloc\n");
178-
return malloc(size);
178+
void* ptr = malloc(size);
179+
if (!ptr) {
180+
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocateFromRegion\n");
181+
return NULL;
182+
}
183+
return ptr;
179184
}
180185

181186
newCursor = (void*)((uintptr_t)originalCursor + size);
@@ -192,12 +197,22 @@ void* FIRCLSAllocatorSafeAllocate(FIRCLSAllocatorRef allocator,
192197
if (!allocator) {
193198
// fall back to malloc in this case
194199
FIRCLSSDKLog("Allocator invalid, falling back to malloc\n");
195-
return malloc(size);
200+
void* ptr = malloc(size);
201+
if (!ptr) {
202+
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocate\n");
203+
return NULL;
204+
}
205+
return ptr;
196206
}
197207

198208
if (allocator->protectionEnabled) {
199209
FIRCLSSDKLog("Allocator already protected, falling back to malloc\n");
200-
return malloc(size);
210+
void* ptr = malloc(size);
211+
if (!ptr) {
212+
FIRCLSSDKLog("Unable to malloc in FIRCLSAllocatorSafeAllocate\n");
213+
return NULL;
214+
}
215+
return ptr;
201216
}
202217

203218
switch (type) {

Crashlytics/Crashlytics/Helpers/FIRCLSFile.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ static bool FIRCLSFileInit(FIRCLSFile* file, int fd, bool appendMode, bool buffe
7373
file->bufferWrites = bufferWrites;
7474
if (bufferWrites) {
7575
file->writeBuffer = malloc(FIRCLSWriteBufferLength * sizeof(char));
76+
if (!file->writeBuffer) {
77+
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileInit");
78+
return false;
79+
}
80+
7681
file->writeBufferLength = 0;
7782
}
7883

@@ -645,6 +650,7 @@ void FIRCLSFileWriteArrayEntryHexEncodedString(FIRCLSFile* file, const char* val
645650
char* encodedBuffer = malloc(length * 2 + 1);
646651

647652
if (!encodedBuffer) {
653+
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileHexEncodeString");
648654
return nil;
649655
}
650656

@@ -667,6 +673,10 @@ void FIRCLSFileWriteArrayEntryHexEncodedString(FIRCLSFile* file, const char* val
667673
NSString* FIRCLSFileHexDecodeString(const char* string) {
668674
size_t length = strlen(string);
669675
char* decodedBuffer = malloc(length); // too long, but safe
676+
if (!decodedBuffer) {
677+
FIRCLSErrorLog(@"Unable to malloc in FIRCLSFileHexDecodeString");
678+
return nil;
679+
}
670680

671681
memset(decodedBuffer, 0, length);
672682

Crashlytics/Crashlytics/Helpers/FIRCLSUtility.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ bool FIRCLSUnlinkIfExists(const char* path) {
168168
size = (length * 2) + 1;
169169
buffer = malloc(sizeof(char) * size);
170170

171+
if (!buffer) {
172+
FIRCLSErrorLog(@"Unable to malloc in FIRCLSNSDataToNSString");
173+
return nil;
174+
}
175+
171176
FIRCLSSafeHexToString([data bytes], length, buffer);
172177

173178
string = [NSString stringWithUTF8String:buffer];

Crashlytics/Shared/FIRCLSByteUtility.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ void FIRCLSSafeHexToString(const uint8_t *value, size_t length, char *outputBuff
6060
size = (length * 2) + 1;
6161
buffer = malloc(sizeof(char) * size);
6262

63+
if (!buffer) {
64+
return nil;
65+
}
66+
6367
FIRCLSSafeHexToString(data.bytes, length, buffer);
6468

6569
string = [NSString stringWithUTF8String:buffer];

Crashlytics/Shared/FIRCLSMachO/FIRCLSMachOBinary.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ static void FIRCLSSafeHexToString(const uint8_t* value, size_t length, char* out
146146
size = (length * 2) + 1;
147147
buffer = malloc(sizeof(char) * size);
148148

149+
if (!buffer) {
150+
return nil;
151+
}
152+
149153
FIRCLSSafeHexToString([data bytes], length, buffer);
150154

151155
string = [NSString stringWithUTF8String:buffer];

0 commit comments

Comments
 (0)