summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLStyleLayer.mm.ejs
diff options
context:
space:
mode:
authorJesse Bounds <jesse@rebounds.net>2016-11-30 11:59:29 -0800
committerGitHub <noreply@github.com>2016-11-30 11:59:29 -0800
commit37a087c675b847f64dd73a570c11b4ddebac35fb (patch)
tree92a76a6dc4ad7aa4aecb0312de7183c012bd9793 /platform/darwin/src/MGLStyleLayer.mm.ejs
parentba775404d88ddb61b7347eb14c5e39cf2df8e1dd (diff)
downloadqtlocation-mapboxgl-37a087c675b847f64dd73a570c11b4ddebac35fb.tar.gz
[ios, macos] Avoid unsafe cast when removing style layers (#7227)
This updates the code generation for style layers to add a test to check that the style object returned from mbgl can be safely cast into the expected type so ownership can be returned to the caller when removing layers. This scenario was previously possible if a layer of type T was added before a layer of type U but both layers shared the same identifier. In that case, if the pointer to the second layer of type U was used to remove a layer, a runtime exception would occur since mbgl returns the layer with type T (the first one added). Now, an exception will still occur if layers with the same identifier are manipulated as described above but it is done as part of a validation check for all public style layer methods at the Darwin platform level. The NSException attempts to warn developers about using and reusing style identifiers to avoid this issue in the first place.
Diffstat (limited to 'platform/darwin/src/MGLStyleLayer.mm.ejs')
-rw-r--r--platform/darwin/src/MGLStyleLayer.mm.ejs29
1 files changed, 28 insertions, 1 deletions
diff --git a/platform/darwin/src/MGLStyleLayer.mm.ejs b/platform/darwin/src/MGLStyleLayer.mm.ejs
index ae90b67d39..85a1af9970 100644
--- a/platform/darwin/src/MGLStyleLayer.mm.ejs
+++ b/platform/darwin/src/MGLStyleLayer.mm.ejs
@@ -81,22 +81,30 @@ namespace mbgl {
<% if (type !== 'background' && type !== 'raster') { -%>
- (NSString *)sourceLayerIdentifier
{
+ MGLAssertStyleLayerIsValid();
+
auto layerID = _rawLayer->getSourceLayer();
return layerID.empty() ? nil : @(layerID.c_str());
}
- (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier
{
+ MGLAssertStyleLayerIsValid();
+
_rawLayer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: "");
}
- (void)setPredicate:(NSPredicate *)predicate
{
+ MGLAssertStyleLayerIsValid();
+
_rawLayer->setFilter(predicate.mgl_filter);
}
- (NSPredicate *)predicate
{
+ MGLAssertStyleLayerIsValid();
+
return [NSPredicate mgl_predicateWithFilter:_rawLayer->getFilter()];
}
<% } -%>
@@ -119,11 +127,22 @@ namespace mbgl {
- (void)removeFromMapView:(MGLMapView *)mapView
{
+ _pendingLayer = nullptr;
+ _rawLayer = nullptr;
+
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
if (!removedLayer) {
return;
}
- _pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::<%- camelize(type) %>Layer> &>(removedLayer));
+
+ mbgl::style::<%- camelize(type) %>Layer *layer = dynamic_cast<mbgl::style::<%- camelize(type) %>Layer *>(removedLayer.get());
+ if (!layer) {
+ return;
+ }
+
+ removedLayer.release();
+
+ _pendingLayer = std::unique_ptr<mbgl::style::<%- camelize(type) %>Layer>(layer);
_rawLayer = _pendingLayer.get();
}
@@ -132,6 +151,8 @@ namespace mbgl {
<% for (const property of layoutProperties) { -%>
- (void)set<%- camelize(property.name) %>:(MGLStyleValue<<%- propertyType(property, true) %>> *)<%- objCName(property) %> {
+ MGLAssertStyleLayerIsValid();
+
<% if (property.type == "enum") { -%>
auto mbglValue = MGLStyleValueTransformer<mbgl::style::<%- mbglType(property) %>, NSValue *, mbgl::style::<%- mbglType(property) %>, MGL<%- camelize(originalPropertyName(property)) %>>().toEnumPropertyValue(<%- objCName(property) %>);
_rawLayer->set<%- camelize(originalPropertyName(property)) %>(mbglValue);
@@ -142,6 +163,8 @@ namespace mbgl {
}
- (MGLStyleValue<<%- propertyType(property, true) %>> *)<%- objCName(property) %> {
+ MGLAssertStyleLayerIsValid();
+
auto propertyValue = _rawLayer->get<%- camelize(originalPropertyName(property)) %>() ?: _rawLayer->getDefault<%- camelize(originalPropertyName(property)) %>();
<% if (property.type == "enum") { -%>
return MGLStyleValueTransformer<mbgl::style::<%- mbglType(property) %>, NSValue *, mbgl::style::<%- mbglType(property) %>, MGL<%- camelize(originalPropertyName(property)) %>>().toEnumStyleValue(propertyValue);
@@ -157,6 +180,8 @@ namespace mbgl {
<% for (const property of paintProperties) { -%>
- (void)set<%- camelize(property.name) %>:(MGLStyleValue<<%- propertyType(property, true) %>> *)<%- objCName(property) %> {
+ MGLAssertStyleLayerIsValid();
+
<% if (property.type == "enum") { -%>
auto mbglValue = MGLStyleValueTransformer<mbgl::style::<%- mbglType(property) %>, NSValue *, mbgl::style::<%- mbglType(property) %>, MGL<%- camelize(originalPropertyName(property)) %>>().toEnumPropertyValue(<%- objCName(property) %>);
_rawLayer->set<%- camelize(originalPropertyName(property)) %>(mbglValue);
@@ -167,6 +192,8 @@ namespace mbgl {
}
- (MGLStyleValue<<%- propertyType(property, true) %>> *)<%- objCName(property) %> {
+ MGLAssertStyleLayerIsValid();
+
auto propertyValue = _rawLayer->get<%- camelize(originalPropertyName(property)) %>() ?: _rawLayer->getDefault<%- camelize(originalPropertyName(property)) %>();
<% if (property.type == "enum") { -%>
return MGLStyleValueTransformer<mbgl::style::<%- mbglType(property) %>, NSValue *, mbgl::style::<%- mbglType(property) %>, MGL<%- camelize(originalPropertyName(property)) %>>().toEnumStyleValue(propertyValue);