Skip to content
Snippets Groups Projects
Commit 984f1eb6 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Insure all builds of safe_strerror() write the string to the passed buf

The GNU_SOURCE build of this function was (sometimes?) just returning the
string and not actually copying it to the passed buf. This is consistent
with the GNU manpage on strerror_r():
"This may be either a pointer to a string that the function stores in buf ..."
but was inconsistent with all the other build types of this function.

Also updated to use strlcpy and write the problematic error number to the
default string (if unknown).
parent bfa0668e
No related branches found
No related tags found
No related merge requests found
Pipeline #5930 failed
......@@ -1123,16 +1123,16 @@ bool terminate_pid(pid_t pid)
/****************************************************************************/
char* safe_strerror(int errnum, char *buf, size_t buflen)
{
strncpy(buf, "Unknown error", buflen);
buf[buflen - 1] = 0;
snprintf(buf, buflen, "Unknown error: %d", errnum);
#if defined(_MSC_VER)
strerror_s(buf, buflen, errnum);
#elif defined(_WIN32) || defined(__EMSCRIPTEN__)
strncpy(buf, strerror(errnum), buflen);
buf[buflen - 1] = 0;
strlcpy(buf, strerror(errnum), buflen);
#elif defined(_GNU_SOURCE)
buf = strerror_r(errnum, buf, buflen);
char* ret = strerror_r(errnum, buf, buflen);
if (ret != buf)
strlcpy(buf, ret, buflen);
#else
strerror_r(errnum, buf, buflen);
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment