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
+10 -11
View File
@@ -109,8 +109,8 @@ void RadioData::convert(RadioDataConversionState & cstate)
void RadioData::addLabel(QString label)
{
label = unEscapeCSV(label);
// Truncate possible UTF-8 to 16char maximum
QByteArray output = label.toUtf8();
// Truncate possible UTF-8 to 16 char maximum
QByteArray output = label.toLatin1();
if (output.size() > LABEL_LENGTH) {
int truncateAt = 0;
for (int i = LABEL_LENGTH; i > 0; i--) {
@@ -135,12 +135,12 @@ bool RadioData::deleteLabel(QString label)
// Remove labels in the models
for(auto& model : models) {
QStringList modelLabels = fromCSV(QString::fromUtf8(model.labels));
QStringList modelLabels = fromCSV(QString::fromLatin1(model.labels));
if (modelLabels.indexOf(label) >= 0) {
deleted = true;
modelLabels.removeAll(label);
}
strcpy(model.labels, toCSV(modelLabels).toUtf8().data());
strcpy(model.labels, toCSV(modelLabels).toLatin1().data());
}
// Remove the label from the global list
@@ -186,8 +186,8 @@ bool RadioData::renameLabel(QString from, QString to)
if (ind != -1) {
modelLabels.replace(ind, csvTo);
QString outputcsv = QString(modelLabels.join(','));
if (outputcsv.toUtf8().size() < (int)sizeof(model.labels)) {
strcpy(model.labels, outputcsv.toUtf8().data());
if (outputcsv.toLatin1().size() < (int)sizeof(model.labels)) {
strcpy(model.labels, outputcsv.toLatin1().data());
} else { // Shouldn't ever get here, from check above
success = false;
throw std::length_error(model.name);
@@ -231,10 +231,10 @@ bool RadioData::addLabelToModel(int index, QString label)
char *modelLabelCsv = models[index].labels;
// Make sure it will fit
if (strlen(modelLabelCsv) + label.size() + 1 < sizeof(models[index].labels)-1) {
QStringList modelLabels = QString::fromUtf8(modelLabelCsv).split(',', Qt::SkipEmptyParts);
QStringList modelLabels = QString::fromLatin1(modelLabelCsv).split(',', Qt::SkipEmptyParts);
if (modelLabels.indexOf(label) == -1) {
modelLabels.append(label);
strcpy(models[index].labels, QString(modelLabels.join(',')).toUtf8().data());
strcpy(models[index].labels, QString(modelLabels.join(',')).toLatin1().data());
return true;
}
}
@@ -246,10 +246,10 @@ bool RadioData::removeLabelFromModel(int index, QString label)
{
if ((unsigned int)index >= models.size()) return false;
QStringList lbls = fromCSV(QString::fromUtf8(models[index].labels));
QStringList lbls = fromCSV(QString::fromLatin1(models[index].labels));
if (lbls.indexOf(label) >= 0) {
lbls.removeAll(label);
strcpy(models[index].labels, toCSV(lbls).toUtf8().data());
strcpy(models[index].labels, toCSV(lbls).toLatin1().data());
return true;
}
return false;
@@ -342,4 +342,3 @@ AbstractStaticItemModel * RadioData::modelSortOrderItemModel()
mdl->loadItemList();
return mdl;
}