Skip to content

pigeon objc static analysis problem with ternary conditional nil checks #63966

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

Open
gaaclarke opened this issue Aug 17, 2020 · 10 comments
Open
Labels
p: pigeon related to pigeon messaging codegen tool P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team

Comments

@gaaclarke
Copy link
Member

gaaclarke commented Aug 17, 2020

 - WARN  | [iOS] xcodebuild:  /private/var/folders/3y/l0z1x3693dl_8n0qybp4dqwh0000gn/T/cirrus-ci-build/packages/video_player/video_player/ios/Classes/messages.m:59:51: warning: Converting a pointer value of type 'NSNumber * _Nullable' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue
    - WARN  | [iOS] xcodebuild:  /private/var/folders/3y/l0z1x3693dl_8n0qybp4dqwh0000gn/T/cirrus-ci-build/packages/video_player/video_player/ios/Classes/messages.m:111:37: warning: Converting a pointer value of type 'NSNumber * _Nullable' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue
    - WARN  | [iOS] xcodebuild:  /private/var/folders/3y/l0z1x3693dl_8n0qybp4dqwh0000gn/T/cirrus-ci-build/packages/video_player/video_player/ios/Classes/messages.m:112:37: warning: Converting a pointer value of type 'NSNumber * _Nullable' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue
    - WARN  | [iOS] xcodebuild:  /private/var/folders/3y/l0z1x3693dl_8n0qybp4dqwh0000gn/T/cirrus-ci-build/packages/video_player/video_player/ios/Classes/messages.m:132:37: warning: Converting a pointer value of type 'NSNumber * _Nullable' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue
    - WARN  | [iOS] xcodebuild:  /private/var/folders/3y/l0z1x3693dl_8n0qybp4dqwh0000gn/T/cirrus-ci-build/packages/video_player/video_player/ios/Classes/messages.m:133:37: warning: Converting a pointer value of type 'NSNumber * _Nullable' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue
    - WARN  | [iOS] xcodebuild:  /private/var/folders/3y/l0z1x3693dl_8n0qybp4dqwh0000gn/T/cirrus-ci-build/packages/video_player/video_player/ios/Classes/messages.m:152:37: warning: Converting a pointer value of type 'NSNumber * _Nullable' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue
    - WARN  | [iOS] xcodebuild:  /private/var/folders/3y/l0z1x3693dl_8n0qybp4dqwh0000gn/T/cirrus-ci-build/packages/video_player/video_player/ios/Classes/messages.m:153:37: warning: Converting a pointer value of type 'NSNumber * _Nullable' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue
    - WARN  | [iOS] xcodebuild:  /private/var/folders/3y/l0z1x3693dl_8n0qybp4dqwh0000gn/T/cirrus-ci-build/packages/video_player/video_player/ios/Classes/messages.m:169:37: warning: Converting a pointer value of type 'NSNumber * _Nullable' to a primitive boolean value; instead, either compare the pointer to nil or call -boolValue

From @jmagman:

Instead of (self.textureId ? self.textureId : [NSNull null]) you need to say (self.textureId != nil ? self.textureId : [NSNull null])

Spawned from:
flutter/plugins#2922

@gaaclarke gaaclarke added the p: pigeon related to pigeon messaging codegen tool label Aug 17, 2020
@jmagman
Copy link
Member

jmagman commented Aug 17, 2020

Would be nice to run the static analyzer in CI for https://github.com/flutter/packages/tree/cf96cba3c50a410e9b450d0a24ea9d7e1c020c09/packages/pigeon/platform_tests/ios_unit_tests/ios to catch future issues.

@jmagman jmagman added package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically P2 Important issues not at the top of the work list labels Aug 17, 2020
@gaaclarke
Copy link
Member Author

@jmagman I was looking into it. It's not clear what linter cocoapods uses. I was playing around with oclint but might take a bit of work to setup since it doesn't seem to support commandline compiler arguments. Otherwise I'll have to generate a fake podspec to run "pod lib lint" =T

@jmagman
Copy link
Member

jmagman commented Aug 17, 2020

It's just the clang static analyzer with default warnings from a newly generated Xcode project. You don't need to use CocoaPods to see it.

@jmagman
Copy link
Member

jmagman commented Aug 17, 2020

If the unit tests were running, I think you could add analyze to https://github.com/flutter/packages/blob/c9053f1fea56efd5a7d77bd5063994a573743ab2/packages/pigeon/run_tests.sh#L92-L97 combined with GCC_TREAT_WARNINGS_AS_ERRORS=YES.

@gaaclarke
Copy link
Member Author

gaaclarke commented Aug 19, 2020

After the ios_unit_tests have been reenabled i tried:

cd platform_tests/ios_unit_tests/ios
xcodebuild analyze DEVELOPMENT_TEAM=$DEVELOPMENT_TEAM GCC_TREAT_WARNINGS_AS_ERRORS=YES

Despite "messages.m" containing:

- (NSDictionary *)toMap {
  return [NSDictionary
      dictionaryWithObjectsAndKeys:(self.query ? self.query : [NSNull null]), @"query",
                                   (self.anInt ? self.anInt : [NSNull null]), @"anInt",
                                   (self.aBool ? self.aBool : [NSNull null]), @"aBool", nil];
}

The analyzer didn't trigger any messages.

I tried to trigger problems in messages.m, like adding unused ivars, but couldn't get the analyzer to complain.

edit: Running the analyzer inside of xcode didn't flag the issue either.

@gaaclarke
Copy link
Member Author

gaaclarke commented Aug 19, 2020

I was finally able to get the analyzer to trigger a problem by turning on CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES to prove it works. It seems like the reported analyzer problem may be dependent on analyzer settings (of which I saw none that seemed applicable in the project settings) or a different version of xcode.

@jmagman
Copy link
Member

jmagman commented Aug 19, 2020

It should be coming from CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION, which is on in a default Flutter (and Xcode) project.
I was able to reproduce the analyzer warning from flutter/plugins#2922 by removing the nil conditional check in -[FLTTextureMessage toMap] and running the analyzer Product > Analyze in Xcode (it's not a recently added clang warning, you're on a reasonably new version of Xcode so you should see it?).

- (NSDictionary *)toMap {
  return [NSDictionary
      dictionaryWithObjectsAndKeys:(self.textureId ? self.textureId : [NSNull null]),
                                   @"textureId", nil];
}

Screen Shot 2020-08-19 at 4 01 37 PM

@gaaclarke
Copy link
Member Author

Screen Shot 2020-08-19 at 4 24 34 PM

I have CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION set to YES but still doesn't trigger a problem in 11.5.1. My guess is that clang redefined that warning. I also suspect my xcode version is newer than the one being used by CI (I can't find the link to the failure since there was a force push to verify).

I can still turn on the analyzer and we should be good as long as the analyzers never conflict and we accept there may be certain issues it doesn't catch that other variants will flag like this one.

@jmagman
Copy link
Member

jmagman commented Aug 19, 2020

@gaaclarke The CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER is different. CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION is:

Warn when a number object, such as an instance of `NSNumber`, `CFNumberRef`, `OSNumber`, or `OSBoolean` is compared or converted to a primitive value instead of another object.

Did you try it on the video_player? That's where it actually showed up in the wild and where I reproduced it. I will check out the platform test and see why it's different.

@gaaclarke
Copy link
Member Author

@jmagman Right, I know it's different. I was just showing that the analyzer flagged that problem, but not the one for CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION.

@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team labels Jul 8, 2023
@jmagman jmagman added P3 Issues that are less important to the Flutter project and removed P2 Important issues not at the top of the work list labels Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: pigeon related to pigeon messaging codegen tool P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team
Projects
None yet
Development

No branches or pull requests

5 participants