]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/ kr_strerror(): unify / cover more error codes
authorVladimír Čunát <vladimir.cunat@nic.cz>
Sun, 25 May 2025 08:17:44 +0000 (10:17 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Sun, 25 May 2025 08:17:44 +0000 (10:17 +0200)
We often propagate errors from Knot libs...

lib/defines.h
lib/utils.c

index c4c8d4090cfd307562d4a4d76d8062f820d44c0b..1ee0ac4e0cdd981aa41ab99e9dfd3346a0ca3be5 100644 (file)
@@ -5,7 +5,8 @@
 #pragma once
 
 #include <errno.h>
-#include <libknot/errcode.h>
+#include <stdlib.h>
+#include <libknot/error.h>
 #include <libknot/dname.h>
 #include <libknot/rrset.h>
 #include <libknot/version.h>
@@ -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. */
index a2ebceaf1da11f39d7ed76e07d5983416269cb41..5bd476cdb26a55721f74b54afb07562e92e74fc7 100644 (file)
@@ -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;