Skip to content

Commit 4de32c7

Browse files
authored
add explanation of how alloc works (#14288)
1 parent 5006c2a commit 4de32c7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Crashlytics/Crashlytics/Models/Record/FIRCLSReportAdapter.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,21 @@ - (google_crashlytics_Platforms)protoPlatformFromString:(NSString *)str {
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.
260+
// Here is a short explaination on how it is calculated so buffer overflow is prevented:
261+
// We will alloc an amount of memeory for struct `pb_bytes_array_t`, this struct contains two
262+
// attributes:
263+
// pb_size_t size
264+
// pb_byte_t bytes[1]
265+
// It contains the size the of the data and the actually data information in byte form (which
266+
// is represented by a pointer), for more information check the declaration in nanopb/pb.h.
267+
268+
// For size, NSData return size in `unsigned long` type which is the same size as `pb_size_t` and
269+
// it is declared in compile time depending on the arch of system. If overflow happened it should
270+
// happend at NSData level first when user trying to inserting data to NSData.
271+
// For bytes, it is just a strict memeory copy of the data in NSData.
272+
// The whole structure will be freed as a part of process for deallocing report in dealloc() of
273+
// this class
259274
pb_bytes_array_t *pbBytes = malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
260275
if (pbBytes == NULL) {
261276
return NULL;

0 commit comments

Comments
 (0)