tree-wide: use ERRNO_NAME almost everywhere

We had errno_to_name() which works for "known" errnos, and returns NULL for
unknown ones, and then ERRNO_NAME which always returns an answer, possibly just
a number as a string, but requires a helper buffer.

It is possible for the kernel to add a new errno. We recently learned that some
architectures define custom errno names. Or for some function to unexpectedly
return a bogus errno value. In almost all cases it's better to print that value
rather than "n/a" or "(null)". So let's use ERRNO_NAME is most error handling
code. Noteably, our code wasn't very good in handling the potential NULL, so
in various places we could print "(null)". Since this is supposed to be used
most of the time, let's shorten the names to ERRNO_NAME/errno_name.

There are a few places where we don't want to use the fallback path, in
particular for D-Bus error names or when saving the error name. Let's rename
errno_to_name() to errno_name_no_fallback() to make the distinction clearer.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-07-29 18:05:20 +02:00
parent 225450d043
commit 858cb6e49e
22 changed files with 216 additions and 215 deletions
+5 -5
View File
@@ -25,8 +25,8 @@ int errno_from_name(const char *name) {
}
#if HAVE_STRERRORNAME_NP
const char* errno_to_name(int id) {
if (id == 0) /* To stay in line with our own impl */
const char* errno_name_no_fallback(int id) {
if (id == 0) /* To stay in line with our implementation below. */
return NULL;
return strerrorname_np(ABS(id));
@@ -34,7 +34,7 @@ const char* errno_to_name(int id) {
#else
# include "errno-to-name.inc"
const char* errno_to_name(int id) {
const char* errno_name_no_fallback(int id) {
if (id < 0)
id = -id;
@@ -45,8 +45,8 @@ const char* errno_to_name(int id) {
}
#endif
const char* errno_name_full(int id, char buf[static ERRNO_NAME_BUF_LEN]) {
const char *a = errno_to_name(id);
const char* errno_name(int id, char buf[static ERRNO_NAME_BUF_LEN]) {
const char *a = errno_name_no_fallback(id);
if (a)
return a;
snprintf(buf, ERRNO_NAME_BUF_LEN, "%d", abs(id));
+3 -3
View File
@@ -3,7 +3,7 @@
#include "forward.h"
const char* errno_to_name(int id) _const_;
const char* errno_name_no_fallback(int id) _const_;
int errno_from_name(const char *name) _pure_;
static inline bool errno_is_valid(int n) {
@@ -12,7 +12,7 @@ static inline bool errno_is_valid(int n) {
#define ERRNO_NAME_BUF_LEN DECIMAL_STR_MAX(int)
/* Like errno_name, but always returns a string. */
const char* errno_name_full(int id, char buf[static ERRNO_NAME_BUF_LEN]);
const char* errno_name(int id, char buf[static ERRNO_NAME_BUF_LEN]);
/* A helper to print the errno "name" or number if name is not defined. */
#define ERRNO_NAME_FULL(errnum) errno_name_full(errnum, (char[ERRNO_NAME_BUF_LEN]){})
#define ERRNO_NAME(errnum) errno_name(errnum, (char[ERRNO_NAME_BUF_LEN]){})