Browse Source

Add slug validation to plugin loader

tags/v1.0.0
Andrew Belt 6 years ago
parent
commit
3dddb3b240
3 changed files with 32 additions and 14 deletions
  1. +2
    -0
      include/plugin.hpp
  2. +14
    -12
      src/engine/Engine.cpp
  3. +16
    -2
      src/plugin.cpp

+ 2
- 0
include/plugin.hpp View File

@@ -21,6 +21,8 @@ bool isLoggedIn();
Plugin *getPlugin(std::string pluginSlug); Plugin *getPlugin(std::string pluginSlug);
Model *getModel(std::string pluginSlug, std::string modelSlug); Model *getModel(std::string pluginSlug, std::string modelSlug);
std::string getAllowedTag(std::string tag); std::string getAllowedTag(std::string tag);
/** Checks that the slug contains only alphanumeric characters, "-", and "_" */
bool isSlugValid(std::string slug);




extern std::list<Plugin*> plugins; extern std::list<Plugin*> plugins;


+ 14
- 12
src/engine/Engine.cpp View File

@@ -105,30 +105,31 @@ static void Engine_step(Engine *engine) {


// Param smoothing // Param smoothing
{ {
// Store in local variables for thread safety
Module *localSmoothModule = engine->internal->smoothModule;
int localSmoothParamId = engine->internal->smoothParamId;
float localSmoothValue = engine->internal->smoothValue;
if (localSmoothModule) {
float value = localSmoothModule->params[localSmoothParamId].value;
Module *smoothModule = engine->internal->smoothModule;
int smoothParamId = engine->internal->smoothParamId;
float smoothValue = engine->internal->smoothValue;
if (smoothModule) {
Param *param = &smoothModule->params[smoothParamId];
float value = param->value;
// decay rate is 1 graphics frame // decay rate is 1 graphics frame
const float lambda = 60.f; const float lambda = 60.f;
float delta = localSmoothValue - value;
float delta = smoothValue - value;
float newValue = value + delta * lambda * engine->internal->sampleTime; float newValue = value + delta * lambda * engine->internal->sampleTime;
if (value == newValue) { if (value == newValue) {
// Snap to actual smooth value if the value doesn't change enough (due to the granularity of floats) // Snap to actual smooth value if the value doesn't change enough (due to the granularity of floats)
localSmoothModule->params[localSmoothParamId].value = localSmoothValue;
param->value = smoothValue;
engine->internal->smoothModule = NULL; engine->internal->smoothModule = NULL;
} }
else { else {
localSmoothModule->params[localSmoothParamId].value = newValue;
param->value = newValue;
} }
} }
} }


// Step modules
// Iterate modules
for (Module *module : engine->modules) { for (Module *module : engine->modules) {
if (module->bypass) { if (module->bypass) {
// Bypass module
for (Output &output : module->outputs) { for (Output &output : module->outputs) {
output.numChannels = 1; output.numChannels = 1;
output.setValue(0.f); output.setValue(0.f);
@@ -136,6 +137,7 @@ static void Engine_step(Engine *engine) {
module->cpuTime = 0.f; module->cpuTime = 0.f;
} }
else { else {
// Step module
if (settings::powerMeter) { if (settings::powerMeter) {
auto startTime = std::chrono::high_resolution_clock::now(); auto startTime = std::chrono::high_resolution_clock::now();


@@ -151,7 +153,7 @@ static void Engine_step(Engine *engine) {
} }
} }


// Step ports
// Iterate ports and step plug lights
for (Input &input : module->inputs) { for (Input &input : module->inputs) {
if (input.active) { if (input.active) {
float value = input.value / 5.f; float value = input.value / 5.f;
@@ -168,7 +170,7 @@ static void Engine_step(Engine *engine) {
} }
} }


// Step cables by moving their output values to inputs
// Step cables
for (Wire *wire : engine->wires) { for (Wire *wire : engine->wires) {
wire->step(); wire->step();
} }


+ 16
- 2
src/plugin.cpp View File

@@ -114,12 +114,18 @@ static bool loadPlugin(std::string path) {
initCallback(plugin); initCallback(plugin);
plugin->fromJson(rootJ); plugin->fromJson(rootJ);


// Check slug
if (!isSlugValid(plugin->slug)) {
WARN("Plugin slug \"%s\" is invalid", plugin->slug.c_str());
// TODO Fix memory leak with `plugin`
return false;
}

// Reject plugin if slug already exists // Reject plugin if slug already exists
Plugin *oldPlugin = getPlugin(plugin->slug); Plugin *oldPlugin = getPlugin(plugin->slug);
if (oldPlugin) { if (oldPlugin) {
WARN("Plugin \"%s\" is already loaded, not attempting to load it again", plugin->slug.c_str()); WARN("Plugin \"%s\" is already loaded, not attempting to load it again", plugin->slug.c_str());
// TODO
// Fix memory leak with `plugin` here
// TODO Fix memory leak with `plugin`
return false; return false;
} }


@@ -512,6 +518,14 @@ std::string getAllowedTag(std::string tag) {
return ""; return "";
} }


bool isSlugValid(std::string slug) {
for (char c : slug) {
if (!(std::isalnum(c) || c == '-' || c == '_'))
return false;
}
return true;
}



std::list<Plugin*> plugins; std::list<Plugin*> plugins;
std::string token; std::string token;


Loading…
Cancel
Save