Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions dconfig-center/common/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,36 @@
return s;
}

static bool validateTextInput(const QString &s, QString &errorMsg)
{
QString trimmed = s.trimmed();

if (trimmed.isEmpty()) {
return true;
}

QChar firstChar = trimmed[0];
QChar lastChar = trimmed[trimmed.length() - 1];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个可能溢出吧,


bool looksLikeJsonObject = (firstChar == '{' && lastChar == '}');
bool looksLikeJsonArray = (firstChar == '[' && lastChar == ']');

if (!looksLikeJsonObject && !looksLikeJsonArray) {
return true;
}

QJsonParseError error;
QJsonDocument::fromJson(trimmed.toUtf8(), &error);

if (error.error == QJsonParseError::NoError) {
return true;
}

errorMsg = QString("Input looks like JSON format but is not valid JSON: %1").arg(error.errorString());
return false;
}

static QString qvariantToCmd(const QVariant &v)

Check warning on line 260 in dconfig-center/common/helper.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'qvariantToCmd' is never used.

Check warning on line 260 in dconfig-center/common/helper.hpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

The function 'qvariantToCmd' is never used.
{
auto stringValue = qvariantToStringCompact(v);
auto jsonValue = QJsonValue::fromVariant(v);
Expand Down
5 changes: 5 additions & 0 deletions dconfig-center/dde-dconfig-editor/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,11 @@ void KeyContent::setBaseInfo(ConfigGetter *getter, const QString &language)
auto widget = new DLineEdit(this);
widget->setEnabled(canWrite);
connect(widget, &DLineEdit::editingFinished, widget, [this, widget](){
QString errorMsg;
if (!validateTextInput(widget->text(), errorMsg)) {
qWarning() << errorMsg;
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When validation fails, this path just logs a warning and returns, but editingFinished has already fired so the user receives no actionable feedback and the displayed text may not match the persisted value. Consider surfacing the error in the UI (dialog/inline error) and/or restoring the previous value and returning focus to the field to prompt correction.

Suggested change
qWarning() << errorMsg;
qWarning() << errorMsg;
const QString message = errorMsg.isEmpty()
? tr("The entered value is invalid.")
: errorMsg;
QMessageBox::warning(widget,
tr("Invalid input"),
message);
widget->setFocus();
widget->selectAll();

Copilot uses AI. Check for mistakes.
return;
}
widget->clearFocus();
emit valueChanged(stringToQVariant(widget->text()));
});
Expand Down
8 changes: 7 additions & 1 deletion dconfig-center/dde-dconfig-editor/oemdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,13 @@ QWidget *OEMDialog::getItemWidget(ConfigGetter *getter, DStandardItem *item)
auto widget = new DLineEdit();
widget->setText(qvariantToString(v));
widget->setEnabled(canWrite);
connect(widget, &DLineEdit::textChanged, widget, [this, item](const QString &text){
connect(widget, &DLineEdit::editingFinished, widget, [this, widget, item](){
QString text = widget->text();
QString errorMsg;
if (!validateTextInput(text, errorMsg)) {
qWarning() << errorMsg;
return;
}
Comment on lines +397 to +400
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On invalid JSON-like input, the handler only logs a warning and returns, leaving the editor with text that looks accepted but is not applied to the underlying item/model. This is easy to miss for users. Consider providing in-UI feedback (e.g., dialog/toast/inline error state) and/or reverting the line edit back to the last valid value so the UI and stored value can’t silently diverge.

Copilot uses AI. Check for mistakes.
item->setData(stringToQVariant(text), ValueRole);
treeItemChanged(item);
});
Expand Down
5 changes: 5 additions & 0 deletions dconfig-center/dde-dconfig/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ int CommandManager::setCommand()
#endif
manager->setValue(key, value.toDouble());
} else {
QString errorMsg;
if (!validateTextInput(value, errorMsg)) {
outpuSTDError(errorMsg);
return 1;
}
manager->setValue(key, stringToQVariant(value));
Comment on lines +296 to 300
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new validation will reject values that are quoted (e.g. "foo"). The query value path prints non-bool/non-double values with surrounding quotes, so users copying that output into set --value may now hit a validation error even when they’re just setting a string. Either avoid quoting the query output for strings, or ensure validateTextInput() accepts quoted strings (or doesn’t attempt to validate them).

Suggested change
if (!validateTextInput(value, errorMsg)) {
outpuSTDError(errorMsg);
return 1;
}
manager->setValue(key, stringToQVariant(value));
QString sanitizedValue = value;
if (!validateTextInput(sanitizedValue, errorMsg)) {
// If validation fails, try again after stripping surrounding quotes (e.g. "foo" -> foo)
const int len = sanitizedValue.size();
if (len >= 2 && sanitizedValue.startsWith(QLatin1Char('"')) && sanitizedValue.endsWith(QLatin1Char('"'))) {
QString unquoted = sanitizedValue.mid(1, len - 2);
errorMsg.clear();
if (!validateTextInput(unquoted, errorMsg)) {
outpuSTDError(errorMsg);
return 1;
}
sanitizedValue = unquoted;
} else {
outpuSTDError(errorMsg);
return 1;
}
}
manager->setValue(key, stringToQVariant(sanitizedValue));

Copilot uses AI. Check for mistakes.
}
} else {
Expand Down
Loading