From 9bd59bf041b0d0ca400f586fdbfed53d06258613 Mon Sep 17 00:00:00 2001 From: Julian Rex Date: Tue, 30 Apr 2019 17:08:37 -0400 Subject: [ios, macos] Fixes for static analyzer warnings (#14453) --- platform/darwin/src/MGLStyleValue_Private.h | 2 +- platform/darwin/src/NSExpression+MGLAdditions.h | 2 +- platform/darwin/src/NSExpression+MGLAdditions.mm | 17 +++++++++++++---- platform/darwin/src/NSExpression+MGLPrivateAdditions.h | 4 ++-- platform/darwin/src/NSPredicate+MGLPrivateAdditions.h | 6 +++--- platform/ios/app/MBXCustomLocationViewController.m | 1 + platform/ios/app/MBXFrameTimeGraphView.m | 7 ++++++- platform/ios/app/MBXOrnamentsViewController.m | 2 ++ platform/ios/app/MBXViewController.m | 16 ++++++++-------- platform/ios/ios.xcodeproj/project.pbxproj | 12 ++++++++++++ platform/ios/src/MGLMapView.h | 8 ++++---- platform/ios/src/MGLMapView.mm | 2 +- platform/ios/src/UIView+MGLAdditions.h | 2 +- platform/ios/vendor/SMCalloutView/SMCalloutView.m | 9 +++++++-- 14 files changed, 62 insertions(+), 28 deletions(-) diff --git a/platform/darwin/src/MGLStyleValue_Private.h b/platform/darwin/src/MGLStyleValue_Private.h index fee34b4b71..376bf5e73b 100644 --- a/platform/darwin/src/MGLStyleValue_Private.h +++ b/platform/darwin/src/MGLStyleValue_Private.h @@ -291,7 +291,7 @@ private: // Private utilities for converting from mbgl to mgl values } // Array - static ObjCType toMGLRawStyleValue(const std::vector &mbglStopValue) { + static NSArray *toMGLRawStyleValue(const std::vector &mbglStopValue) { NSMutableArray *array = [NSMutableArray arrayWithCapacity:mbglStopValue.size()]; for (const auto &mbglElement: mbglStopValue) { [array addObject:[NSExpression expressionForConstantValue:toMGLRawStyleValue(mbglElement)]]; diff --git a/platform/darwin/src/NSExpression+MGLAdditions.h b/platform/darwin/src/NSExpression+MGLAdditions.h index a19ec1af2e..9ef6623068 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.h +++ b/platform/darwin/src/NSExpression+MGLAdditions.h @@ -190,7 +190,7 @@ FOUNDATION_EXTERN MGL_EXPORT const MGLExpressionInterpolationMode MGLExpressionI @return An initialized expression equivalent to `object`, suitable for use as the value of a style layer attribute. */ -+ (instancetype)expressionWithMGLJSONObject:(id)object NS_SWIFT_NAME(init(mglJSONObject:)); ++ (instancetype)expressionWithMGLJSONObject:(nullable id)object NS_SWIFT_NAME(init(mglJSONObject:)); /** An equivalent Foundation object that can be serialized as JSON. diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 9dad00e56e..026d8bec02 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -823,9 +823,14 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { minimum = [NSExpression expressionWithMGLJSONObject:valueExpression]; } } - NSExpression *stopExpression = [NSExpression expressionForConstantValue:stops]; - return [NSExpression expressionForFunction:@"mgl_step:from:stops:" - arguments:@[inputExpression, minimum, stopExpression]]; + + NSAssert(minimum, @"minimum should be non-nil"); + if (minimum) { + NSExpression *stopExpression = [NSExpression expressionForConstantValue:stops]; + return [NSExpression expressionForFunction:@"mgl_step:from:stops:" + arguments:@[inputExpression, minimum, stopExpression]]; + } + } else if ([op isEqualToString:@"zoom"]) { return NSExpression.zoomLevelVariableExpression; } else if ([op isEqualToString:@"heatmap-density"]) { @@ -1030,7 +1035,11 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { expressionObject = @[@"get", pathComponent]; } } - return expressionObject; + + NSAssert(expressionObject.count > 0, @"expressionObject should be non-empty"); + + // Return a non-null value to quieten static analysis + return expressionObject ?: @[]; } case NSFunctionExpressionType: { diff --git a/platform/darwin/src/NSExpression+MGLPrivateAdditions.h b/platform/darwin/src/NSExpression+MGLPrivateAdditions.h index 4ef49a2d15..54bc9068f4 100644 --- a/platform/darwin/src/NSExpression+MGLPrivateAdditions.h +++ b/platform/darwin/src/NSExpression+MGLPrivateAdditions.h @@ -13,8 +13,8 @@ NS_ASSUME_NONNULL_BEGIN @interface NSObject (MGLExpressionAdditions) -- (NSNumber *)mgl_number; -- (NSNumber *)mgl_numberWithFallbackValues:(id)fallbackValue, ... NS_REQUIRES_NIL_TERMINATION; +- (nullable NSNumber *)mgl_number; +- (nullable NSNumber *)mgl_numberWithFallbackValues:(id)fallbackValue, ... NS_REQUIRES_NIL_TERMINATION; @end diff --git a/platform/darwin/src/NSPredicate+MGLPrivateAdditions.h b/platform/darwin/src/NSPredicate+MGLPrivateAdditions.h index 1828009678..051a2775a1 100644 --- a/platform/darwin/src/NSPredicate+MGLPrivateAdditions.h +++ b/platform/darwin/src/NSPredicate+MGLPrivateAdditions.h @@ -10,15 +10,15 @@ NS_ASSUME_NONNULL_BEGIN - (mbgl::style::Filter)mgl_filter; -+ (instancetype)mgl_predicateWithFilter:(mbgl::style::Filter)filter; ++ (nullable instancetype)mgl_predicateWithFilter:(mbgl::style::Filter)filter; @end @interface NSPredicate (MGLExpressionAdditions) -- (id)mgl_if:(id)firstValue, ...; +- (nullable id)mgl_if:(id)firstValue, ...; -- (id)mgl_match:(NSExpression *)firstCase, ...; +- (nullable id)mgl_match:(NSExpression *)firstCase, ...; @end diff --git a/platform/ios/app/MBXCustomLocationViewController.m b/platform/ios/app/MBXCustomLocationViewController.m index 34887c5736..0473f8c2ef 100644 --- a/platform/ios/app/MBXCustomLocationViewController.m +++ b/platform/ios/app/MBXCustomLocationViewController.m @@ -168,6 +168,7 @@ } - (void)viewWillDisappear:(BOOL)animated { + [super viewWillDisappear:animated]; self.mapView.showsUserLocation = NO; } diff --git a/platform/ios/app/MBXFrameTimeGraphView.m b/platform/ios/app/MBXFrameTimeGraphView.m index f689768818..e881eb9f48 100644 --- a/platform/ios/app/MBXFrameTimeGraphView.m +++ b/platform/ios/app/MBXFrameTimeGraphView.m @@ -59,7 +59,12 @@ const CGFloat MBXFrameTimeBarWidth = 4.f; self.scrollLayer.frame = self.bounds; CGRect thresholdLineRect = CGRectMake(0, self.frame.size.height - [self renderDurationTargetMilliseconds], self.frame.size.width, 1); - self.thresholdLayer.path = CGPathCreateWithRect(thresholdLineRect, nil); + + { + CGPathRef path = CGPathCreateWithRect(thresholdLineRect, nil); + self.thresholdLayer.path = path; + CGPathRelease(path); + } } } diff --git a/platform/ios/app/MBXOrnamentsViewController.m b/platform/ios/app/MBXOrnamentsViewController.m index 5c7aac7f88..861d5d7678 100644 --- a/platform/ios/app/MBXOrnamentsViewController.m +++ b/platform/ios/app/MBXOrnamentsViewController.m @@ -72,11 +72,13 @@ } - (void)viewDidDisappear:(BOOL)animated { + [super viewDidDisappear:animated]; [self.timer invalidate]; self.timer = nil; } - (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimerTick) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 8d936d6a25..1bd2b2f5b3 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -155,13 +155,13 @@ CLLocationCoordinate2D randomWorldCoordinate() { {{ 12.966246, 77.586505 }, 19000 } // Bengaluru }; - NSInteger index = drand48() * (sizeof(landmasses)/sizeof(landmasses[0])); + NSInteger index = arc4random_uniform(sizeof(landmasses)/sizeof(landmasses[0])); CLLocationCoordinate2D coordinate = landmasses[index].coordinate; CLLocationDistance radius = landmasses[index].radius; // Now create a world coord - CLLocationDegrees heading = drand48()*360.0; - CLLocationDistance distance = drand48()*radius; + CLLocationDegrees heading = (CLLocationDegrees)arc4random_uniform(360); + CLLocationDistance distance = (CLLocationDistance)arc4random_uniform(radius); CLLocationCoordinate2D newLocation = coordinateCentered(coordinate, heading, distance); return newLocation; } @@ -1775,8 +1775,8 @@ CLLocationCoordinate2D randomWorldCoordinate() { NSMutableArray *annotations = [[NSMutableArray alloc] initWithCapacity:numAnnotations]; for (NSInteger i = 0; i