mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-13 15:39:05 +01:00
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.
This commit is contained in:
@@ -14,22 +14,21 @@
|
||||
std::string SysErrorString(int err)
|
||||
{
|
||||
char buf[256];
|
||||
buf[0] = 0;
|
||||
/* Too bad there are three incompatible implementations of the
|
||||
* thread-safe strerror. */
|
||||
const char *s;
|
||||
const char *s = nullptr;
|
||||
#ifdef WIN32
|
||||
s = buf;
|
||||
if (strerror_s(buf, sizeof(buf), err) != 0)
|
||||
buf[0] = 0;
|
||||
if (strerror_s(buf, sizeof(buf), err) == 0) s = buf;
|
||||
#else
|
||||
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
|
||||
s = strerror_r(err, buf, sizeof(buf));
|
||||
#else /* POSIX variant always returns message in buffer */
|
||||
s = buf;
|
||||
if (strerror_r(err, buf, sizeof(buf)))
|
||||
buf[0] = 0;
|
||||
if (strerror_r(err, buf, sizeof(buf)) == 0) s = buf;
|
||||
#endif
|
||||
#endif
|
||||
return strprintf("%s (%d)", s, err);
|
||||
if (s != nullptr) {
|
||||
return strprintf("%s (%d)", s, err);
|
||||
} else {
|
||||
return strprintf("Unknown error (%d)", err);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user