Skip to content

Commit

Permalink
lib: PAIR_LOOKUP(): Print int value instead of "???"
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Petr Vorel <[email protected]>
  • Loading branch information
metan-ucw committed Jan 14, 2025
1 parent db2e34c commit 21afd7d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/tests/tst_strsig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions lib/tst_res.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 21afd7d

Please sign in to comment.