Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pigeon] Crash in swift: Could not cast value of type 'NSNull' to 'NSArray' #129283

Closed
2 tasks done
dnys1 opened this issue Jun 21, 2023 · 5 comments · Fixed by flutter/packages#4289
Closed
2 tasks done
Assignees
Labels
c: fatal crash Crashes that terminate the process c: regression It was better in the past than it is now found in release: 3.10 Found to occur in 3.10 found in release: 3.12 Found to occur in 3.12 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: pigeon related to pigeon messaging codegen tool P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically

Comments

@dnys1
Copy link
Contributor

dnys1 commented Jun 21, 2023

Is there an existing issue for this?

Steps to reproduce

  1. Using pigeon 10.0.1
  2. Run swift generator for schema:
@FlutterApi()
abstract class NativeAuthPlugin {
  @async
  NativeAuthSession fetchAuthSession();
}

class NativeAuthSession {
  late bool isSignedIn;

  String? userSub;
  NativeUserPoolTokens? userPoolTokens;

  String? identityId;
  NativeAWSCredentials? awsCredentials;
}

class NativeUserPoolTokens {
  late String accessToken;
  late String refreshToken;
  late String idToken;
}

class NativeAWSCredentials {
  late String accessKeyId;
  late String secretAccessKey;
  String? sessionToken;
  String? expirationIso8601Utc;
}
  1. Send null for userPoolTokens (code from unit test)
    lazy var codec = NativeAuthPluginCodec.shared

    func send(
        onChannel channel: String,
        message: Data?,
        binaryReply callback: FlutterBinaryReply? = nil
    ) {
        let authSession = NativeAuthSession(
            isSignedIn: isSignedIn,
            userSub: nil,
            userPoolTokens: nil,
            identityId: nil,
            awsCredentials: nil
        )
        let encodedAuthSession = codec.encode(authSession)
        callback?(encodedAuthSession)
    }

Expected results

Passing null for userPoolTokens should work. This code worked with pigeon v9 output.

Actual results

A crash is observed running in Xcode.

Code sample

Included above.

Screenshots or Video

Screenshot 2023-06-21 at 8 08 47 AM

Logs

Logs
Test Case '-[unit_tests.NativeAuthPluginTests testFetchAuthSessionSignedIn]' started.
Could not cast value of type 'NSNull' (0x107eaa978) to 'NSArray' (0x107eaaa20).
2023-06-21 08:02:22.225474-0700 Runner[54059:310281] Could not cast value of type 'NSNull' (0x107eaa978) to 'NSArray' (0x107eaaa20).

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.5, on macOS 13.4 22F66 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.1)
[✓] VS Code (version 1.79.2)
[✓] Connected device (2 available)
[✓] Network resources

• No issues found!
@huycozy huycozy added the in triage Presently being triaged by the triage team label Jun 22, 2023
@huycozy
Copy link
Member

huycozy commented Jun 22, 2023

Thanks for filing the issue, @dnys1
In order to address the issue properly, can you please provide a completed and minimal reproducible code sample so that I can verify this easier?

@huycozy huycozy added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jun 22, 2023
@dnys1
Copy link
Contributor Author

dnys1 commented Jun 23, 2023

Sure, here ya go https://github.com/dnys1/pigeon_repro

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jun 23, 2023
@huycozy
Copy link
Member

huycozy commented Jun 23, 2023

Thanks for the update. Reproduced the crash on iPhone 14 Pro, iOS 16.4. As you confirmed it worked in version 9.x, maybe it's related to breaking change in version 10.0.0.

Full crash stack trace: crash_log.txt

flutter doctor -v (stable and master)
[✓] Flutter (Channel stable, 3.10.5, on macOS 13.0.1 22A400 darwin-x64, locale en-VN)
    • Flutter version 3.10.5 on channel stable at /Users/huynq/Documents/GitHub/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (29 hours ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-33, build-tools 32.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.79.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.66.0

[✓] Connected device (5 available)
    • Pixel 3a (mobile)      • 964AY0WL20                               • android-arm64  • Android 12 (API 32)
    • iPhone (mobile)        • d9a94afe2b649fef56ba0bfeb052f0f2a7dae95e • ios            • iOS 15.7.2 19H218
    • iPhone 14 Pro (mobile) • 98082851-A0C4-4E39-A310-21C48B10A364     • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator)
    • macOS (desktop)        • macos                                    • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
    • Chrome (web)           • chrome                                   • web-javascript • Google Chrome 114.0.5735.133

[✓] Network resources
    • All expected network resources are available.

• No issues found!
[!] Flutter (Channel master, 3.12.0-9.0.pre.25, on macOS 13.0.1 22A400 darwin-x64, locale en-VN)
    • Flutter version 3.12.0-9.0.pre.25 on channel master at /Users/huynq/Documents/GitHub/flutter_master
    ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 34b42acf1b (54 minutes ago), 2023-06-22 19:09:12 -0700
    • Engine revision aca26b2afc
    • Dart version 3.1.0 (build 3.1.0-239.0.dev)
    • DevTools version 2.24.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-33, build-tools 32.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.79.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.66.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 114.0.5735.133

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

@huycozy huycozy added platform-ios iOS applications specifically package flutter/packages repository. See also p: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on p: pigeon related to pigeon messaging codegen tool c: fatal crash Crashes that terminate the process found in release: 3.10 Found to occur in 3.10 found in release: 3.12 Found to occur in 3.12 c: regression It was better in the past than it is now and removed in triage Presently being triaged by the triage team labels Jun 23, 2023
@stuartmorgan stuartmorgan added the P1 High-priority issues at the top of the work list label Jun 23, 2023
@stuartmorgan
Copy link
Contributor

/cc @tarrinneal @hellohuanlin, looks like fallout from the casting changes.

@hellohuanlin hellohuanlin self-assigned this Jun 23, 2023
auto-submit bot pushed a commit to flutter/packages that referenced this issue Jul 6, 2023
Fixes a crash introduced in #3658: 

```
static func fromList(_ list: [Any?]) -> NativeAuthSession? {
  if let userPoolTokensList = list[2] as! [Any?]? {}
}
```
where `list[2]` is `NSNull`, which is not an Optional, hence can't be casted to `[Any?]?`. 

Recall that `nilOrValue` helper is created for this purpose. So the fix is simply: 

```
static func fromList(_ list: [Any?]) -> NativeAuthSession? {
  if let userPoolTokensList: [Any?] = nilOrValue(list[2]) {} // <- HERE
}
```

## Why didn't we catch this regression

Missing unit tests - we did not test `NSNull` fields to ensure `nilOrValue` works for them.  

## Why did it work in previous version? 

It's surprising that this worked fine before! The original code is: 
```
static func fromList(_ list: [Any]) -> NativeAuthSession? {
  if let userPoolTokensList = list[2] as! [Any]? {}
}
```
From [my previous PR](flutter/flutter#129283), we know that list[2] is an implicit optional (that contains a NSNull value). I think Swift made an exception here when casting implicit optional NSNull (either intentionally or unintentionally). 

*List which issues are fixed by this PR. You must list at least one issue.*

Fixes flutter/flutter#129283

*If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
auto-submit bot pushed a commit to flutter/packages that referenced this issue Jul 13, 2023
Updates `pigeon` in all of our Swift plugins, to ensure that they have the fix for flutter/flutter#129283

Fixes flutter/flutter#129536
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: fatal crash Crashes that terminate the process c: regression It was better in the past than it is now found in release: 3.10 Found to occur in 3.10 found in release: 3.12 Found to occur in 3.12 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: pigeon related to pigeon messaging codegen tool P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants