From b2e7c507049c765f3d10269bd74e1b396a954e56 Mon Sep 17 00:00:00 2001 From: Niels Grewe Date: Mon, 7 Sep 2015 12:01:39 +0200 Subject: [PATCH 1/2] Fix throwing nil exceptions. --- Test/CMakeLists.txt | 1 + Test/NilException.m | 17 +++++++++++++++++ eh_personality.c | 5 ++++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Test/NilException.m diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt index 8e253ae..1e87e6c 100644 --- a/Test/CMakeLists.txt +++ b/Test/CMakeLists.txt @@ -26,6 +26,7 @@ set(TESTS WeakReferences_arc.m objc_msgSend.m msgInterpose.m + NilException.m ) # Function for adding a test. This takes the name of the test and the list of diff --git a/Test/NilException.m b/Test/NilException.m new file mode 100644 index 0000000..cd4fa58 --- /dev/null +++ b/Test/NilException.m @@ -0,0 +1,17 @@ +#include "Test.h" + +int main(void) +{ + BOOL caught_exception = NO; + @try + { + @throw(nil); + } + @catch (id x) + { + assert(nil == x); + caught_exception = YES; + } + assert(caught_exception == YES); + return 0; +} diff --git a/eh_personality.c b/eh_personality.c index 5308862..e5abf5a 100644 --- a/eh_personality.c +++ b/eh_personality.c @@ -358,7 +358,10 @@ static inline _Unwind_Reason_Code internal_objc_personality(int version, else if (!foreignException) { ex = objc_exception_from_header(exceptionObject); - thrown_class = classForObject(ex->object); + if (ex->object != nil) + { + thrown_class = classForObject(ex->object); + } } else if (_objc_class_for_boxing_foreign_exception) { From e121b34a9fe208e07b7ff38485846b6829c95db9 Mon Sep 17 00:00:00 2001 From: Niels Grewe Date: Mon, 7 Sep 2015 13:03:00 +0200 Subject: [PATCH 2/2] Check other exception handlers that should/shouldn't catch nil. --- Test/NilException.m | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Test/NilException.m b/Test/NilException.m index cd4fa58..de85663 100644 --- a/Test/NilException.m +++ b/Test/NilException.m @@ -1,5 +1,23 @@ #include "Test.h" + +#ifdef __has_attribute +#if __has_attribute(objc_root_class) +__attribute__((objc_root_class)) +#endif +#endif +@interface NSObject +{ + Class isa; +} +@end + +@implementation NSObject ++ (id)new +{ + return class_createInstance(self, 0); +} +@end int main(void) { BOOL caught_exception = NO; @@ -7,11 +25,25 @@ int main(void) { @throw(nil); } + @catch (NSObject* o) + { + assert(0); + } @catch (id x) { assert(nil == x); caught_exception = YES; } assert(caught_exception == YES); + caught_exception = NO; + @try + { + @throw(nil); + } + @catch (...) + { + caught_exception = YES; + } + assert(caught_exception == YES); return 0; }