Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Fix miscellaneous static analyzer warnings #7670

Merged
merged 4 commits into from
Jan 11, 2017
Merged

Conversation

1ec5
Copy link
Contributor

@1ec5 1ec5 commented Jan 11, 2017

This PR fixes the following static analyzer warnings, which were introduced in #6709, #7377, #6559, and #7300:

/path/to/mapbox-gl-native/platform/darwin/src/MGLNetworkConfiguration.m:16:9: Null is returned from a method that is expected to return a non-null value
/path/to/mapbox-gl-native/platform/darwin/src/MGLRasterSource.mm:63:23: Function call argument is an uninitialized value (within a call to 'make_unique')
/path/to/mapbox-gl-native/platform/darwin/src/MGLShape.mm:98:10: The left expression of the compound assignment is an uninitialized value. The computed value will also be garbage
/path/to/mapbox-gl-native/platform/ios/src/MGLAnnotationImage.m:60:54: Null passed to a callee that requires a non-null 1st parameter
/path/to/mapbox-gl-native/platform/ios/src/MGLAnnotationImage.m:65:10: The left expression of the compound assignment is an uninitialized value. The computed value will also be garbage
/path/to/mapbox-gl-native/platform/macos/src/MGLAnnotationImage.m:61:10: The left expression of the compound assignment is an uninitialized value. The computed value will also be garbage
/path/to/mapbox-gl-native/platform/macos/src/NSImage+MGLAdditions.mm:13:5: Returning 'self' while it is not set to the result of '[(super or self) init...]'

Working towards #7668.

/cc @frederoni @incanus

1ec5 added 3 commits January 10, 2017 21:58
Fixed static analyzer warnings in MGLNetworkConfiguration, MGLRasterSource, and MGLShape.
Fixed static analyzer warnings in MGLAnnotationImage.
Fixed static analyzer warnings in MGLAnnotationImage and NSImage(MGLAdditions).
@1ec5 1ec5 added annotations Annotations on iOS and macOS or markers on Android build iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling labels Jan 11, 2017
@1ec5 1ec5 added this to the ios-v3.4.0 milestone Jan 11, 2017
@1ec5 1ec5 self-assigned this Jan 11, 2017
@1ec5 1ec5 requested a review from frederoni January 11, 2017 06:07
@mention-bot
Copy link

@1ec5, thanks for your PR! By analyzing this pull request, we identified @incanus and @boundsj to be potential reviewers.

@@ -12,9 +11,6 @@ + (void)load {
}

+ (instancetype)sharedManager {
if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
Copy link
Contributor Author

@1ec5 1ec5 Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+[MGLAccountManager sharedManager] bails in IB because MGLAccountManager spins up lots of other machinery, including MGLMapboxEvents. MGLNetworkConfiguration is intentionally isolated from anything else, so initializing the singleton within IB is no big deal.

@@ -57,15 +57,11 @@ - (BOOL)isEqual:(id)other {

return ((!_reuseIdentifier && !otherAnnotationImage.reuseIdentifier) || [_reuseIdentifier isEqualToString:otherAnnotationImage.reuseIdentifier])
&& _enabled == otherAnnotationImage.enabled
&& ((!_image && !otherAnnotationImage.image) || [UIImagePNGRepresentation(_image) isEqualToData:UIImagePNGRepresentation(otherAnnotationImage.image)]);
Copy link
Contributor Author

@1ec5 1ec5 Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frederoni, any reason why comparing two UIImages for object equality isn’t good enough? (There’s a similar comparison of NSImages’ TIFF representations on the macOS side.)

Copy link
Contributor Author

@1ec5 1ec5 Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that testAnnotationImage() is failing as a result of my change. It’s true that drawing the image to a new context could make the two images unequal, but there are a number of reasons that would be the case anyways (such as different color profiles). In general, I don’t think a class’s -isEqual: implementation should be more thorough than the -isEqual: implementations of its ivars.

Edit: I misread the test. Looks like it’s doing something pretty ordinary.

@@ -51,7 +51,7 @@ - (instancetype)initWithIdentifier:(NSString *)identifier tileURLTemplates:(NS_A
if (self = [super initWithIdentifier:identifier tileURLTemplates:tileURLTemplates options:options]) {
mbgl::Tileset tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, options);

uint16_t tileSize;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had somehow forgotten that primitive-typed stack variables are uninitialized in Objective-C just as they are in C. ARC nils out only object pointers on the stack.

NSUInteger hash;
hash += _title.hash;
hash += _subtitle.hash;
NSUInteger hash = _title.hash + _subtitle.hash;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a safe way to compute the hash?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A XOR over the values could improve it but I'm ok with it because this is an abstract class and can't be used directly. All concrete subclasses include the hash of the GeoJSON dictionary.

Copy link
Contributor Author

@1ec5 1ec5 Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fairly conventional implementation, as far as I know. The only hard requirement for a -hash implementation is that two equal objects (as in -isEqual:) must have the same hash. But feel free to improve on the various -hash implementations in this SDK in a separate PR.

@1ec5 1ec5 merged commit 581fb38 into release-ios-v3.4.0 Jan 11, 2017
@1ec5 1ec5 deleted the 1ec5-analyze-7668 branch January 11, 2017 11:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
annotations Annotations on iOS and macOS or markers on Android build iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants