Browse Source

Add slug validation to plugin loader

tags/v1.0.0
Andrew Belt 5 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);
Model *getModel(std::string pluginSlug, std::string modelSlug);
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;


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

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

// 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
const float lambda = 60.f;
float delta = localSmoothValue - value;
float delta = smoothValue - value;
float newValue = value + delta * lambda * engine->internal->sampleTime;
if (value == newValue) {
// 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;
}
else {
localSmoothModule->params[localSmoothParamId].value = newValue;
param->value = newValue;
}
}
}

// Step modules
// Iterate modules
for (Module *module : engine->modules) {
if (module->bypass) {
// Bypass module
for (Output &output : module->outputs) {
output.numChannels = 1;
output.setValue(0.f);
@@ -136,6 +137,7 @@ static void Engine_step(Engine *engine) {
module->cpuTime = 0.f;
}
else {
// Step module
if (settings::powerMeter) {
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) {
if (input.active) {
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) {
wire->step();
}


+ 16
- 2
src/plugin.cpp View File

@@ -114,12 +114,18 @@ static bool loadPlugin(std::string path) {
initCallback(plugin);
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
Plugin *oldPlugin = getPlugin(plugin->slug);
if (oldPlugin) {
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;
}

@@ -512,6 +518,14 @@ std::string getAllowedTag(std::string tag) {
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::string token;


Loading…
Cancel
Save