Skip to content

Commit 3eb74c2

Browse files
authored
fix: add type-safety for screenshots result array (#4843)
The screenshots array had wrong typing causing crashes due to type casting.
1 parent d3a65fb commit 3eb74c2

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
- Fix missing `sample_rate` in baggage (#4751)
2323
- Serializing SentryGeo with `nil` values (#4724)
24+
- Add type-safety for screenshots result array (#4843)
2425

2526
### Internal
2627

Sources/Sentry/SentryScreenshot.m

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ - (instancetype)init
2121
return self;
2222
}
2323

24-
- (NSArray<NSData *> *)appScreenshotsFromMainThread
24+
- (NSArray<UIImage *> *)appScreenshotsFromMainThread
2525
{
26-
__block NSArray *result;
26+
__block NSArray<UIImage *> *result;
2727

2828
void (^takeScreenShot)(void) = ^{ result = [self appScreenshots]; };
2929

@@ -33,6 +33,18 @@ - (instancetype)init
3333
return result;
3434
}
3535

36+
- (NSArray<NSData *> *)appScreenshotDatasFromMainThread
37+
{
38+
__block NSArray<NSData *> *result;
39+
40+
void (^takeScreenShot)(void) = ^{ result = [self appScreenshotsData]; };
41+
42+
[[SentryDependencyContainer sharedInstance].dispatchQueueWrapper
43+
dispatchSyncOnMainQueue:takeScreenShot];
44+
45+
return result;
46+
}
47+
3648
- (void)saveScreenShots:(NSString *)imagesDirectoryPath
3749
{
3850
// This function does not dispatch the screenshot to the main thread.

Sources/Sentry/SentryScreenshotIntegration.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ - (void)uninstall
8585
return attachments;
8686
}
8787

88-
NSArray *screenshot =
89-
[SentryDependencyContainer.sharedInstance.screenshot appScreenshotsFromMainThread];
88+
NSArray<NSData *> *screenshot =
89+
[SentryDependencyContainer.sharedInstance.screenshot appScreenshotDatasFromMainThread];
9090

9191
NSMutableArray *result =
9292
[NSMutableArray arrayWithCapacity:attachments.count + screenshot.count];

Sources/Sentry/include/SentryScreenshot.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ NS_ASSUME_NONNULL_BEGIN
1212
* Get a screenshot of every open window in the app.
1313
* @return An array of @c NSData instances containing PNG images.
1414
*/
15-
- (NSArray<NSData *> *)appScreenshotsFromMainThread;
15+
- (NSArray<NSData *> *)appScreenshotDatasFromMainThread;
16+
17+
/**
18+
* Get a screenshot of every open window in the app.
19+
* @return An array of @c UIImage instances.
20+
*/
21+
- (NSArray<UIImage *> *)appScreenshotsFromMainThread;
1622

1723
/**
1824
* Save the current app screen shots in the given directory.

Tests/SentryTests/Integrations/Screenshot/TestSentryScreenShot.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
class TestSentryScreenshot: SentryScreenshot {
44

55
var result: [Data] = []
6+
var images: [UIImage] = []
67
var processScreenshotsCallback: (() -> Void)?
78

89
override func appScreenshotsData() -> [Data] {
910
processScreenshotsCallback?()
1011
return result
1112
}
1213

13-
override func appScreenshotsFromMainThread() -> [Data] {
14-
return result
14+
override func appScreenshotsFromMainThread() -> [UIImage] {
15+
return images
1516
}
1617
}
1718

Tests/SentryTests/SentryScreenShotTests.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class SentryScreenShotTests: XCTestCase {
2626
}
2727

2828
func test_IsMainThread() {
29+
// -- Arrange --
2930
let testWindow = TestWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
3031
var isMainThread = false
3132

@@ -35,15 +36,16 @@ class SentryScreenShotTests: XCTestCase {
3536

3637
fixture.uiApplication.windows = [testWindow]
3738

38-
let queue = DispatchQueue(label: "TestQueue")
39-
39+
// -- Act --
4040
let expect = expectation(description: "Screenshot")
41+
let queue = DispatchQueue(label: "TestQueue")
4142
let _ = queue.async {
4243
self.fixture.sut.appScreenshotsFromMainThread()
4344
expect.fulfill()
4445
}
45-
4646
wait(for: [expect], timeout: 1)
47+
48+
// -- Assert --
4749
XCTAssertTrue(isMainThread)
4850
}
4951

0 commit comments

Comments
 (0)