Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an fraction typein; be defensive about strncpy #171

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions include/sst/basic-blocks/params/ParamMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ struct ParamMetaData
enum struct Features : uint64_t
{
SUPPORTS_MULTIPLICATIVE_MODULATION = 1 << 0,
BELOW_ONE_IS_INVERSE_FRACTION = 1 << 1
BELOW_ONE_IS_INVERSE_FRACTION = 1 << 1,
ALLOW_FRACTIONAL_TYPEINS = 1 << 2
};
uint64_t features{0};
ParamMetaData withFeature(Features f) const
Expand Down Expand Up @@ -858,8 +859,10 @@ struct ParamMetaData
void toClapParamInfo(IsAClapParamInfo *info) const
{
info->id = id;
strncpy(info->name, name.c_str(), stringSize);
strncpy(info->module, groupName.c_str(), stringSize);
strncpy(info->name, name.c_str(), stringSize - 2);
info->name[stringSize - 1] = 0;
strncpy(info->module, groupName.c_str(), stringSize - 2);
info->module[stringSize - 1] = 0;
info->min_value = minVal;
info->max_value = maxVal;
info->default_value = defaultVal;
Expand Down Expand Up @@ -1142,6 +1145,19 @@ inline std::optional<float> ParamMetaData::valueFromString(std::string_view v, s
else
r = 1.0 / uv;
}
else if ((features & (uint64_t)Features::ALLOW_FRACTIONAL_TYPEINS) &&
vs.find("/") != std::string::npos)
{
auto ps = vs.find("/");
auto num = vs.substr(0, ps);
auto den = vs.substr(ps + 1);
auto uv = std::stof(num);
auto dv = std::stof(den);
if (uv == 0 || dv == 0)
r = std::stof(std::string(v));
else
r = uv / dv;
}
else
{
r = std::stof(std::string(v));
Expand Down