Browse Source

Normalize plugin and model slugs when loading patches. Fail with warning when model slug is invalid, instead of crashing.

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
69c5ec8983
3 changed files with 21 additions and 2 deletions
  1. +2
    -0
      include/plugin.hpp
  2. +12
    -1
      src/plugin.cpp
  3. +7
    -1
      src/plugin/Plugin.cpp

+ 2
- 0
include/plugin.hpp View File

@@ -27,6 +27,8 @@ Model *getModel(const std::string &pluginSlug, const std::string &modelSlug);
std::string normalizeTag(const std::string &tag);
/** Checks that the slug contains only alphanumeric characters, "-", and "_" */
bool isSlugValid(const std::string &slug);
/** Returns a string containing only the valid slug characters. */
std::string normalizeSlug(const std::string &slug);


struct Update {


+ 12
- 1
src/plugin.cpp View File

@@ -485,8 +485,9 @@ bool isLoggedIn() {
}

Plugin *getPlugin(const std::string &pluginSlug) {
std::string slug = normalizeSlug(pluginSlug);
for (Plugin *plugin : plugins) {
if (plugin->slug == pluginSlug) {
if (plugin->slug == slug) {
return plugin;
}
}
@@ -610,6 +611,16 @@ bool isSlugValid(const std::string &slug) {
return true;
}

std::string normalizeSlug(const std::string &slug) {
std::string s;
for (char c : slug) {
if (!(std::isalnum(c) || c == '-' || c == '_'))
continue;
s += c;
}
return s;
}


std::vector<Plugin*> plugins;



+ 7
- 1
src/plugin/Plugin.cpp View File

@@ -14,13 +14,19 @@ Plugin::~Plugin() {
}

void Plugin::addModel(Model *model) {
assert(isSlugValid(model->slug));
// Check that the model is not added to a plugin already
assert(!model->plugin);
// Check model slug
if (!isSlugValid(model->slug)) {
WARN("Module slug \"%s\" is invalid", model->slug.c_str());
return;
}
model->plugin = this;
models.push_back(model);
}

Model *Plugin::getModel(std::string slug) {
slug = normalizeSlug(slug);
for (Model *model : models) {
if (model->slug == slug) {
return model;


Loading…
Cancel
Save