Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unchecked malloc #5431

Merged
merged 4 commits into from
Apr 22, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Code review comments
  • Loading branch information
morganchen12 committed Apr 21, 2020
commit 3d59501b66464c3d3ae6239f94d6a83b760e5574
37 changes: 20 additions & 17 deletions Crashlytics/Crashlytics/Models/Record/FIRCLSReportAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,23 @@ - (google_crashlytics_FilesPayload)protoFilesPayload {
google_crashlytics_FilesPayload_File *files =
malloc(sizeof(google_crashlytics_FilesPayload_File) * clsRecords.count);

if (files != NULL) {
for (NSUInteger i = 0; i < clsRecords.count; i++) {
google_crashlytics_FilesPayload_File file = google_crashlytics_FilesPayload_File_init_default;
file.filename = FIRCLSEncodeString(clsRecords[i].lastPathComponent);

NSError *error;
file.contents = FIRCLSEncodeData([NSData dataWithContentsOfFile:clsRecords[i]
options:0
error:&error]);
if (error) {
FIRCLSErrorLog(@"Failed to read from %@ with error: %@", clsRecords[i], error);
}

files[i] = file;
if (files == NULL) {
// files and files_count are initialized to NULL and 0 by default.
return apple_payload;
}
for (NSUInteger i = 0; i < clsRecords.count; i++) {
google_crashlytics_FilesPayload_File file = google_crashlytics_FilesPayload_File_init_default;
file.filename = FIRCLSEncodeString(clsRecords[i].lastPathComponent);

NSError *error;
file.contents = FIRCLSEncodeData([NSData dataWithContentsOfFile:clsRecords[i]
options:0
error:&error]);
if (error) {
FIRCLSErrorLog(@"Failed to read from %@ with error: %@", clsRecords[i], error);
}

files[i] = file;
}

apple_payload.files = files;
Expand Down Expand Up @@ -232,10 +234,11 @@ - (google_crashlytics_Platforms)protoPlatformFromString:(NSString *)str {
*/
pb_bytes_array_t *FIRCLSEncodeData(NSData *data) {
pb_bytes_array_t *pbBytes = malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
if (pbBytes != NULL) {
memcpy(pbBytes->bytes, [data bytes], data.length);
pbBytes->size = (pb_size_t)data.length;
if (pbBytes == NULL) {
return NULL;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - can we just return NULL when pbBytes == null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

memcpy(pbBytes->bytes, [data bytes], data.length);
pbBytes->size = (pb_size_t)data.length;
return pbBytes;
}

Expand Down