diff --git a/Ports/iOSPort/nativeSources/CN1OidcBrowser.m b/Ports/iOSPort/nativeSources/CN1OidcBrowser.m index dc0f06c8ea5..31e46a0f6c2 100644 --- a/Ports/iOSPort/nativeSources/CN1OidcBrowser.m +++ b/Ports/iOSPort/nativeSources/CN1OidcBrowser.m @@ -94,11 +94,25 @@ - (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuth #endif // !TARGET_OS_WATCH // Single static slot to keep the session strongly referenced for the -// duration of a flow (ARC would otherwise deallocate it the moment the -// dispatch_async block returned). +// duration of a flow. The port compiles without ARC (CLANG_ENABLE_OBJC_ARC = +// NO), so there these hold the +1 reference from -alloc, and +// cn1OidcClearSession() gives it up. Under ARC (the iOS native-source lint +// compiles with -fobjc-arc) the slots are strong and assigning nil is enough. static id g_cn1OidcCurrentSession = nil; static id g_cn1OidcCurrentContext = nil; +// Drops the flow's session and context. Without ARC they are autoreleased +// rather than released: this runs inside the session's own completion +// handler, and the session must outlive the handler's return. +static void cn1OidcClearSession(void) { +#if !__has_feature(objc_arc) + [g_cn1OidcCurrentSession autorelease]; + [g_cn1OidcCurrentContext autorelease]; +#endif + g_cn1OidcCurrentSession = nil; + g_cn1OidcCurrentContext = nil; +} + JAVA_BOOLEAN com_codename1_impl_ios_IOSNative_oidcSystemBrowserSupported___R_boolean( CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT me) { if (@available(iOS 12.0, watchOS 6.2, *)) { @@ -133,13 +147,21 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ [[ASWebAuthenticationSession alloc] initWithURL:url callbackURLScheme:redirectScheme completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) { + // Retained: absoluteString is autoreleased into the main + // thread's pool, which drains before the waiting thread reads + // it. Without the copy that read was a use-after-free, which + // ParparVM's signal handler surfaced as a NullPointerException + // (OidcException transport_error, "null"). if (callbackURL) { - result = [callbackURL absoluteString]; + result = [[callbackURL absoluteString] copy]; } else if (error) { +#if __has_feature(objc_arc) failure = error; +#else + failure = [error retain]; +#endif } - g_cn1OidcCurrentSession = nil; - g_cn1OidcCurrentContext = nil; + cn1OidcClearSession(); dispatch_semaphore_signal(sem); }]; @@ -157,8 +179,10 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ failure = [NSError errorWithDomain:@"com.codename1.io.oidc" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"ASWebAuthenticationSession refused to start"}]; - g_cn1OidcCurrentSession = nil; - g_cn1OidcCurrentContext = nil; +#if !__has_feature(objc_arc) + [failure retain]; +#endif + cn1OidcClearSession(); dispatch_semaphore_signal(sem); } }); @@ -167,10 +191,18 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_ // sheet is ever held open indefinitely. dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3600 * NSEC_PER_SEC))); - if (failure != nil || result == nil) { - return JAVA_NULL; + // Without ARC both were retained on the main thread (above); release them + // here, once the flow is over. dispatch_semaphore_signal/wait order the + // writes before this. + JAVA_OBJECT answer = JAVA_NULL; + if (failure == nil && result != nil) { + answer = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG result); } - return fromNSString(CN1_THREAD_GET_STATE_PASS_ARG result); +#if !__has_feature(objc_arc) + [result release]; + [failure release]; +#endif + return answer; } #else