From ed15636fd7616e9171b862e3bd42aebeac027b17 Mon Sep 17 00:00:00 2001 From: Krystof Woldrich Date: Tue, 29 Nov 2022 10:56:48 +0100 Subject: [PATCH 1/6] fix(screenshots): Don't capture zero size screenshots --- CHANGELOG.md | 6 ++++++ Sources/Sentry/SentryScreenshot.m | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f6ca4b08d..031a4ad684 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Don't capture zero size screenshots + ## 7.31.2 ### Fixes diff --git a/Sources/Sentry/SentryScreenshot.m b/Sources/Sentry/SentryScreenshot.m index a3eba3e120..f48c82d010 100644 --- a/Sources/Sentry/SentryScreenshot.m +++ b/Sources/Sentry/SentryScreenshot.m @@ -47,7 +47,9 @@ - (void)saveScreenShots:(NSString *)path if ([window drawViewHierarchyInRect:window.bounds afterScreenUpdates:false]) { UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); - [result addObject:UIImagePNGRepresentation(img)]; + if (img.size.width > 0 && img.size.height > 0) { + [result addObject:UIImagePNGRepresentation(img)]; + } } UIGraphicsEndImageContext(); From 7d9c5950361833125c290fed5bd26a5f3252a4e9 Mon Sep 17 00:00:00 2001 From: Krystof Woldrich Date: Tue, 29 Nov 2022 11:32:07 +0100 Subject: [PATCH 2/6] Update changelog with pr id --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 031a4ad684..cec3bc0d42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- Don't capture zero size screenshots +- Don't capture zero size screenshots ([#2459](https://github.com/getsentry/sentry-cocoa/pull/2459)) ## 7.31.2 From d8f9d5fe89aa6c08f789d7e7f5f87319d410d1ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kry=C5=A1tof=20Wold=C5=99ich?= <31292499+krystofwoldrich@users.noreply.github.com> Date: Tue, 29 Nov 2022 13:20:58 +0100 Subject: [PATCH 3/6] Update Sources/Sentry/SentryScreenshot.m Co-authored-by: Dhiogo Brustolin --- Sources/Sentry/SentryScreenshot.m | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sources/Sentry/SentryScreenshot.m b/Sources/Sentry/SentryScreenshot.m index f48c82d010..7b378f708b 100644 --- a/Sources/Sentry/SentryScreenshot.m +++ b/Sources/Sentry/SentryScreenshot.m @@ -47,8 +47,11 @@ - (void)saveScreenShots:(NSString *)path if ([window drawViewHierarchyInRect:window.bounds afterScreenUpdates:false]) { UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); - if (img.size.width > 0 && img.size.height > 0) { - [result addObject:UIImagePNGRepresentation(img)]; + if (img.size.width > 0 || img.size.height > 0) { + NSData* bytes = UIImagePNGRepresentation(img); + if (bytes && bytes.length > 0) { + [result addObject:bytes]; + } } } From 645d46937a82d9b441107f984ec8d58df2413f41 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Tue, 29 Nov 2022 14:03:20 +0000 Subject: [PATCH 4/6] Format code --- Sources/Sentry/SentryScreenshot.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Sentry/SentryScreenshot.m b/Sources/Sentry/SentryScreenshot.m index 7b378f708b..9fe1ee45ab 100644 --- a/Sources/Sentry/SentryScreenshot.m +++ b/Sources/Sentry/SentryScreenshot.m @@ -48,9 +48,9 @@ - (void)saveScreenShots:(NSString *)path if ([window drawViewHierarchyInRect:window.bounds afterScreenUpdates:false]) { UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); if (img.size.width > 0 || img.size.height > 0) { - NSData* bytes = UIImagePNGRepresentation(img); + NSData *bytes = UIImagePNGRepresentation(img); if (bytes && bytes.length > 0) { - [result addObject:bytes]; + [result addObject:bytes]; } } } From c8a2689cc0c35670b0fae670edf931faf0b3501a Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Thu, 1 Dec 2022 17:07:15 +0100 Subject: [PATCH 5/6] Add test --- Tests/SentryTests/SentryScreenShotTests.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Tests/SentryTests/SentryScreenShotTests.swift b/Tests/SentryTests/SentryScreenShotTests.swift index e8dc672548..351d72697b 100644 --- a/Tests/SentryTests/SentryScreenShotTests.swift +++ b/Tests/SentryTests/SentryScreenShotTests.swift @@ -80,7 +80,18 @@ class SentryScreenShotTests: XCTestCase { XCTAssertEqual(image?.size.width, 10) XCTAssertEqual(image?.size.height, 10) + } + + func test_ZeroSizeScreenShot_GetsDiscarded() { + let testWindow = TestWindow(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) + fixture.uiApplication.windows = [testWindow] + + guard let data = self.fixture.sut.appScreenshots() else { + XCTFail("Could not make window screenshot") + return + } + XCTAssertEqual(0, data.count, "No screenshot should be taken, cause the image has zero size.") } class TestSentryUIApplication: SentryUIApplication { From 8b99397091c0a645308f4e0c0476359c3c129f65 Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Thu, 1 Dec 2022 17:07:58 +0100 Subject: [PATCH 6/6] fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3a3c6960e..78da7b78a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This version adds a dependency on Swift. - Increase `SentryCrashMAX_STRINGBUFFERSIZE` to reduce the instances where we're dropping a crash due to size limit (#2465) - `SentryAppStateManager` correctly unsubscribes from `NSNotificationCenter` when closing the SDK (#2460) - The SDK no longer reports an OOM when a crash happens after closing the SDK (#2468) +- Don't capture zero size screenshots ([#2459](https://github.com/getsentry/sentry-cocoa/pull/2459)) ### Breaking Changes @@ -51,7 +52,6 @@ This version adds a dependency on Swift. ### Fixes -- Don't capture zero size screenshots ([#2459](https://github.com/getsentry/sentry-cocoa/pull/2459)) - Reporting crashes when restarting the SDK (#2440) - Core data span status with error (#2439)