refactor(mod): validate modulation matrix and macro inputs
Add bounds, finiteness, and range clamping to addConnection and addAssignment. Validate ValueTree types and property values in fromValueTree before inserting, rejecting non-integer indices and unknown source names. Run toValueTree through a validated copy to ensure persisted state is always within constraints.
This commit is contained in:
+26
-11
@@ -5,10 +5,11 @@ namespace serum
|
|||||||
|
|
||||||
bool MacroControls::addAssignment (int macro, ModTarget target, float depth)
|
bool MacroControls::addAssignment (int macro, ModTarget target, float depth)
|
||||||
{
|
{
|
||||||
macro = juce::jlimit (0, kNumMacros - 1, macro);
|
if (macro < 0 || macro >= kNumMacros
|
||||||
if (target == ModTarget::None || (int) assignments[(size_t) macro].size() >= kMaxAssignments)
|
|| (int) target < 0 || (int) target >= kNumModTargets
|
||||||
|
|| ! std::isfinite (depth) || (int) assignments[(size_t) macro].size() >= kMaxAssignments)
|
||||||
return false;
|
return false;
|
||||||
assignments[(size_t) macro].push_back ({ target, depth });
|
assignments[(size_t) macro].push_back ({ target, juce::jlimit (-1.0f, 1.0f, depth) });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,12 +41,21 @@ juce::String MacroControls::macroName (int index)
|
|||||||
|
|
||||||
juce::ValueTree MacroControls::toValueTree() const
|
juce::ValueTree MacroControls::toValueTree() const
|
||||||
{
|
{
|
||||||
|
MacroControls validated;
|
||||||
|
for (int m = 0; m < kNumMacros; ++m)
|
||||||
|
for (const auto& a : assignments[(size_t) m])
|
||||||
|
{
|
||||||
|
if ((int) validated.assignments[(size_t) m].size() >= kMaxAssignments)
|
||||||
|
break;
|
||||||
|
if (! validated.addAssignment (m, a.target, a.depth))
|
||||||
|
continue;
|
||||||
|
}
|
||||||
juce::ValueTree tree ("MACROS");
|
juce::ValueTree tree ("MACROS");
|
||||||
for (int m = 0; m < kNumMacros; ++m)
|
for (int m = 0; m < kNumMacros; ++m)
|
||||||
{
|
{
|
||||||
juce::ValueTree mac ("MACRO");
|
juce::ValueTree mac ("MACRO");
|
||||||
mac.setProperty ("index", m, nullptr);
|
mac.setProperty ("index", m, nullptr);
|
||||||
for (const auto& a : assignments[(size_t) m])
|
for (const auto& a : validated.assignments[(size_t) m])
|
||||||
{
|
{
|
||||||
juce::ValueTree asg ("ASSIGN");
|
juce::ValueTree asg ("ASSIGN");
|
||||||
asg.setProperty ("target", modTargetToString (a.target), nullptr);
|
asg.setProperty ("target", modTargetToString (a.target), nullptr);
|
||||||
@@ -60,23 +70,28 @@ juce::ValueTree MacroControls::toValueTree() const
|
|||||||
void MacroControls::fromValueTree (const juce::ValueTree& tree)
|
void MacroControls::fromValueTree (const juce::ValueTree& tree)
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
if (! tree.isValid())
|
if (! tree.hasType ("MACROS"))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (const auto& mac : tree)
|
for (const auto& mac : tree)
|
||||||
{
|
{
|
||||||
if (! mac.hasType ("MACRO"))
|
if (! mac.hasType ("MACRO"))
|
||||||
continue;
|
continue;
|
||||||
const int index = juce::jlimit (0, kNumMacros - 1, (int) mac.getProperty ("index", 0));
|
const double savedIndex = (double) mac.getProperty ("index", -1);
|
||||||
|
if (! std::isfinite (savedIndex) || savedIndex < 0.0 || savedIndex >= kNumMacros
|
||||||
|
|| std::floor (savedIndex) != savedIndex)
|
||||||
|
continue;
|
||||||
|
const int index = (int) savedIndex;
|
||||||
for (const auto& asg : mac)
|
for (const auto& asg : mac)
|
||||||
{
|
{
|
||||||
|
if ((int) assignments[(size_t) index].size() >= kMaxAssignments)
|
||||||
|
break;
|
||||||
if (! asg.hasType ("ASSIGN"))
|
if (! asg.hasType ("ASSIGN"))
|
||||||
continue;
|
continue;
|
||||||
MacroAssignment a;
|
if (! addAssignment (index,
|
||||||
a.target = modTargetFromString (asg.getProperty ("target").toString());
|
modTargetFromString (asg.getProperty ("target").toString()),
|
||||||
a.depth = (float) asg.getProperty ("depth", 0.0);
|
(float) asg.getProperty ("depth", 0.0)))
|
||||||
if (a.target != ModTarget::None)
|
continue;
|
||||||
assignments[(size_t) index].push_back (a);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-11
@@ -5,9 +5,11 @@ namespace serum
|
|||||||
|
|
||||||
bool ModulationMatrix::addConnection (ModSource source, ModTarget target, float depth, bool bipolar)
|
bool ModulationMatrix::addConnection (ModSource source, ModTarget target, float depth, bool bipolar)
|
||||||
{
|
{
|
||||||
if (target == ModTarget::None || (int) connections.size() >= kMaxConnections)
|
if ((int) source < 0 || (int) source >= kNumModSources
|
||||||
|
|| (int) target < 0 || (int) target >= kNumModTargets
|
||||||
|
|| ! std::isfinite (depth) || (int) connections.size() >= kMaxConnections)
|
||||||
return false;
|
return false;
|
||||||
connections.push_back ({ source, target, depth, bipolar });
|
connections.push_back ({ source, target, juce::jlimit (-1.0f, 1.0f, depth), bipolar });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,8 +28,16 @@ void ModulationMatrix::removeAllWithTarget (ModTarget target)
|
|||||||
|
|
||||||
juce::ValueTree ModulationMatrix::toValueTree() const
|
juce::ValueTree ModulationMatrix::toValueTree() const
|
||||||
{
|
{
|
||||||
juce::ValueTree tree ("MODMATRIX");
|
ModulationMatrix validated;
|
||||||
for (const auto& c : connections)
|
for (const auto& c : connections)
|
||||||
|
{
|
||||||
|
if (validated.size() >= kMaxConnections)
|
||||||
|
break;
|
||||||
|
if (! validated.addConnection (c.source, c.target, c.depth, c.bipolar))
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
juce::ValueTree tree ("MODMATRIX");
|
||||||
|
for (const auto& c : validated.connections)
|
||||||
{
|
{
|
||||||
juce::ValueTree con ("CONNECTION");
|
juce::ValueTree con ("CONNECTION");
|
||||||
con.setProperty ("source", modSourceToString (c.source), nullptr);
|
con.setProperty ("source", modSourceToString (c.source), nullptr);
|
||||||
@@ -42,20 +52,24 @@ juce::ValueTree ModulationMatrix::toValueTree() const
|
|||||||
void ModulationMatrix::fromValueTree (const juce::ValueTree& tree)
|
void ModulationMatrix::fromValueTree (const juce::ValueTree& tree)
|
||||||
{
|
{
|
||||||
connections.clear();
|
connections.clear();
|
||||||
if (! tree.isValid())
|
if (! tree.hasType ("MODMATRIX"))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (const auto& con : tree)
|
for (const auto& con : tree)
|
||||||
{
|
{
|
||||||
|
if ((int) connections.size() >= kMaxConnections)
|
||||||
|
break;
|
||||||
if (! con.hasType ("CONNECTION"))
|
if (! con.hasType ("CONNECTION"))
|
||||||
continue;
|
continue;
|
||||||
ModConnection c;
|
const auto sourceName = con.getProperty ("source").toString();
|
||||||
c.source = modSourceFromString (con.getProperty ("source").toString());
|
const auto source = modSourceFromString (sourceName);
|
||||||
c.target = modTargetFromString (con.getProperty ("target").toString());
|
if (sourceName.isEmpty() || modSourceToString (source) != sourceName)
|
||||||
c.depth = (float) con.getProperty ("depth", 0.0);
|
continue;
|
||||||
c.bipolar = (bool) con.getProperty ("bipolar", false);
|
if (! addConnection (source,
|
||||||
if (c.target != ModTarget::None && (int) connections.size() < kMaxConnections)
|
modTargetFromString (con.getProperty ("target").toString()),
|
||||||
connections.push_back (c);
|
(float) con.getProperty ("depth", 0.0),
|
||||||
|
(bool) con.getProperty ("bipolar", false)))
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user