From 1c04f92be30687b9d3331a6796bf3324ed49f6c9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Sun, 25 May 2025 10:17:44 +0200 Subject: [PATCH] lib/ kr_strerror(): unify / cover more error codes We often propagate errors from Knot libs... --- lib/defines.h | 20 +++++++++++++++++--- lib/utils.c | 2 ++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/defines.h b/lib/defines.h index c4c8d4090..1ee0ac4e0 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -5,7 +5,8 @@ #pragma once #include -#include +#include +#include #include #include #include @@ -34,10 +35,23 @@ typedef unsigned int uint; */ #define kr_ok() 0 /* Mark as cold to mark all branches as unlikely. */ -static inline int KR_COLD kr_error(int x) { +KR_COLD static inline +int kr_error(int x) { return x <= 0 ? x : -x; } -#define kr_strerror(x) strerror(abs(x)) +/** Our strerror() variant, covering at least OS and Knot libs. */ +KR_EXPORT KR_COLD inline +const char * kr_strerror(int e) +{ + return e < KNOT_ERROR_MAX ? knot_strerror(e) : strerror(abs(e)); + /* Inline: we also need it from lua, so that's why there's a non-inline + * instance of this in utils.c (and it's a cold function anyway). + * + * Condition: it's nice to have wider coverage provided by knot_strerror(), + * but that would *redefine* the strings also for some common errors + * like EINVAL, and I'm not sure about that at this point. + */ +} /* We require C11 but want to avoid including the standard assertion header * so we alias it ourselves. */ diff --git a/lib/utils.c b/lib/utils.c index a2ebceaf1..5bd476cdb 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -53,6 +53,8 @@ struct __attribute((packed)) kr_sockaddr_un_key { extern inline uint64_t kr_rand_bytes(unsigned int size); +extern inline const char * kr_strerror(int e); + /* Logging & debugging */ bool kr_dbg_assertion_abort = DBG_ASSERTION_ABORT; int kr_dbg_assertion_fork = DBG_ASSERTION_FORK; -- 2.47.2