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
51 changes: 1 addition & 50 deletions dconfig-center/common/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
#include <QList>
#include <QDir>
#include <QDirIterator>
#include <QDBusArgument>

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

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusArgument> not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDBusArgument> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QJsonDocument>

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

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QJsonDocument> not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QJsonDocument> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QJsonValue>

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

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QJsonValue> not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QJsonValue> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QJsonObject>
#include <QJsonArray>
#include <DConfigFile>

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

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DConfigFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <DConfigFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dstandardpaths.h>

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

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dstandardpaths.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dstandardpaths.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QCoreApplication>

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

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCoreApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QCoreApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QTranslator>

using ResourceId = QString;
Expand Down Expand Up @@ -211,20 +209,12 @@

static QString qvariantToString(const QVariant &v)
{
const QJsonValue jsonVal = QJsonValue::fromVariant(v);
if (jsonVal.isString()) {
return QString("\"%1\"").arg(jsonVal.toString());
}
const auto &doc = QJsonDocument::fromVariant(v);
return doc.isNull() ? v.toString() : doc.toJson();
}

static QString qvariantToStringCompact(const QVariant &v)
{
const QJsonValue jsonVal = QJsonValue::fromVariant(v);
if (jsonVal.isString()) {
return QString("\"%1\"").arg(jsonVal.toString());
}
const auto &doc = QJsonDocument::fromVariant(v);
return doc.isNull() ? v.toString() : doc.toJson(QJsonDocument::Compact);
}
Expand All @@ -233,51 +223,12 @@
{
QJsonParseError error;
const auto &doc = QJsonDocument::fromJson(s.toUtf8(), &error);
if (error.error == QJsonParseError::NoError) {
// 成功解析为对象或数组
if (error.error == QJsonParseError::NoError)
return doc.toVariant();
}

// 尝试解析为单个 JSON 值(字符串、数字、布尔、null)
// 通过包装在数组中来解析
QString wrapped = QString("[%1]").arg(s);
const auto &arrayDoc = QJsonDocument::fromJson(wrapped.toUtf8(), &error);
if (error.error == QJsonParseError::NoError) {
QJsonArray array = arrayDoc.array();
if (!array.isEmpty()) {
return array.first().toVariant();
}
}

// 如果都失败了,返回原字符串
return s;
}

static bool isValidTextJsonValue(const QString &s)
{
QString trimmed = s.trimmed();
if (trimmed.isEmpty()) {
return false;
}

QJsonParseError error;

// 首先尝试解析为 JSON 文档(对象或数组)
QJsonDocument::fromJson(trimmed.toUtf8(), &error);
if (error.error == QJsonParseError::NoError) {
return true;
}

// 如果不是对象或数组,尝试解析为单个 JSON 值(字符串、数字、布尔值、null)
// 通过将其包装在数组中来验证
QString wrapped = QString("[%1]").arg(trimmed);
QJsonDocument::fromJson(wrapped.toUtf8(), &error);

// 如果包装后能成功解析,说明是有效的 JSON 值
return error.error == QJsonParseError::NoError;
}

static QString qvariantToCmd(const QVariant &v)

Check warning on line 231 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 231 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
4 changes: 0 additions & 4 deletions dconfig-center/dde-dconfig-editor/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,6 @@ void KeyContent::setBaseInfo(ConfigGetter *getter, const QString &language)
auto widget = new DLineEdit(this);
widget->setEnabled(canWrite);
connect(widget, &DLineEdit::editingFinished, widget, [this, widget](){
if (!isValidTextJsonValue(widget->text())) {
qWarning() << "invalid json value" << widget->text();
return;
}
widget->clearFocus();
emit valueChanged(stringToQVariant(widget->text()));
});
Expand Down
4 changes: 0 additions & 4 deletions dconfig-center/dde-dconfig-editor/oemdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,6 @@ QWidget *OEMDialog::getItemWidget(ConfigGetter *getter, DStandardItem *item)
widget->setText(qvariantToString(v));
widget->setEnabled(canWrite);
connect(widget, &DLineEdit::textChanged, widget, [this, item](const QString &text){
if (!isValidTextJsonValue(text)) {
qWarning() << "invalid json value" << text;
return;
}
item->setData(stringToQVariant(text), ValueRole);
treeItemChanged(item);
});
Expand Down
6 changes: 1 addition & 5 deletions dconfig-center/dde-dconfig/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ int CommandManager::getCommand()
#endif
outpuSTD(QString::number(result.toDouble()));
} else {
outpuSTD(qvariantToString(result));
outpuSTD(QString("\"%1\"").arg(qvariantToString(result)));
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Wrapping qvariantToString in quotes without escaping introduces malformed output for strings containing quotes or newlines.

With the new behavior:

  • qvariantToString(result) now returns the raw string (no JSON-style escaping).
  • This call wraps that raw value in "%1" without escaping.

So for a value like foo"bar, the output becomes:

"foo"bar"

which is not a valid single quoted string and will break consumers expecting JSON-like or shell-safe output. This is a regression from the previous implementation, where QJsonValue::fromVariant handled quoting/escaping.

If the intent is JSON-like output, the escaping/quoting should live in one place only, e.g.:

  • Make qvariantToString return a proper JSON string literal for string variants, and call it directly here, or
  • Add a helper dedicated to producing correctly escaped/quoted CLI output instead of wrapping the raw string locally.

}
} else if (method == "name") {
QString result = manager->displayName(key, language);
Expand Down Expand Up @@ -292,10 +292,6 @@ int CommandManager::setCommand()
#endif
manager->setValue(key, value.toDouble());
} else {
if (!isValidTextJsonValue(value)) {
outpuSTDError(QString("the value:[%1] is not a valid json text.").arg(value));
return 1;
}
manager->setValue(key, stringToQVariant(value));
}
} else {
Expand Down
1 change: 0 additions & 1 deletion dconfig-center/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ list(APPEND SOURCES
ut_dconfigconn.cpp
ut_dconfigrefmanager.cpp
ut_dconfigserver.cpp
ut_helper.cpp
)

ADD_EXECUTABLE(dconfigtest main.cpp ${HEADERS} ${SOURCES} ${DCONFIG_DBUS_XML} data.qrc)
Expand Down
4 changes: 1 addition & 3 deletions dconfig-center/tests/ut_dconfigserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ TEST_F(ut_DConfigServer, acquireManagerGeneric) {
}

TEST_F(ut_DConfigServer, removeUserData) {
GTEST_SKIP() << "Skipping removeUserData test for now";
// 测试用户ID
const uint testUid1 = 1001;
const uint testUid2 = 1002;
Expand Down Expand Up @@ -255,7 +254,6 @@ TEST_F(ut_DConfigServer, removeUserData) {
}

TEST_F(ut_DConfigServer, removeUserDataMultipleResources) {
GTEST_SKIP() << "Skipping removeUserData test for now";
// 测试删除用户数据时涉及多个资源的情况
const uint testUid = 1003;

Expand Down Expand Up @@ -412,7 +410,7 @@ TEST_F(ut_DConfigServer, removeUserDataFiles) {
TEST_F(ut_DConfigServer, removeUserDataWithSubpath) {
// 测试带有子路径的配置删除
const uint testUid = 1006;
const QString subpath = "/test/subdir";
const QString subpath = "test/subdir";

// 创建带有子路径的连接
auto path = server->acquireManagerV2(testUid, APP_ID, FILE_NAME, subpath).path();
Expand Down
Loading
Loading