logo

utils-std

Collection of commonly available Unix tools
commit: c8db7df93df9b7c25c3d165e49870348bea0f836
parent 58262feec89e4734b14592149a302fb21b62d01e
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 14 Mar 2024 03:07:48 +0100

lib/mode: Improve error message for [89]

Diffstat:

Mlib/mode.c9+++++++--
Mtest-cmd/chmod2+-
Mtest-lib/mode.c4++--
3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/lib/mode.c b/lib/mode.c @@ -114,6 +114,11 @@ new_mode(const char *mode, mode_t old, const char **errstr) { if(mode[i] < '0' || mode[i] > '7') { + if(mode[i] == '8' || mode[i] == '9') + { + *errstr = "contains digit outside of [0-7]"; + return old; + } symbolic = true; break; } @@ -134,13 +139,13 @@ new_mode(const char *mode, mode_t old, const char **errstr) if(new < 0) { - *errstr = "mode can't be negative"; + *errstr = "can't be negative"; return old; } if(new > 07777) { - *errstr = "mode can't be higher than 0o7777"; + *errstr = "can't be higher than 0o7777"; return old; } diff --git a/test-cmd/chmod b/test-cmd/chmod @@ -14,7 +14,7 @@ octal_body() { atf_check -o "inline:chmod: Permissions changed from 00000/---------- to 00444/-r--r--r-- for '${TMPDIR}/chmod_octal'\n" ../cmd/chmod -v 0444 "${TMPDIR}/chmod_octal" atf_check -o "inline:chmod: Permissions changed from 00444/-r--r--r-- to 00777/-rwxrwxrwx for '${TMPDIR}/chmod_octal'\n" ../cmd/chmod -v 0777 "${TMPDIR}/chmod_octal" - atf_check -s not-exit:0 -e "inline:chmod: Failed parsing '0888': syntax error, got invalid character\n" ../cmd/chmod -v 0888 "${TMPDIR}/chmod_octal" + atf_check -s not-exit:0 -e "inline:chmod: Failed parsing '0888': contains digit outside of [0-7]\n" ../cmd/chmod -v 0888 "${TMPDIR}/chmod_octal" } octal_cleanup() { atf_check chmod u=rw "${TMPDIR}/chmod_octal" diff --git a/test-lib/mode.c b/test-lib/mode.c @@ -15,7 +15,7 @@ #define TEST_MODE(str, old, expect) \ res = new_mode(str, old, &errstr); \ CHECK_EQ_MODE(expect, res); \ - ATF_CHECK(errstr == NULL) + ATF_CHECK_MSG(errstr == NULL, "errstr: %s", errstr) #define TEST_ERRSTR(str, old, error) \ res = new_mode(str, old, &errstr); \ @@ -265,7 +265,7 @@ ATF_TC_BODY(syntax, tc) TEST_ERRSTR("foo", 0, "syntax error, got invalid character"); - TEST_ERRSTR("77777", 0, "mode can't be higher than 0o7777"); + TEST_ERRSTR("77777", 0, "can't be higher than 0o7777"); TEST_ERRSTR("-0", 0, "syntax error, got invalid character"); }