From ba47e1936f0076bcb3da5b574cdb2a1abf96dde9 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Sat, 28 Sep 2024 13:41:46 +0200 Subject: [PATCH] [uwac] add strerror_r detection and use --- uwac/CMakeLists.txt | 3 +++ uwac/libuwac/uwac-display.c | 3 ++- uwac/libuwac/uwac-input.c | 5 ++++- uwac/libuwac/uwac-utils.h | 15 +++++++++++++++ uwac/templates/config.h.in | 1 + 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/uwac/CMakeLists.txt b/uwac/CMakeLists.txt index 362e0490a..3f1f80f04 100644 --- a/uwac/CMakeLists.txt +++ b/uwac/CMakeLists.txt @@ -49,6 +49,9 @@ option(UWAC_HAVE_PIXMAN_REGION "Use PIXMAN or FreeRDP for region calculations" " set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/) include(CommonConfigOptions) +include(CheckFunctionExists) +check_function_exists(strerror_r UWAC_HAVE_STRERROR_R) + # Check for cmake compatibility (enable/disable features) include(FindFeature) diff --git a/uwac/libuwac/uwac-display.c b/uwac/libuwac/uwac-display.c index 070edc515..9bc602aa5 100644 --- a/uwac/libuwac/uwac-display.c +++ b/uwac/libuwac/uwac-display.c @@ -402,8 +402,9 @@ UwacDisplay* UwacOpenDisplay(const char* name, UwacReturnCode* err) if (ret->display == NULL) { + char buffer[256] = { 0 }; (void)fprintf(stderr, "failed to connect to Wayland display %s: %s\n", name, - strerror(errno)); + uwac_strerror(errno, buffer, sizeof(buffer))); *err = UWAC_ERROR_UNABLE_TO_CONNECT; goto out_free; } diff --git a/uwac/libuwac/uwac-input.c b/uwac/libuwac/uwac-input.c index dc224f7ae..2234eba88 100644 --- a/uwac/libuwac/uwac-input.c +++ b/uwac/libuwac/uwac-input.c @@ -75,8 +75,11 @@ static struct wl_buffer* create_pointer_buffer(UwacSeat* seat, const void* src, wl_shm_pool_destroy(pool); if (munmap(data, size) < 0) + { + char buffer[256] = { 0 }; (void)fprintf(stderr, "%s: munmap(%p, %zu) failed with [%d] %s\n", __func__, data, size, - errno, strerror(errno)); + errno, uwac_strerror(errno, buffer, sizeof(buffer))); + } error_mmap: close(fd); diff --git a/uwac/libuwac/uwac-utils.h b/uwac/libuwac/uwac-utils.h index f5b95fec6..34cfbe1ad 100644 --- a/uwac/libuwac/uwac-utils.h +++ b/uwac/libuwac/uwac-utils.h @@ -24,6 +24,9 @@ #define UWAC_UTILS_H_ #include +#include + +#include #define min(a, b) (a) < (b) ? (a) : (b) @@ -44,4 +47,16 @@ char* xstrdup(const char* s); void* xrealloc(void* p, size_t s); +static inline char* uwac_strerror(int dw, char* dmsg, size_t size) +{ +#ifdef __STDC_LIB_EXT1__ + (void)strerror_s(dw, dmsg, size); +#elif defined(UWAC_HAVE_STRERROR_R) + (void)strerror_r(dw, dmsg, size); +#else + (void)_snprintf(dmsg, size, "%s", strerror(dw)); +#endif + return dmsg; +} + #endif /* UWAC_UTILS_H_ */ diff --git a/uwac/templates/config.h.in b/uwac/templates/config.h.in index 90caf3973..bd2d84e36 100644 --- a/uwac/templates/config.h.in +++ b/uwac/templates/config.h.in @@ -7,5 +7,6 @@ #cmakedefine UWAC_HAVE_SYSLOG_H #cmakedefine UWAC_HAVE_JOURNALD_H #cmakedefine UWAC_HAVE_PIXMAN_REGION +#cmakedefine UWAC_HAVE_STRERROR_R #endif /* UWAC_CONFIG_H */