Skip to content

Commit 002d3c1

Browse files
RSNarafacebook-github-bot
authored andcommitted
Retain cropData object in ImageEditingManager.cropImage
Summary: All struct args are passed into NativeModule methods via references. If blocks access those references, we don't move those references to the heap. This means that by the time that the block accesses the struct arg, it could be freed. This can crash the program. The solution is simple: we copy the struct arg, and access the copy in the block. This ensures that the block will make a copy, which prevents the underlying data structures from being released by the time that the block accesses the struct arg. Changelog: [iOS][Fixed] - Retain cropData struct arg in ImageEditingManager.cropImage call Differential Revision: D18076026 fbshipit-source-id: 1a7bb602606ff1afac38ad5451662c82fa86f205
1 parent 94845b5 commit 002d3c1

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Libraries/Image/RCTImageEditingManager.mm

+6-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ @implementation RCTImageEditingManager
5353
@"height": @(cropData.size().height()),
5454
}]
5555
};
56+
57+
// We must keep a copy of cropData so that we can access data from it at a later time
58+
JS::NativeImageEditor::Options cropDataCopy = cropData;
5659

5760
[[_bridge moduleForName:@"ImageLoader" lazilyLoadIfNecessary:YES]
5861
loadImageWithURLRequest:imageRequest callback:^(NSError *error, UIImage *image) {
@@ -68,9 +71,9 @@ @implementation RCTImageEditingManager
6871
UIImage *croppedImage = RCTTransformImage(image, targetSize, image.scale, transform);
6972

7073
// Scale image
71-
if (cropData.displaySize()) {
72-
targetSize = [RCTConvert CGSize:@{@"width": @(cropData.displaySize()->width()), @"height": @(cropData.displaySize()->height())}]; // in pixels
73-
RCTResizeMode resizeMode = [RCTConvert RCTResizeMode:cropData.resizeMode() ?: @"contain"];
74+
if (cropDataCopy.displaySize()) {
75+
targetSize = [RCTConvert CGSize:@{@"width": @(cropDataCopy.displaySize()->width()), @"height": @(cropDataCopy.displaySize()->height())}]; // in pixels
76+
RCTResizeMode resizeMode = [RCTConvert RCTResizeMode:cropDataCopy.resizeMode() ?: @"contain"];
7477
targetRect = RCTTargetRect(croppedImage.size, targetSize, 1, resizeMode);
7578
transform = RCTTransformFromTargetRect(croppedImage.size, targetRect);
7679
croppedImage = RCTTransformImage(croppedImage, targetSize, image.scale, transform);

0 commit comments

Comments
 (0)