logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: eaf123c2451e7ad2b5da7b12561a468d200172ac
parent 5655c484f39f374f46f38a788bbf737ea07fc5e1
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue,  1 Mar 2022 20:17:43 +0100

bin/errno: Handle error from strerror

Diffstat:

Mbin/errno.c10+++++++++-
Mtest-bin/errno11+++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/bin/errno.c b/bin/errno.c @@ -25,7 +25,15 @@ main(int argc, char *argv[]) return 1; } - if(puts(strerror(err)) <= 0) + errno = 0; + char *msg = strerror(err); + if(errno != 0) + { + perror("errno: strerror"); + return 1; + } + + if(puts(msg) <= 0) { return 1; } diff --git a/test-bin/errno b/test-bin/errno @@ -22,9 +22,20 @@ devfull_body() { atf_check -s exit:1 -o "inline:usage: errno <number>\n" sh -c '../bin/errno 1 >/dev/full' } +atf_test_case einval +einval_body() { + if test -f /usr/include/features.h && grep -q '#define\W__GLIBC__' /usr/include/features.h + then + atf_expect_fail "glibc not setting errno for strerror" + fi + + atf_check -s exit:1 -o "inline:Unknown error -1\n" ../bin/errno -1 +} + atf_init_test_cases() { cd "$(atf_get_srcdir)" || exit 1 atf_add_test_case simple atf_add_test_case noargs atf_add_test_case devfull + atf_add_test_case einval }