fix(cpn): radio specific name and label validators (#4670)

Only allows valid characters in names, drops invalid characters if present
This commit is contained in:
Neil Horne
2024-02-28 11:35:57 +10:00
committed by GitHub
parent 3807d7759b
commit 57c7aca2f6
31 changed files with 428 additions and 139 deletions
@@ -26,6 +26,7 @@
#include "yaml_ops.h"
#include "yaml_generalsettings.h"
#include "yaml_modeldata.h"
#include "labelvalidator.h"
#include <QMessageBox>
@@ -113,11 +114,14 @@ bool loadLabelsListFromYaml(RadioData::ModelLabels& labels,
std::string lbl = it->first.as<std::string>();
RadioData::LabelData ld;
ld.name = QString::fromStdString(lbl);
if (lbls[lbl]["selected"])
ld.selected = lbls[lbl]["selected"].as<bool>();
else
ld.selected = false;
labels.append(ld);
YamlValidateLabel(ld.name);
if (!ld.name.isEmpty()) {
if (lbls[lbl]["selected"])
ld.selected = lbls[lbl]["selected"].as<bool>();
else
ld.selected = false;
labels.append(ld);
}
}
}
@@ -32,6 +32,17 @@
#include <QMessageBox>
void YamlValidateNames(GeneralSettings& gs, Board::Type board)
{
for (int i = 0; i < CPN_MAX_INPUTS; i++) {
YamlValidateName(gs.inputConfig[i].name, board);
}
for (int i = 0; i < CPN_MAX_SWITCHES; i++) {
YamlValidateName(gs.switchConfig[i].name, board);
}
}
const YamlLookupTable beeperModeLut = {
{ GeneralSettings::BEEPER_QUIET, "mode_quiet" },
{ GeneralSettings::BEEPER_ALARMS_ONLY, "mode_alarms" },
@@ -599,7 +610,8 @@ bool convert<GeneralSettings>::decode(const Node& node, GeneralSettings& rhs)
node["labelMultiMode"] >> rhs.labelMultiMode;
node["favMultiMode"] >> rhs.favMultiMode;
// fix ups
// perform integrity checks and fix-ups
YamlValidateNames(rhs, fw->getBoard());
rhs.validateFlexSwitches();
return true;
@@ -41,6 +41,57 @@
SemanticVersion version; // used for data conversions
void YamlValidateLabelsNames(ModelData& model, Board::Type board)
{
YamlValidateName(model.name, board);
QStringList lst = QString(model.labels).split(',', Qt::SkipEmptyParts);
for (int i = lst.count() - 1; i >= 0; i--) {
YamlValidateLabel(lst[i]);
if (lst.at(i).isEmpty())
lst.removeAt(i);
}
strcpy(model.labels, QString(lst.join(',')).toLatin1().data());
for (int i = 0; i < CPN_MAX_CURVES; i++) {
YamlValidateName(model.curves[i].name, board);
}
for (int i = 0; i < CPN_MAX_EXPOS; i++) {
YamlValidateName(model.expoData[i].name, board);
}
for (int i = 0; i < CPN_MAX_GVARS; i++) {
YamlValidateName(model.gvarData[i].name, board);
}
for (int i = 0; i < CPN_MAX_FLIGHT_MODES; i++) {
YamlValidateName(model.flightModeData[i].name, board);
}
for (int i = 0; i < CPN_MAX_SWITCHES_FUNCTION; i++) {
YamlValidateName(model.functionSwitchNames[i], board);
}
for (int i = 0; i < CPN_MAX_INPUTS; i++) {
YamlValidateName(model.inputNames[i], board);
}
for (int i = 0; i < CPN_MAX_CHNOUT; i++) {
YamlValidateName(model.limitData[i].name, board);
}
for (int i = 0; i < CPN_MAX_MIXERS; i++) {
YamlValidateName(model.mixData[i].name, board);
}
for (int i = 0; i < CPN_MAX_SENSORS; i++) {
YamlValidateName(model.sensorData[i].label, board);
}
}
static const YamlLookupTable timerModeLut = {
{TimerData::TIMERMODE_OFF, "OFF"},
{TimerData::TIMERMODE_ON, "ON"},
@@ -1092,6 +1143,8 @@ bool convert<ModelData>::decode(const Node& node, ModelData& rhs)
{
if (!node.IsMap()) return false;
Board::Type board = getCurrentBoard();
unsigned int modelIds[CPN_MAX_MODULES];
memset(modelIds, 0, sizeof(modelIds));
@@ -1151,6 +1204,7 @@ bool convert<ModelData>::decode(const Node& node, ModelData& rhs)
node["limitData"] >> rhs.limitData;
node["inputNames"] >> rhs.inputNames;
node["expoData"] >> rhs.expoData;
node["curves"] >> rhs.curves;
@@ -1326,7 +1380,7 @@ bool convert<ModelData>::decode(const Node& node, ModelData& rhs)
}
// perform integrity checks and fix-ups
YamlValidateLabelsNames(rhs, board);
rhs.sortMixes(); // critical for Companion and radio that mix lines are in sequence
return true;
@@ -19,6 +19,8 @@
*/
#include "yaml_ops.h"
#include "labelvalidator.h"
#include "namevalidator.h"
SemanticVersion radioSettingsVersion;
SemanticVersion modelSettingsVersion;
@@ -57,6 +59,32 @@ void operator >> (const YAML::Node& node, bool& value)
}
}
void YamlValidateLabel(QString &input)
{
LabelValidator *lv = new LabelValidator();
if (!lv->isValid(input)) {
lv->fixup(input);
input = input.trimmed();
}
delete lv;
}
void YamlValidateName(char *input, Board::Type board)
{
NameValidator *nv = new NameValidator(board);
QString in(input);
if (!nv->isValid(in)) {
nv->fixup(in);
in = in.trimmed();
}
strcpy(input, in.toLatin1().data());
delete nv;
}
namespace YAML {
std::string LookupValue(const YamlLookupTable& lut, const int& value)
+6 -1
View File
@@ -21,9 +21,11 @@
#pragma once
#include <yaml-cpp/yaml.h>
#include "helpers.h"
#include "boards.h"
#include <algorithm>
#include <QString>
#include "helpers.h"
typedef std::pair<int, std::string> YamlLookupTableElmt;
typedef std::vector<YamlLookupTableElmt> YamlLookupTable;
@@ -122,6 +124,9 @@ void operator>>(const YAML::Node& node, T (&value)[N])
}
}
void YamlValidateName(char *input, Board::Type board);
void YamlValidateLabel(QString &input);
namespace YAML {
std::string LookupValue(const YamlLookupTable& lut, const int& value);