Skip to content

Commit 8ca303b

Browse files
committed
fix(deepin-os-release): filter RDRAND warnings and improve error handling
- Filter out known RDRAND-related WARNING lines from stderr to prevent pollution of stdout when stdout and stderr are merged by CMake. - Add missing <cstdlib> header for free(). - Close pipe file descriptor when fdopen fails to avoid potential deadlock. - Wrap write() in a retry loop to handle EINTR and short writes. - Update SPDX copyright year to 2026.
1 parent 4fccd4d commit 8ca303b

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

tools/deepin-os-release/main.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2017-2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -15,6 +15,7 @@
1515
#include <stdio.h>
1616
#include <unistd.h>
1717
#include <cstring>
18+
#include <cstdlib>
1819

1920
DCORE_USE_NAMESPACE
2021

@@ -28,16 +29,31 @@ class StderrFilterThread : public QThread
2829
void run() override
2930
{
3031
FILE *readEnd = fdopen(m_readFd, "r");
31-
if (!readEnd) return;
32+
if (!readEnd) {
33+
close(m_readFd); // Close unused file descriptors
34+
return;
35+
}
3236

3337
char *line = nullptr;
3438
size_t len = 0;
3539
while (getline(&line, &len, readEnd) != -1) {
3640
if (strstr(line, "WARNING: CPU random generator seem to be failing") ||
3741
strstr(line, "WARNING: RDRND generated:")) {
38-
continue; // 丢弃该行
42+
continue; // Discard this row
43+
}
44+
// Use a loop to ensure complete writing
45+
size_t total = 0;
46+
size_t remaining = strlen(line);
47+
const char *buf = line;
48+
while (total < remaining) {
49+
ssize_t written = write(m_origStderr, buf + total, remaining - total);
50+
if (written < 0) {
51+
if (errno == EINTR)
52+
continue; // Interrupted by signal, retry
53+
break; // Other errors, abandon writing
54+
}
55+
total += written;
3956
}
40-
write(m_origStderr, line, strlen(line));
4157
}
4258
free(line);
4359
fclose(readEnd);
@@ -61,7 +77,7 @@ void printDistributionOrgInfo(DSysInfo::OrgType type) {
6177

6278
int main(int argc, char *argv[])
6379
{
64-
// 设置过滤器
80+
// Set filter
6581
int origStderr = dup(STDERR_FILENO);
6682
int pipefd[2];
6783
pipe(pipefd);
@@ -167,8 +183,8 @@ int main(int argc, char *argv[])
167183
}
168184
}
169185

170-
// 清理
171-
dup2(origStderr, STDERR_FILENO); // 恢复 stderr
186+
// clean
187+
dup2(origStderr, STDERR_FILENO); // Restore stderr
172188
close(origStderr);
173189

174190
filterThread->wait();

0 commit comments

Comments
 (0)