Skip to content

Commit 390efa3

Browse files
Merge 0a51b17 into 06bac56
2 parents 06bac56 + 0a51b17 commit 390efa3

14 files changed

+147
-81
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Fix a crash in SentryCoreDataTracker for nil error params (#3152)
8+
39
## 8.9.1
410

511
### Fixes

Sentry.xcodeproj/project.pbxproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
33042A1729DC2C4300C60085 /* SentryExtraContextProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */; };
8282
622C08D829E546F4002571D4 /* SentryTraceOrigins.h in Headers */ = {isa = PBXBuildFile; fileRef = 622C08D729E546F4002571D4 /* SentryTraceOrigins.h */; };
8383
622C08DB29E554B9002571D4 /* SentrySpanContext+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 622C08D929E554B9002571D4 /* SentrySpanContext+Private.h */; };
84+
623C45B02A651D8200D9E88B /* SentryCoreDataTracker+Test.m in Sources */ = {isa = PBXBuildFile; fileRef = 623C45AF2A651D8200D9E88B /* SentryCoreDataTracker+Test.m */; };
8485
627E7589299F6FE40085504D /* SentryInternalDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = 627E7588299F6FE40085504D /* SentryInternalDefines.h */; };
8586
62885DA729E946B100554F38 /* TestConncurrentModifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62885DA629E946B100554F38 /* TestConncurrentModifications.swift */; };
8687
62950F1029E7FE0100A42624 /* SentryTransactionContextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62950F0F29E7FE0100A42624 /* SentryTransactionContextTests.swift */; };
@@ -775,7 +776,7 @@
775776
D867063E27C3BC2400048851 /* SentryCoreDataSwizzling.h in Headers */ = {isa = PBXBuildFile; fileRef = D867063B27C3BC2400048851 /* SentryCoreDataSwizzling.h */; };
776777
D867063F27C3BC2400048851 /* SentryCoreDataTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = D867063C27C3BC2400048851 /* SentryCoreDataTracker.h */; };
777778
D86B6835294348A400B8B1FC /* SentryAttachment+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = D86B6834294348A400B8B1FC /* SentryAttachment+Private.h */; };
778-
D86F419827C8FEFA00490520 /* SentryCoreDataMiddleware+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D86F419727C8FEFA00490520 /* SentryCoreDataMiddleware+Extension.swift */; };
779+
D86F419827C8FEFA00490520 /* SentryCoreDataTrackerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D86F419727C8FEFA00490520 /* SentryCoreDataTrackerExtension.swift */; };
779780
D8751FA5274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8751FA4274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift */; };
780781
D875ED0B276CC84700422FAC /* SentryNSDataTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D875ED0A276CC84700422FAC /* SentryNSDataTrackerTests.swift */; };
781782
D880E3A728573E87008A90DB /* SentryBaggageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D880E3A628573E87008A90DB /* SentryBaggageTests.swift */; };
@@ -959,6 +960,8 @@
959960
33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryExtraContextProviderTests.swift; sourceTree = "<group>"; };
960961
622C08D729E546F4002571D4 /* SentryTraceOrigins.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryTraceOrigins.h; path = include/SentryTraceOrigins.h; sourceTree = "<group>"; };
961962
622C08D929E554B9002571D4 /* SentrySpanContext+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentrySpanContext+Private.h"; path = "include/SentrySpanContext+Private.h"; sourceTree = "<group>"; };
963+
623C45AE2A651C4500D9E88B /* SentryCoreDataTracker+Test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SentryCoreDataTracker+Test.h"; sourceTree = "<group>"; };
964+
623C45AF2A651D8200D9E88B /* SentryCoreDataTracker+Test.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "SentryCoreDataTracker+Test.m"; sourceTree = "<group>"; };
962965
627E7588299F6FE40085504D /* SentryInternalDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryInternalDefines.h; path = include/SentryInternalDefines.h; sourceTree = "<group>"; };
963966
62885DA629E946B100554F38 /* TestConncurrentModifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestConncurrentModifications.swift; sourceTree = "<group>"; };
964967
62950F0F29E7FE0100A42624 /* SentryTransactionContextTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTransactionContextTests.swift; sourceTree = "<group>"; };
@@ -1725,7 +1728,7 @@
17251728
D867063C27C3BC2400048851 /* SentryCoreDataTracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryCoreDataTracker.h; path = include/SentryCoreDataTracker.h; sourceTree = "<group>"; };
17261729
D86B6820293F39E000B8B1FC /* TestSentryViewHierarchy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestSentryViewHierarchy.h; sourceTree = "<group>"; };
17271730
D86B6834294348A400B8B1FC /* SentryAttachment+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentryAttachment+Private.h"; path = "include/SentryAttachment+Private.h"; sourceTree = "<group>"; };
1728-
D86F419727C8FEFA00490520 /* SentryCoreDataMiddleware+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SentryCoreDataMiddleware+Extension.swift"; sourceTree = "<group>"; };
1731+
D86F419727C8FEFA00490520 /* SentryCoreDataTrackerExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCoreDataTrackerExtension.swift; sourceTree = "<group>"; };
17291732
D8751FA4274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryNSURLSessionTaskSearchTests.swift; sourceTree = "<group>"; };
17301733
D8757D142A209F7300BFEFCC /* SentrySampleDecision+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentrySampleDecision+Private.h"; path = "include/SentrySampleDecision+Private.h"; sourceTree = "<group>"; };
17311734
D875ED0A276CC84700422FAC /* SentryNSDataTrackerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SentryNSDataTrackerTests.swift; sourceTree = "<group>"; };
@@ -3378,9 +3381,11 @@
33783381
isa = PBXGroup;
33793382
children = (
33803383
D884A20327C80F2700074664 /* SentryCoreDataTrackerTest.swift */,
3381-
D86F419727C8FEFA00490520 /* SentryCoreDataMiddleware+Extension.swift */,
3384+
D86F419727C8FEFA00490520 /* SentryCoreDataTrackerExtension.swift */,
33823385
D855B3E727D652AF00BCED76 /* SentryCoreDataTrackingIntegrationTest.swift */,
33833386
D855B3E927D652C700BCED76 /* TestCoreDataStack.swift */,
3387+
623C45AE2A651C4500D9E88B /* SentryCoreDataTracker+Test.h */,
3388+
623C45AF2A651D8200D9E88B /* SentryCoreDataTracker+Test.m */,
33843389
);
33853390
path = CoreData;
33863391
sourceTree = "<group>";
@@ -4285,7 +4290,7 @@
42854290
7B7D8730248648AD00D2ECFF /* SentryStacktraceBuilderTests.swift in Sources */,
42864291
62E081AB29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift in Sources */,
42874292
D8751FA5274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift in Sources */,
4288-
D86F419827C8FEFA00490520 /* SentryCoreDataMiddleware+Extension.swift in Sources */,
4293+
D86F419827C8FEFA00490520 /* SentryCoreDataTrackerExtension.swift in Sources */,
42894294
7B944FB32469C02900A10721 /* TestHub.swift in Sources */,
42904295
7BF536D124BDF3E7004FA6A2 /* SentryEnvelopeTests.swift in Sources */,
42914296
7B98D7E025FB73B900C5A389 /* SentryWatchdogTerminationsTrackerTests.swift in Sources */,
@@ -4346,6 +4351,7 @@
43464351
7B0A542E2521C62400A71716 /* SentryFrameRemoverTests.swift in Sources */,
43474352
7BE912B12721C76000E49E62 /* SentryPerformanceTrackingIntegrationTests.swift in Sources */,
43484353
7BA61CCC247D14E600C130A8 /* SentryThreadInspectorTests.swift in Sources */,
4354+
623C45B02A651D8200D9E88B /* SentryCoreDataTracker+Test.m in Sources */,
43494355
15360CF52433C59B00112302 /* SentryInstallationTests.m in Sources */,
43504356
8E0551E026A7A63C00400526 /* TestProtocolClient.swift in Sources */,
43514357
7BD86ECD264A78A6005439DB /* SentryAppStartTrackerTests.swift in Sources */,

Sources/Sentry/SentryCoreDataSwizzling.m

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11

22
#import "SentryCoreDataSwizzling.h"
3+
#import "SentryCoreDataTracker.h"
34
#import "SentrySwizzle.h"
45

5-
@interface
6-
SentryCoreDataSwizzling ()
7-
8-
@property (nonatomic, strong) id<SentryCoreDataMiddleware> middleware;
9-
10-
@end
11-
126
@implementation SentryCoreDataSwizzling
137

148
+ (SentryCoreDataSwizzling *)sharedInstance
@@ -19,18 +13,18 @@ + (SentryCoreDataSwizzling *)sharedInstance
1913
return instance;
2014
}
2115

22-
- (void)startWithMiddleware:(id<SentryCoreDataMiddleware>)middleware
16+
- (void)startWithTracker:(SentryCoreDataTracker *)coreDataTracker;
2317
{
2418
// We just need to swizzle once, than we can control execution with the middleware.
2519
static dispatch_once_t onceToken;
2620
dispatch_once(&onceToken, ^{ [self swizzleCoreData]; });
2721

28-
self.middleware = middleware;
22+
_coreDataTracker = coreDataTracker;
2923
}
3024

3125
- (void)stop
3226
{
33-
self.middleware = nil;
27+
_coreDataTracker = nil;
3428
}
3529

3630
// SentrySwizzleInstanceMethod declaration shadows a local variable. The swizzling is working
@@ -47,11 +41,10 @@ - (void)swizzleCoreData
4741
SentrySWArguments(NSFetchRequest * originalRequest, NSError * *error), SentrySWReplacement({
4842
NSArray *result;
4943

50-
id<SentryCoreDataMiddleware> middleware
51-
= SentryCoreDataSwizzling.sharedInstance.middleware;
44+
SentryCoreDataTracker *tracker = SentryCoreDataSwizzling.sharedInstance.coreDataTracker;
5245

53-
if (middleware) {
54-
result = [middleware
46+
if (tracker) {
47+
result = [tracker
5548
managedObjectContext:self
5649
executeFetchRequest:originalRequest
5750
error:error
@@ -70,15 +63,14 @@ - (void)swizzleCoreData
7063
SentrySwizzleInstanceMethod(NSManagedObjectContext.class, saveSelector,
7164
SentrySWReturnType(BOOL), SentrySWArguments(NSError * *error), SentrySWReplacement({
7265
BOOL result;
73-
id<SentryCoreDataMiddleware> middleware
74-
= SentryCoreDataSwizzling.sharedInstance.middleware;
75-
76-
if (middleware) {
77-
result = [middleware managedObjectContext:self
78-
save:error
79-
originalImp:^BOOL(NSError **outError) {
80-
return SentrySWCallOriginal(outError);
81-
}];
66+
SentryCoreDataTracker *tracker = SentryCoreDataSwizzling.sharedInstance.coreDataTracker;
67+
68+
if (tracker) {
69+
result = [tracker managedObjectContext:self
70+
save:error
71+
originalImp:^BOOL(NSError **outError) {
72+
return SentrySWCallOriginal(outError);
73+
}];
8274
} else {
8375
result = SentrySWCallOriginal(error);
8476
}

Sources/Sentry/SentryCoreDataTrackingIntegration.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ - (BOOL)installWithOptions:(SentryOptions *)options
2626
self.tracker = [[SentryCoreDataTracker alloc]
2727
initWithThreadInspector:[[SentryThreadInspector alloc] initWithOptions:options]
2828
processInfoWrapper:[SentryDependencyContainer.sharedInstance processInfoWrapper]];
29-
[SentryCoreDataSwizzling.sharedInstance startWithMiddleware:self.tracker];
29+
[SentryCoreDataSwizzling.sharedInstance startWithTracker:self.tracker];
3030

3131
return YES;
3232
}

Sources/Sentry/include/SentryCoreDataSwizzling.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,16 @@
55

66
NS_ASSUME_NONNULL_BEGIN
77

8-
@protocol SentryCoreDataMiddleware
9-
10-
- (NSArray *)managedObjectContext:(NSManagedObjectContext *)context
11-
executeFetchRequest:(NSFetchRequest *)request
12-
error:(NSError **)error
13-
originalImp:(NSArray *_Nullable(NS_NOESCAPE ^)(NSFetchRequest *, NSError **))
14-
original NS_REFINED_FOR_SWIFT;
15-
16-
- (BOOL)managedObjectContext:(NSManagedObjectContext *)context
17-
save:(NSError **)error
18-
originalImp:(BOOL(NS_NOESCAPE ^)(NSError **))original
19-
NS_SWIFT_NAME(saveManagedObjectContext(_:originalImp:));
20-
21-
@end
8+
@class SentryCoreDataTracker;
229

2310
@interface SentryCoreDataSwizzling : NSObject
2411
SENTRY_NO_INIT
2512

2613
@property (class, readonly, nonatomic) SentryCoreDataSwizzling *sharedInstance;
2714

28-
@property (nonatomic, readonly, nullable) id<SentryCoreDataMiddleware> middleware;
15+
@property (nonatomic, readonly, nullable) SentryCoreDataTracker *coreDataTracker;
2916

30-
- (void)startWithMiddleware:(id<SentryCoreDataMiddleware>)middleware;
17+
- (void)startWithTracker:(SentryCoreDataTracker *)coreDataTracker;
3118

3219
- (void)stop;
3320

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
2-
#import "SentryCoreDataSwizzling.h"
3-
#import <Foundation/Foundation.h>
1+
#import "SentryDefines.h"
2+
#import <CoreData/CoreData.h>
43

54
NS_ASSUME_NONNULL_BEGIN
65

@@ -9,12 +8,22 @@ static NSString *const SENTRY_COREDATA_SAVE_OPERATION = @"db.sql.transaction";
98

109
@class SentryThreadInspector, SentryNSProcessInfoWrapper;
1110

12-
@interface SentryCoreDataTracker : NSObject <SentryCoreDataMiddleware>
11+
@interface SentryCoreDataTracker : NSObject
1312
SENTRY_NO_INIT
1413

1514
- (instancetype)initWithThreadInspector:(SentryThreadInspector *)threadInspector
1615
processInfoWrapper:(SentryNSProcessInfoWrapper *)processInfoWrapper;
1716

17+
- (NSArray *)managedObjectContext:(NSManagedObjectContext *)context
18+
executeFetchRequest:(NSFetchRequest *)request
19+
error:(NSError **)error
20+
originalImp:(NSArray *_Nullable(NS_NOESCAPE ^)(NSFetchRequest *, NSError **))
21+
original NS_REFINED_FOR_SWIFT;
22+
23+
- (BOOL)managedObjectContext:(NSManagedObjectContext *)context
24+
save:(NSError **)error
25+
originalImp:(BOOL(NS_NOESCAPE ^)(NSError **))original;
26+
1827
@end
1928

2029
NS_ASSUME_NONNULL_END

Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataMiddleware+Extension.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#import "SentryCoreDataTracker.h"
2+
#import <Foundation/Foundation.h>
3+
#import <XCTest/XCTest.h>
4+
5+
NS_ASSUME_NONNULL_BEGIN
6+
7+
@interface
8+
SentryCoreDataTracker (Test)
9+
10+
- (BOOL)saveManagedObjectContextWithNilError:(NSManagedObjectContext *)context
11+
originalImp:(BOOL(NS_NOESCAPE ^)(NSError **))original;
12+
13+
@end
14+
15+
NS_ASSUME_NONNULL_END
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#import "SentryCoreDataTracker+Test.h"
2+
#import <Foundation/Foundation.h>
3+
4+
@implementation
5+
SentryCoreDataTracker (Test)
6+
7+
- (BOOL)saveManagedObjectContextWithNilError:(NSManagedObjectContext *)context
8+
originalImp:(BOOL(NS_NOESCAPE ^)(NSError **))original
9+
{
10+
return [self managedObjectContext:context save:nil originalImp:original];
11+
}
12+
13+
@end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
extension SentryCoreDataTracker {
4+
5+
func fetchManagedObjectContext<T>(_ context: NSManagedObjectContext, request: NSFetchRequest<T>, isErrorNil: Bool = false, originalImp: (NSFetchRequest<T>, NSErrorPointer) -> [T]?) throws -> [Any] {
6+
7+
var error: NSError?
8+
var result: [Any]
9+
10+
if isErrorNil {
11+
result = __managedObjectContext(context, execute: request as! NSFetchRequest<NSFetchRequestResult>, error: nil) { fetchRequest, errorOut in
12+
return originalImp(fetchRequest as! NSFetchRequest<T>, errorOut)
13+
}
14+
15+
} else {
16+
result = __managedObjectContext(context, execute: request as! NSFetchRequest<NSFetchRequestResult>, error: &error) { fetchRequest, errorOut in
17+
return originalImp(fetchRequest as! NSFetchRequest<T>, errorOut)
18+
}
19+
}
20+
21+
if let er = error {
22+
throw er
23+
}
24+
25+
return result
26+
}
27+
28+
}

0 commit comments

Comments
 (0)