Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 386ac02

Browse files
committed
Fix potential stack overflow usage in createFile and writeChunk API.
1 parent 0fb4652 commit 386ac02

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,15 @@ - (NSDictionary *)constantsToExport
209209

210210
NSFileManager * fm = [NSFileManager defaultManager];
211211
NSMutableData * fileContent = [NSMutableData alloc];
212-
213-
char bytes[[dataArray count]];
212+
// prevent stack overflow, alloc on heap
213+
char * bytes = (char*) malloc([dataArray count]);
214+
// char bytes[[dataArray count]];
214215
for(int i = 0; i < dataArray.count; i++) {
215216
bytes[i] = [[dataArray objectAtIndex:i] charValue];
216217
}
217218
[fileContent appendBytes:bytes length:dataArray.count];
218219
BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
219-
220+
free(bytes);
220221
if(success == YES)
221222
callback(@[[NSNull null]]);
222223
else
@@ -259,13 +260,15 @@ - (NSDictionary *)constantsToExport
259260

260261
RCT_EXPORT_METHOD(writeArrayChunk:(NSString *)streamId withArray:(NSArray *)dataArray callback:(RCTResponseSenderBlock) callback) {
261262
RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
262-
char bytes[[dataArray count]];
263+
// char bytes[[dataArray count]];
264+
char * bytes = (char *) malloc([dataArray count]);
263265
for(int i = 0; i < dataArray.count; i++) {
264266
bytes[i] = [[dataArray objectAtIndex:i] charValue];
265267
}
266268
NSMutableData * data = [NSMutableData alloc];
267269
[data appendBytes:bytes length:dataArray.count];
268270
[fs write:data];
271+
free(bytes);
269272
callback(@[[NSNull null]]);
270273
}
271274

0 commit comments

Comments
 (0)