Skip to content

Commit 005bb4c

Browse files
fix: Crash in CppException Monitor (#2639)
Only call the previous terminate_handler if it's not NULL to avoid crashing. Fixes GH-1533 Co-authored-by: Andrew McKnight <andrew.mcknight@sentry.io>
1 parent 156e771 commit 005bb4c

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Support uint64 in crash reports (#2631)
1313
- Always fetch view hierarchy on the main thread (#2629)
1414
- Carthage Xcode 14 compatibility issue (#2636)
15+
- Crash in CppException Monitor (#2639)
1516

1617
## 8.0.0
1718

Sentry.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@
584584
7BF9EF882722D13000B5BBEF /* SentryTestObjCRuntimeWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BF9EF872722D13000B5BBEF /* SentryTestObjCRuntimeWrapper.m */; };
585585
7BF9EF8B2722D58700B5BBEF /* SentryInitializeForGettingSubclassesNotCalled.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BF9EF8A2722D58700B5BBEF /* SentryInitializeForGettingSubclassesNotCalled.m */; };
586586
7BFA69F627E0840400233199 /* SentryANRTrackingIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BFA69F527E0840400233199 /* SentryANRTrackingIntegrationTests.swift */; };
587+
7BFAA6E7297AA16A00E7E02E /* SentryCrashMonitor_CppException_Tests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7BFAA6E6297AA16A00E7E02E /* SentryCrashMonitor_CppException_Tests.mm */; };
587588
7BFC169B2524995700FF6266 /* SentryMessage.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BFC169A2524995700FF6266 /* SentryMessage.h */; settings = {ATTRIBUTES = (Public, ); }; };
588589
7BFC16A125249A9D00FF6266 /* SentryMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BFC16A025249A9D00FF6266 /* SentryMessage.m */; };
589590
7BFC16AD2524BCE700FF6266 /* SentryMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BFC16AC2524BCE700FF6266 /* SentryMessageTests.swift */; };
@@ -1408,6 +1409,7 @@
14081409
7BF9EF892722D57100B5BBEF /* SentryInitializeForGettingSubclassesNotCalled.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryInitializeForGettingSubclassesNotCalled.h; sourceTree = "<group>"; };
14091410
7BF9EF8A2722D58700B5BBEF /* SentryInitializeForGettingSubclassesNotCalled.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryInitializeForGettingSubclassesNotCalled.m; sourceTree = "<group>"; };
14101411
7BFA69F527E0840400233199 /* SentryANRTrackingIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryANRTrackingIntegrationTests.swift; sourceTree = "<group>"; };
1412+
7BFAA6E6297AA16A00E7E02E /* SentryCrashMonitor_CppException_Tests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryCrashMonitor_CppException_Tests.mm; sourceTree = "<group>"; };
14111413
7BFC169A2524995700FF6266 /* SentryMessage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryMessage.h; path = Public/SentryMessage.h; sourceTree = "<group>"; };
14121414
7BFC16A025249A9D00FF6266 /* SentryMessage.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryMessage.m; sourceTree = "<group>"; };
14131415
7BFC16AC2524BCE700FF6266 /* SentryMessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryMessageTests.swift; sourceTree = "<group>"; };
@@ -2360,6 +2362,7 @@
23602362
63FE71FB20DA66EB00CDBAE8 /* SentryCrashMonitor_NSException_Tests.m */,
23612363
63FE71E820DA66E900CDBAE8 /* SentryCrashMonitor_Signal_Tests.m */,
23622364
63FE71E120DA66E800CDBAE8 /* SentryCrashMonitor_Tests.m */,
2365+
7BFAA6E6297AA16A00E7E02E /* SentryCrashMonitor_CppException_Tests.mm */,
23632366
63FE71F720DA66EB00CDBAE8 /* SentryCrashObjC_Tests.m */,
23642367
63FE71DB20DA66E700CDBAE8 /* SentryCrashReportFilter_Tests.m */,
23652368
63FE71EB20DA66E900CDBAE8 /* SentryCrashReportFixer_Tests.m */,
@@ -3903,6 +3906,7 @@
39033906
7B5B94332657A816002E474B /* SentryAppStartTrackingIntegrationTests.swift in Sources */,
39043907
0A5370A128A3EC2400B2DCDE /* SentryViewHierarchyTests.swift in Sources */,
39053908
D8FFE50C2703DBB400607131 /* SwizzlingCallTests.swift in Sources */,
3909+
7BFAA6E7297AA16A00E7E02E /* SentryCrashMonitor_CppException_Tests.mm in Sources */,
39063910
D8B76B0828081461000A58C4 /* TestSentryScreenShot.swift in Sources */,
39073911
A8AFFCD22907DA7600967CD7 /* SentryHttpStatusCodeRangeTests.swift in Sources */,
39083912
7BE2C7F8257000A4003B66C7 /* SentryTestIntegration.m in Sources */,

Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_CPPException.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ __cxa_throw(void *thrown_exception, std::type_info *tinfo, void (*dest)(void *))
9494
}
9595
}
9696

97+
void
98+
sentrycrashcm_cppexception_callOriginalTerminationHandler(void)
99+
{
100+
// Can be NULL as the return value of set_terminate can be a NULL pointer; see:
101+
// https://en.cppreference.com/w/cpp/error/set_terminate
102+
if (g_originalTerminateHandler != NULL) {
103+
SentryCrashLOG_DEBUG("Calling original terminate handler.");
104+
g_originalTerminateHandler();
105+
}
106+
}
107+
97108
static void
98109
CPPExceptionTerminate(void)
99110
{
@@ -167,8 +178,7 @@ CPPExceptionTerminate(void)
167178
}
168179
sentrycrashmc_resumeEnvironment(threads, numThreads);
169180

170-
SentryCrashLOG_DEBUG("Calling original terminate handler.");
171-
g_originalTerminateHandler();
181+
sentrycrashcm_cppexception_callOriginalTerminationHandler();
172182
}
173183

174184
// ============================================================================
@@ -197,6 +207,7 @@ setEnabled(bool isEnabled)
197207
g_originalTerminateHandler = std::set_terminate(CPPExceptionTerminate);
198208
} else {
199209
std::set_terminate(g_originalTerminateHandler);
210+
g_originalTerminateHandler = NULL;
200211
}
201212
g_captureNextStackTrace = isEnabled;
202213
}

Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_CPPException.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ extern "C" {
3535
*/
3636
SentryCrashMonitorAPI *sentrycrashcm_cppexception_getAPI(void);
3737

38+
/** For testing.
39+
*/
40+
void sentrycrashcm_cppexception_callOriginalTerminationHandler(void);
41+
3842
#ifdef __cplusplus
3943
}
4044
#endif
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#import "SentryCrashMonitor_CPPException.h"
2+
#import <XCTest/XCTest.h>
3+
4+
#include <iostream>
5+
#include <stdexcept>
6+
7+
@interface SentryCrashMonitor_CppException_Tests : XCTestCase
8+
9+
@end
10+
11+
@implementation SentryCrashMonitor_CppException_Tests
12+
13+
bool terminateCalled = false;
14+
15+
void
16+
testTerminationHandler()
17+
{
18+
terminateCalled = true;
19+
}
20+
21+
- (void)setUp
22+
{
23+
[super setUp];
24+
terminateCalled = false;
25+
}
26+
27+
- (void)testCallTerminationHandler_NotEnabled
28+
{
29+
30+
std::set_terminate(&testTerminationHandler);
31+
32+
sentrycrashcm_cppexception_callOriginalTerminationHandler();
33+
34+
XCTAssertFalse(terminateCalled);
35+
}
36+
37+
- (void)testCallTerminationHandler_Enabled
38+
{
39+
40+
std::set_terminate(&testTerminationHandler);
41+
42+
SentryCrashMonitorAPI *api = sentrycrashcm_cppexception_getAPI();
43+
api->setEnabled(true);
44+
45+
sentrycrashcm_cppexception_callOriginalTerminationHandler();
46+
47+
XCTAssertTrue(terminateCalled);
48+
49+
api->setEnabled(false);
50+
}
51+
52+
@end

0 commit comments

Comments
 (0)