From 21afd7d9b0d7019798760e6ff52de5af0f378942 Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Tue, 14 Jan 2025 10:17:23 +0100 Subject: [PATCH] lib: PAIR_LOOKUP(): Print int value instead of "???" When there was no mapping found the PAIR_LOOKUP() returned "???" which wasn't really helpful. This commit changes the macro to print the integer value and return a pointer to a static buffer with the value instead. This means that functions such as tst_strsig() and tst_strerrno() will now print a numerical value instead of "???" when there is no mapping defined. Signed-off-by: Cyril Hrubis Reviewed-by: Petr Vorel --- lib/tests/tst_strsig.c | 1 + lib/tst_res.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/tests/tst_strsig.c b/lib/tests/tst_strsig.c index 9a5ca80aaae..ed5be3f003e 100644 --- a/lib/tests/tst_strsig.c +++ b/lib/tests/tst_strsig.c @@ -29,6 +29,7 @@ int TST_TOTAL = 1; int main(void) { + fprintf(stderr, "0 = %s\n", tst_strsig(0)); fprintf(stderr, "SIGKILL = %s\n", tst_strsig(SIGKILL)); fprintf(stderr, "SIGALRM = %s\n", tst_strsig(SIGALRM)); return 0; diff --git a/lib/tst_res.c b/lib/tst_res.c index 7c66d2f6c5d..f50c07271f5 100644 --- a/lib/tst_res.c +++ b/lib/tst_res.c @@ -141,11 +141,14 @@ struct pair { #define PAIR(def) [def] = {.name = #def, .val = def}, #define STRPAIR(key, value) [key] = {.name = value, .val = key}, -#define PAIR_LOOKUP(pair_arr, idx) do { \ - if (idx < 0 || (size_t)idx >= ARRAY_SIZE(pair_arr) || \ - pair_arr[idx].name == NULL) \ - return "???"; \ - return pair_arr[idx].name; \ +#define PAIR_LOOKUP(pair_arr, idx) do { \ + static char pair_str_buf__[16]; \ + if (idx < 0 || (size_t)idx >= ARRAY_SIZE(pair_arr) || \ + pair_arr[idx].name == NULL) { \ + snprintf(pair_str_buf__, sizeof(pair_str_buf__), "%i", idx); \ + return pair_str_buf__; \ + } \ + return pair_arr[idx].name; \ } while (0) const char *strttype(int ttype)