diff options
author | laanwj <126646+laanwj@users.noreply.github.com> | 2022-04-20 19:41:30 +0200 |
---|---|---|
committer | laanwj <126646+laanwj@users.noreply.github.com> | 2022-04-28 10:24:06 +0200 |
commit | 718da302c7b11b375042c3000d421fd93348c199 (patch) | |
tree | a4e6676a45c549164b07576a14790b2a8ecc8d70 /src/util/syserror.cpp | |
parent | e7f2f77756d33c6be9c8998a575b263ff2d39270 (diff) |
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.
Diffstat (limited to 'src/util/syserror.cpp')
-rw-r--r-- | src/util/syserror.cpp | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/src/util/syserror.cpp b/src/util/syserror.cpp index 20f89057fc..d721602bbb 100644 --- a/src/util/syserror.cpp +++ b/src/util/syserror.cpp @@ -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); + } } |