From d39ea23b0975bf39f54bb181b14f3caed69a4767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Tue, 4 Apr 2017 21:08:50 -0700 Subject: [ios, macos] Hardened std::map usage in MGLMapVIew Replaced std::map::operator[] usage with std::map::at(). Backed up an assertion on iOS with a guard when -annotationTagsInRect: returns the tag of a nonexistent annotation, for consistency with macOS. Removed an unnecessary and risky subscript into _annotationContextsByAnnotationTag in -positioningRectForCalloutForAnnotationWithTag: on iOS. --- platform/ios/src/MGLMapView.mm | 12 +++++++----- platform/macos/src/MGLMapView.mm | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index ef4326556b..7458c39299 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -1452,7 +1452,7 @@ public: } else { - nextElement = _annotationContextsByAnnotationTag[_selectedAnnotationTag].accessibilityElement; + nextElement = _annotationContextsByAnnotationTag.at(_selectedAnnotationTag).accessibilityElement; } [self deselectAnnotation:self.selectedAnnotation animated:YES]; UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nextElement); @@ -3082,7 +3082,7 @@ public: return nil; } - MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag[tag]; + MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(tag); return annotationContext.annotation; } @@ -3552,8 +3552,12 @@ public: { id annotation = [self annotationWithTag:annotationTag]; NSAssert(annotation, @"Unknown annotation found nearby tap"); + if ( ! annotation) + { + return true; + } - MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag[annotationTag]; + MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); CGRect annotationRect; MGLAnnotationView *annotationView = annotationContext.annotationView; @@ -3867,8 +3871,6 @@ public: /// and is appropriate for positioning a popover. - (CGRect)positioningRectForCalloutForAnnotationWithTag:(MGLAnnotationTag)annotationTag { - MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag[annotationTag]; - id annotation = [self annotationWithTag:annotationTag]; if ( ! annotation) { diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index fc9834e016..a5a5c53df3 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -1820,7 +1820,7 @@ public: return nil; } - MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag[tag]; + MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(tag); return annotationContext.annotation; } @@ -2057,8 +2057,8 @@ public: // Filter out any annotation whose image is unselectable or for which // hit testing fails. auto end = std::remove_if(nearbyAnnotations.begin(), nearbyAnnotations.end(), [&](const MGLAnnotationTag annotationTag) { - NSAssert(_annotationContextsByAnnotationTag.count(annotationTag) != 0, @"Unknown annotation found nearby click"); id annotation = [self annotationWithTag:annotationTag]; + NSAssert(annotation, @"Unknown annotation found nearby click"); if (!annotation) { return true; } -- cgit v1.2.1