1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-19 11:35:12 +00:00

Use portable version of strerror_r

There are 2 variants of strerror_r, GNU extension and a POSIX variant.
Use the more portable POSIX variant.

Also fix a potensial stack-use-after-return bug, instead of relying on
undefined behavoir in GNU libc implementation. The strerror_r manpage
says:

> This may be either a pointer to a string that the function stores in
> buf, or a pointer to some (immutable) static string (in which case buf
> is unused).

So it does not guarantee that it returns a pointer to some (immutable)
static string. It might return a pointer to "buf", in which case we get
a stack-use-after-return bug.

We fix this by declaring a thread local static buffer which we always
return.
This commit is contained in:
Natanael Copa 2015-07-23 13:58:02 +02:00
parent 98b063866f
commit 476f84cf59

View File

@ -26,8 +26,14 @@
#include "c++wrap.hh"
#include <unistd.h>
/* force use of POSIX strerror_r instead of non-portable GNU specific */
#ifdef _GNU_SOURCE
#undef _GNU_SOURCE
#endif
#include <string.h>
#if !defined(HAVE_PIPE2) || !defined(HAVE_O_CLOEXEC)
#include <fcntl.h>
@ -62,8 +68,10 @@ namespace {
std::string strerror_r(int errnum)
{
char buf[100];
return strerror_r(errnum, buf, sizeof buf);
static thread_local char buf[100];
if (strerror_r(errnum, buf, sizeof buf) != 0)
snprintf(buf, sizeof buf, "Unknown error %i", errnum);
return buf;
}
std::pair<int, int> pipe2(int flags)