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:
2026-09-09 14:33:06 +02:00
parent 4748bfe768
commit e2e66457c2
2 changed files with 51 additions and 22 deletions
+26 -11
View File
@@ -5,10 +5,11 @@ namespace serum
bool MacroControls::addAssignment (int macro, ModTarget target, float depth)
{
macro = juce::jlimit (0, kNumMacros - 1, macro);
if (target == ModTarget::None || (int) assignments[(size_t) macro].size() >= kMaxAssignments)
if (macro < 0 || macro >= kNumMacros
|| (int) target < 0 || (int) target >= kNumModTargets
|| ! std::isfinite (depth) || (int) assignments[(size_t) macro].size() >= kMaxAssignments)
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;
}
@@ -40,12 +41,21 @@ juce::String MacroControls::macroName (int index)
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");
for (int m = 0; m < kNumMacros; ++m)
{
juce::ValueTree mac ("MACRO");
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");
asg.setProperty ("target", modTargetToString (a.target), nullptr);
@@ -60,23 +70,28 @@ juce::ValueTree MacroControls::toValueTree() const
void MacroControls::fromValueTree (const juce::ValueTree& tree)
{
clear();
if (! tree.isValid())
if (! tree.hasType ("MACROS"))
return;
for (const auto& mac : tree)
{
if (! mac.hasType ("MACRO"))
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)
{
if ((int) assignments[(size_t) index].size() >= kMaxAssignments)
break;
if (! asg.hasType ("ASSIGN"))
continue;
MacroAssignment a;
a.target = modTargetFromString (asg.getProperty ("target").toString());
a.depth = (float) asg.getProperty ("depth", 0.0);
if (a.target != ModTarget::None)
assignments[(size_t) index].push_back (a);
if (! addAssignment (index,
modTargetFromString (asg.getProperty ("target").toString()),
(float) asg.getProperty ("depth", 0.0)))
continue;
}
}
}
+25 -11
View File
@@ -5,9 +5,11 @@ namespace serum
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;
connections.push_back ({ source, target, depth, bipolar });
connections.push_back ({ source, target, juce::jlimit (-1.0f, 1.0f, depth), bipolar });
return true;
}
@@ -26,8 +28,16 @@ void ModulationMatrix::removeAllWithTarget (ModTarget target)
juce::ValueTree ModulationMatrix::toValueTree() const
{
juce::ValueTree tree ("MODMATRIX");
ModulationMatrix validated;
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");
con.setProperty ("source", modSourceToString (c.source), nullptr);
@@ -42,20 +52,24 @@ juce::ValueTree ModulationMatrix::toValueTree() const
void ModulationMatrix::fromValueTree (const juce::ValueTree& tree)
{
connections.clear();
if (! tree.isValid())
if (! tree.hasType ("MODMATRIX"))
return;
for (const auto& con : tree)
{
if ((int) connections.size() >= kMaxConnections)
break;
if (! con.hasType ("CONNECTION"))
continue;
ModConnection c;
c.source = modSourceFromString (con.getProperty ("source").toString());
c.target = modTargetFromString (con.getProperty ("target").toString());
c.depth = (float) con.getProperty ("depth", 0.0);
c.bipolar = (bool) con.getProperty ("bipolar", false);
if (c.target != ModTarget::None && (int) connections.size() < kMaxConnections)
connections.push_back (c);
const auto sourceName = con.getProperty ("source").toString();
const auto source = modSourceFromString (sourceName);
if (sourceName.isEmpty() || modSourceToString (source) != sourceName)
continue;
if (! addConnection (source,
modTargetFromString (con.getProperty ("target").toString()),
(float) con.getProperty ("depth", 0.0),
(bool) con.getProperty ("bipolar", false)))
continue;
}
}