From cc5437746312127aed4e4c8e62091707ec61153c Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 26 Nov 2014 21:21:56 +0100 Subject: Do not call strerror() ...because everything is terrible. strerror() is not documented as having to be thread-safe by POSIX and C11. (Which is pretty much bullshit, because both mandate threads and some form of thread-local storage - so there's no excuse why implementation couldn't implement this in a thread-safe way. Especially with C11 this is ridiculous, because there is no way to use threads and convert error numbers to strings at the same time!) Since we heavily use threads now, we should avoid unsafe functions like strerror(). strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and gives the function different semantics than the POSIX one. It's a bit of work to convince this piece of shit to expose the POSIX standard function, and not the messed up GNU one. strerror_l() is also in POSIX, but only since the 2008 standard, and thus is not widespread. The solution is using avlibc (libavutil, by its official name), which handles the unportable details for us, mostly. We avoid some pain. --- common/common.c | 9 +++++++++ common/common.h | 3 +++ 2 files changed, 12 insertions(+) (limited to 'common') diff --git a/common/common.c b/common/common.c index 449c95be26..4c1f9f327a 100644 --- a/common/common.c +++ b/common/common.c @@ -20,6 +20,7 @@ #include #include +#include #include "talloc.h" #include "misc/bstr.h" @@ -243,3 +244,11 @@ bool mp_append_escaped_string(void *talloc_ctx, bstr *dst, bstr *src) } return false; } + +// Behaves like strerror()/strerror_r(), but is thread- and GNU-safe. +char *mp_strerror_buf(char *buf, size_t buf_size, int errnum) +{ + // This handles the nasty details of calling the right function for us. + av_strerror(AVERROR(errnum), buf, buf_size); + return buf; +} diff --git a/common/common.h b/common/common.h index 3e5ee1190f..e3411427a5 100644 --- a/common/common.h +++ b/common/common.h @@ -89,4 +89,7 @@ bool mp_append_escaped_string_noalloc(void *talloc_ctx, struct bstr *dst, bool mp_append_escaped_string(void *talloc_ctx, struct bstr *dst, struct bstr *src); +char *mp_strerror_buf(char *buf, size_t buf_size, int errnum); +#define mp_strerror(e) mp_strerror_buf((char[80]){0}, 80, e) + #endif /* MPLAYER_MPCOMMON_H */ -- cgit v1.2.3