Skip to content

Commit 2912925

Browse files
deepin-ci-robot18202781743
authored andcommitted
sync: from linuxdeepin/dtkcore
Synchronize source files from linuxdeepin/dtkcore. Source-pull-request: linuxdeepin/dtkcore#514
1 parent 1f69f7b commit 2912925

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/dsysinfo.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -537,19 +537,21 @@ void DSysInfoPrivate::ensureComputerInfo()
537537

538538
QMap<QString, QString> DSysInfoPrivate::parseInfoFile(QFile &file)
539539
{
540-
char buf[1024];
541-
qint64 lineLength = 0;
542540
QMap<QString, QString> map;
543-
do {
544-
lineLength = file.readLine(buf, sizeof(buf));
545-
QString s(buf);
546-
if (s.contains(':')) {
547-
QStringList list = s.split(':');
548-
if (list.size() == 2) {
549-
map.insert(list.first().trimmed(), list.back().trimmed());
550-
}
551-
}
552-
} while (lineLength >= 0);
541+
542+
qint64 fileSize = file.size();
543+
// 因为此类文件一般不会超过1M大小,超过1M大小大概率出现异常情况
544+
// 避免文件内容异常导致出现问题,对文件大小做了限制
545+
if (fileSize < 1024000) {
546+
QByteArray buff = file.readAll();
547+
map = parseInfoContent(QString::fromLocal8Bit(buff));
548+
} else {
549+
qCWarning(logSysInfo) << "Size is too big, is it broken? File :"
550+
<< file.fileName()
551+
<< ", size :"
552+
<< fileSize;
553+
}
554+
553555
return map;
554556
}
555557

@@ -558,11 +560,11 @@ QMap<QString, QString> DSysInfoPrivate::parseInfoContent(const QString &content)
558560
QMap<QString, QString> map;
559561
QStringList lineContents = content.split("\n");
560562
for (auto lineContent : lineContents) {
561-
if (lineContent.contains(':')) {
562-
QStringList list = lineContent.split(':');
563-
if (list.size() == 2) {
564-
map.insert(list.first().trimmed(), list.back().trimmed());
565-
}
563+
const int index = lineContent.indexOf(':');
564+
if (index != -1) {
565+
const QString key = lineContent.left(index);
566+
const QString value = lineContent.mid(index + 1);
567+
map.insert(key.trimmed(), value.trimmed());
566568
}
567569
}
568570
return map;

0 commit comments

Comments
 (0)