From 1e248abd537c6cf9e897aa206e76834effe1ed99 Mon Sep 17 00:00:00 2001 From: Jens Remus Date: Thu, 6 Jun 2019 17:03:12 +0200 Subject: [PATCH] util_path: Use S_IS*() macros to test type of file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Testing whether a file is a directory or a regular file by masking the struct stat field st_mode with S_IFDIR or S_IFREG is wrong. Depending on the values of the macros S_IF* sockets and symbolic links might erroneously be considered as regular files and block special files as directories. The file type encoded in the struct stat field st_mode is actually an enumeration. To test whether a file is a directory or a regular file either extract the file type from st_mode using the mask S_IFMT and compare it against S_IFDIR or S_IFREG or simply use the macros S_ISDIR() and S_ISREG(). Fixes: b627b8d8e1ab ("Initial s390-tools-2.0.0 import") Signed-off-by: Jens Remus Reviewed-by: Jan Hoeppner Signed-off-by: Jan Höppner --- libutil/util_path.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libutil/util_path.c b/libutil/util_path.c index a4577e55..8ec19f61 100644 --- a/libutil/util_path.c +++ b/libutil/util_path.c @@ -160,7 +160,7 @@ bool util_path_is_reg_file(const char *fmt, ...) rc = false; goto free_str; } - rc = (sb.st_mode & S_IFREG) ? true : false; + rc = S_ISREG(sb.st_mode) ? true : false; free_str: free(path); return rc; @@ -189,7 +189,7 @@ bool util_path_is_dir(const char *fmt, ...) rc = false; goto free_str; } - rc = (sb.st_mode & S_IFDIR) ? true : false; + rc = S_ISDIR(sb.st_mode) ? true : false; free_str: free(path); return rc;