Skip to content

Commit 718da30

Browse files
committed
util: Refactor SysErrorString logic
Deduplicate code and error checks by making sure `s` stays `nullptr` in case of error. Return "Unknown error" instead of an empty string in this case.
1 parent e7f2f77 commit 718da30

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/util/syserror.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@
1414
std::string SysErrorString(int err)
1515
{
1616
char buf[256];
17-
buf[0] = 0;
1817
/* Too bad there are three incompatible implementations of the
1918
* thread-safe strerror. */
20-
const char *s;
19+
const char *s = nullptr;
2120
#ifdef WIN32
22-
s = buf;
23-
if (strerror_s(buf, sizeof(buf), err) != 0)
24-
buf[0] = 0;
21+
if (strerror_s(buf, sizeof(buf), err) == 0) s = buf;
2522
#else
2623
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
2724
s = strerror_r(err, buf, sizeof(buf));
2825
#else /* POSIX variant always returns message in buffer */
29-
s = buf;
30-
if (strerror_r(err, buf, sizeof(buf)))
31-
buf[0] = 0;
26+
if (strerror_r(err, buf, sizeof(buf)) == 0) s = buf;
3227
#endif
3328
#endif
34-
return strprintf("%s (%d)", s, err);
29+
if (s != nullptr) {
30+
return strprintf("%s (%d)", s, err);
31+
} else {
32+
return strprintf("Unknown error (%d)", err);
33+
}
3534
}

0 commit comments

Comments
 (0)