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
+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;
}
}