From 04734e0ebc309f18dde0275323970dff98c928e8 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 26 Oct 2021 10:47:13 +0200 Subject: [PATCH] Define ug_strerror for thread-safe strerror abstraction --- src/audio/export.c | 3 ++- src/debug.cpp | 15 ++------------- src/utils/misc.c | 17 +++++++++++++++++ src/utils/misc.h | 2 ++ src/video_capture/v4l2.c | 8 ++------ 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/audio/export.c b/src/audio/export.c index aa52c2cfb..1e2cf7d7b 100644 --- a/src/audio/export.c +++ b/src/audio/export.c @@ -51,6 +51,7 @@ #include "audio/wav_writer.h" #include "debug.h" #include "export.h" +#include "utils/misc.h" // ug_strerror #include "utils/ring_buffer.h" #define CACHE_SECONDS 10 @@ -109,7 +110,7 @@ static void *audio_export_thread(void *arg) const int sample_size = s->saved_format.bps * s->saved_format.ch_count; int rc = wav_writer_write(s->wav, size / sample_size, data); if (rc != 0) { - fprintf(stderr, "[Audio export] Problem writing audio samples: %s\n", strerror(-rc)); + fprintf(stderr, "[Audio export] Problem writing audio samples: %s\n", ug_strerror(-rc)); } free(data); diff --git a/src/debug.cpp b/src/debug.cpp index 5532d5157..dc5546065 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -58,6 +58,7 @@ #include "host.h" #include "rang.hpp" #include "utils/color_out.h" +#include "utils/misc.h" // ug_strerror using std::string; using std::unordered_map; @@ -130,19 +131,7 @@ void log_msg(int level, const char *format, ...) */ void log_perror(int level, const char *msg) { - constexpr int strerror_buf_len = 1024; - std::array strerror_buf; - const char *errstring; -#ifdef _WIN32 - strerror_s(strerror_buf.data(), strerror_buf.size(), errno); // C11 Annex K (bounds-checking interfaces) - errstring = strerror_buf.data(); -#elif ! defined _POSIX_C_SOURCE || (_POSIX_C_SOURCE >= 200112L && ! _GNU_SOURCE) - strerror_r(errno, strerror_buf.data(), strerror_buf.size()); // XSI version - errstring = strerror_buf.data(); -#else // GNU strerror_r version - errstring = strerror_r(errno, strerror_buf.data(), strerror_buf_len); -#endif - log_msg(level, "%s: %s\n", msg, errstring); + log_msg(level, "%s: %s\n", msg, ug_strerror(errno)); } /** diff --git a/src/utils/misc.c b/src/utils/misc.c index 72ef6bcd5..9835caabb 100644 --- a/src/utils/misc.c +++ b/src/utils/misc.c @@ -47,6 +47,8 @@ #include "debug.h" #include "utils/misc.h" +#define STRERROR_BUF_LEN 1024 + int clampi(long long val, int lo, int hi) { if (val < lo) { return lo; @@ -305,3 +307,18 @@ size_t urldecode(char *out, size_t max_len, const char *in) return len; } + +const char *ug_strerror(int errnum) +{ + static _Thread_local char strerror_buf[STRERROR_BUF_LEN]; + const char *errstring = strerror_buf; +#ifdef _WIN32 + strerror_s(strerror_buf, sizeof strerror_buf, errnum); // C11 Annex K (bounds-checking interfaces) +#elif ! defined _POSIX_C_SOURCE || (_POSIX_C_SOURCE >= 200112L && ! _GNU_SOURCE) + strerror_r(errnum, strerror_buf, sizeof strerror_buf); // XSI version +#else // GNU strerror_r version + errstring = strerror_r(errnum, strerror_buf, sizeof strerror_buf); +#endif + + return errstring; +} diff --git a/src/utils/misc.h b/src/utils/misc.h index ff0101d5d..6eacaf201 100644 --- a/src/utils/misc.h +++ b/src/utils/misc.h @@ -68,6 +68,8 @@ int urlencode_rfc3986_eval(int c); size_t urlencode(char *out, size_t max_len, const char *in, int (*eval_pass)(int c), bool space_plus_replace); size_t urldecode(char *out, size_t max_len, const char *in); +const char *ug_strerror(int errnum); + /** * @brief Creates FourCC word * diff --git a/src/video_capture/v4l2.c b/src/video_capture/v4l2.c index 7db4cb6ac..bd06bd77e 100644 --- a/src/video_capture/v4l2.c +++ b/src/video_capture/v4l2.c @@ -61,12 +61,9 @@ #include "lib_common.h" #include "tv.h" #include "utils/list.h" +#include "utils/misc.h" // ug_strerror #include "video.h" -#ifndef _GNU_SOURCE -#error "GNU variant of strerror_r is used below!" -#endif - /* prototypes of functions defined in this module */ static void show_help(void); static void print_fps(int fd, struct v4l2_frmivalenum *param); @@ -224,9 +221,8 @@ static void show_help() int fd = open(name, O_RDWR); if (fd == -1) { if (errno != ENOENT) { - char errbuf[1024]; log_msg(LOG_LEVEL_WARNING, MOD_NAME "Unable to open input device %s: %s\n", - name, strerror_r(errno, errbuf, sizeof errbuf)); + name, ug_strerror(errno)); } continue; }