From 347cf32326a5be282e311f140cdc850bde46176b Mon Sep 17 00:00:00 2001 From: Jian Yu Date: Mon, 31 Jul 2023 02:03:46 -0700 Subject: [PATCH] LU-16980 build: fix gcc-12 [-Werror=format-truncation=] error This patch fixes the following [-Werror=format-truncation=] errors detected by gcc 12: liblnetconfig.c: In function 'open_sysfs_file': liblnetconfig.c:106:49: error: '%s' directive output may be truncated writing up to 127 bytes into a region of size between 1 and 128 [-Werror=format-truncation=] 106 | snprintf(filename, sizeof(filename), "%s%s", | ^~ lfs_project.c: In function 'lfs_project_handle_dir': lfs_project.c:324:50: error: '%s' directive output may be truncated writing up to 255 bytes into a region of size between 1 and 4095 [-Werror=format-truncation=] 324 | snprintf(fullname, PATH_MAX, "%s/%s", pathname, | ^~ statx.c: In function 'do_dir_list': statx.c:1427:58: error: '%s' directive output may be truncated writing up to 255 bytes into a region of size between 1 and 4095 [-Werror=format-truncation=] 1427 | snprintf(fullname, PATH_MAX, "%s/%s", | ^~ Change-Id: I514a1022d879f8b7af89f6ded68e9b453cd11408 Signed-off-by: Jian Yu Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/51765 Reviewed-by: Andreas Dilger Reviewed-by: Timothy Day Reviewed-by: Oleg Drokin Tested-by: jenkins Tested-by: Maloo --- lnet/utils/lnetconfig/liblnetconfig.c | 10 +++++----- lustre/tests/statx.c | 9 +++++++-- lustre/utils/lfs_project.c | 8 ++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lnet/utils/lnetconfig/liblnetconfig.c b/lnet/utils/lnetconfig/liblnetconfig.c index ff90496..15d9879 100644 --- a/lnet/utils/lnetconfig/liblnetconfig.c +++ b/lnet/utils/lnetconfig/liblnetconfig.c @@ -87,12 +87,12 @@ int open_sysfs_file(const char *path, const char *attr, const int mode) { int fd; char filename[LNET_MAX_STR_LEN]; + size_t size = sizeof(filename); + int namelen; - if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN) - return -1; - - snprintf(filename, sizeof(filename), "%s%s", - path, attr); + namelen = snprintf(filename, size, "%s%s", path, attr); + if (namelen >= size) + filename[size - 1] = '\0'; fd = open(filename, mode); diff --git a/lustre/tests/statx.c b/lustre/tests/statx.c index 93466f1..7a98b33 100644 --- a/lustre/tests/statx.c +++ b/lustre/tests/statx.c @@ -1389,6 +1389,8 @@ static int do_dir_list(char const *dirname, unsigned int request_mask, DIR *dir; struct dirent *ent; char fullname[PATH_MAX]; + size_t size = sizeof(fullname); + int namelen; int rc = 0; dir = opendir(dirname); @@ -1424,8 +1426,11 @@ static int do_dir_list(char const *dirname, unsigned int request_mask, rc = -ENAMETOOLONG; continue; } - snprintf(fullname, PATH_MAX, "%s/%s", - dirname, ent->d_name); + namelen = snprintf(fullname, size, "%s/%s", + dirname, ent->d_name); + if (namelen >= size) + fullname[size - 1] = '\0'; + ret = do_statx(fullname, request_mask, flags); if (!ret) putchar('\n'); diff --git a/lustre/utils/lfs_project.c b/lustre/utils/lfs_project.c index 83fe428..8e03afc 100644 --- a/lustre/utils/lfs_project.c +++ b/lustre/utils/lfs_project.c @@ -294,6 +294,8 @@ lfs_project_handle_dir(struct list_head *head, const char *pathname, struct project_handle_control *)) { char fullname[PATH_MAX]; + size_t size = sizeof(fullname); + int namelen; struct dirent *ent; DIR *dir; int ret = 0; @@ -321,8 +323,10 @@ lfs_project_handle_dir(struct list_head *head, const char *pathname, progname, pathname, ent->d_name); continue; } - snprintf(fullname, PATH_MAX, "%s/%s", pathname, - ent->d_name); + namelen = snprintf(fullname, size, "%s/%s", + pathname, ent->d_name); + if (namelen >= size) + fullname[size - 1] = '\0'; rc = func(fullname, phc); if (rc && !ret) -- 1.8.3.1