logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 04a855e7c02241321787e07ad41870ac484b79f0
parent 7c36b3ccf8480f9c0244f5f4658a9b9b95638286
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon,  2 Sep 2024 19:18:42 +0200

cmd/pathchk: Fix off-by-one due to PATH_MAX including terminating NULL

Diffstat:

Mcmd/pathchk.c9++++++---
Mtest-cmd/pathchk.t4++--
2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/cmd/pathchk.c b/cmd/pathchk.c @@ -40,7 +40,7 @@ int main(int argc, char *argv[]) { bool opt_P = false, opt_p = false; - size_t path_max = PATH_MAX; + size_t path_max = PATH_MAX - 1; size_t name_max = NAME_MAX; int c = -1; @@ -53,7 +53,7 @@ main(int argc, char *argv[]) break; case 'p': opt_p = true; - path_max = _POSIX_PATH_MAX; + path_max = _POSIX_PATH_MAX - 1; name_max = _POSIX_NAME_MAX; break; case ':': @@ -89,6 +89,7 @@ main(int argc, char *argv[]) err = 1; } + // PATH_MAX includes terminating NULL if(len > path_max) { fprintf(stderr, @@ -108,6 +109,8 @@ main(int argc, char *argv[]) fprintf(stderr, "pathchk: Path component starts with an hyphen: %s\n", p); err = 1; } + + // NAME_MAX doesn't includes terminating NULL size_t name_len = strlen(p); if(name_len > name_max) { @@ -126,7 +129,7 @@ main(int argc, char *argv[]) if(access(path, F_OK) < 0 && errno != ENOENT) { fprintf(stderr, - "pathchk: Error checking while checking '%s' against filesystem: %s\n", + "pathchk: Error while checking '%s' against filesystem: %s\n", path, strerror(errno)); errno = 0; diff --git a/test-cmd/pathchk.t b/test-cmd/pathchk.t @@ -24,8 +24,8 @@ Thanks glibc for the broken getconf pathchk: Path \([0-9]+ octets\) is over the maximum size \([0-9]+ octets\): s/+ (re) [1] - $ pathchk -p "$(printf "%*s" "${POSIX_PATH_MAX}" | tr ' ' /)" - $ pathchk -p "$(printf "s%*s" "${POSIX_PATH_MAX}" | tr ' ' /)" + $ pathchk -p "$(printf "%*s" "$(( ${POSIX_PATH_MAX} - 1 ))" | tr ' ' /)" + $ pathchk -p "$(printf "s%*s" "$(( ${POSIX_PATH_MAX} - 1 ))" | tr ' ' /)" pathchk: Path \([0-9]+ octets\) is over the maximum size \([0-9]+ octets\): s/+ (re) [1]