From c2a3fb6efb854a9e10804f67fef0764042c2d6c5 Mon Sep 17 00:00:00 2001
From: liuzhilin <liuzhilin@uniontech.com>
Date: Fri, 22 Dec 2023 16:54:15 +0800
Subject: [PATCH] LSHW returns a multi-element array when querying memory size

---
 src/dsysinfo.cpp | 36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/dsysinfo.cpp b/src/dsysinfo.cpp
index d3f137b..d8914cc 100644
--- a/src/dsysinfo.cpp
+++ b/src/dsysinfo.cpp
@@ -17,6 +17,7 @@
 #include <QStandardPaths>
 #include <QDateTime>
 #include <QRegularExpression>
+#include <QLoggingCategory>
 #include <qmath.h>
 
 #ifdef Q_OS_LINUX
@@ -33,6 +34,12 @@
 
 DCORE_BEGIN_NAMESPACE
 
+#ifdef QT_DEBUG
+Q_LOGGING_CATEGORY(logSysInfo, "dtk.dsysinfo")
+#else
+Q_LOGGING_CATEGORY(logSysInfo, "dtk.dsysinfo", QtInfoMsg)
+#endif
+
 class Q_DECL_HIDDEN DSysInfoPrivate
 {
 public:
@@ -1087,18 +1094,33 @@ qint64 DSysInfo::memoryInstalledSize()
         }
 
         const QByteArray &lshwInfoJson = lshw.readAllStandardOutput();
-        QJsonArray lshwResultArray = QJsonDocument::fromJson(lshwInfoJson).array();
-        if (!lshwResultArray.isEmpty()) {
-            QJsonValue memoryHwInfo = lshwResultArray.first();
-            QString id = memoryHwInfo.toObject().value("id").toString();
-            Q_ASSERT(id == "memory");
-            siGlobal->memoryInstalledSize = memoryHwInfo.toObject().value("size").toDouble(); // TODO: check "units" is "bytes" ?
+	QJsonParseError error;
+        auto doc = QJsonDocument::fromJson(lshwInfoJson, &error);
+        if (error.error != QJsonParseError::NoError) {
+            qCWarning(logSysInfo(), "parse failed, expect json doc from lshw command");
+            return -1;
+        }
+
+        if (!doc.isArray()) {
+            qCWarning(logSysInfo(), "parse failed, expect array");
+            return -1;
+        }
+
+        QJsonArray lshwResultArray = doc.array();
+        for (const QJsonValue value : lshwResultArray) {
+            QJsonObject obj = value.toObject();
+            if (obj.contains("id") && obj.value("id").toString() == "memory") {
+                siGlobal->memoryInstalledSize = obj.value("size").toDouble(); // TODO: check "units" is "bytes" ?
+                break;
+            }
         }
     }
 
+    Q_ASSERT(siGlobal->memoryInstalledSize > 0);
     return siGlobal->memoryInstalledSize;
-#endif
+#else
     return -1;
+#endif
 }
 
 /*!
-- 
2.39.3

