From bb6973ae90d91134de168dc75f0d51e3e28c8ae4 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Thu, 1 Dec 2016 11:23:26 +0200 Subject: [core] guard against duplicate layer ids --- src/mbgl/style/style.cpp | 9 +++++++++ test/style/style_layer.test.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index e55119f9fd..d28963aa64 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -198,6 +198,15 @@ Layer* Style::getLayer(const std::string& id) const { Layer* Style::addLayer(std::unique_ptr layer, optional before) { // TODO: verify source + // Guard against duplicate layer ids + auto it = std::find_if(layers.begin(), layers.end(), [&](const auto& existing) { + return existing->getID() == layer->getID(); + }); + + if (it != layers.end()) { + throw std::runtime_error(std::string{"Layer "} + layer->getID() + " already exists"); + } + if (SymbolLayer* symbolLayer = layer->as()) { if (!symbolLayer->impl->spriteAtlas) { symbolLayer->impl->spriteAtlas = spriteAtlas.get(); diff --git a/test/style/style_layer.test.cpp b/test/style/style_layer.test.cpp index f6dcc684c7..8356f3accd 100644 --- a/test/style/style_layer.test.cpp +++ b/test/style/style_layer.test.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include @@ -15,6 +17,10 @@ #include #include #include +#include +#include + +#include using namespace mbgl; using namespace mbgl::style; @@ -268,3 +274,25 @@ TEST(Layer, Observer) { layer->setLineCap(lineCap); EXPECT_FALSE(layoutPropertyChanged); } + +TEST(Layer, DuplicateLayer) { + util::RunLoop loop; + + //Setup style + StubFileSource fileSource; + Style style { fileSource, 1.0 }; + style.setJSON(util::read_file("test/fixtures/resources/style-unused-sources.json")); + + //Add initial layer + style.addLayer(std::make_unique("line", "unusedsource")); + + //Try to add duplicate + try { + style.addLayer(std::make_unique("line", "unusedsource")); + FAIL() << "Should not have been allowed to add a duplicate layer id"; + } catch (const std::runtime_error e) { + //Expected + ASSERT_STREQ("Layer line already exists", e.what()); + } +} + -- cgit v1.2.1