Whamcloud - gitweb
LU-16980 build: fix gcc-12 [-Werror=format-truncation=] error 65/51765/3
authorJian Yu <yujian@whamcloud.com>
Mon, 31 Jul 2023 09:03:46 +0000 (02:03 -0700)
committerOleg Drokin <green@whamcloud.com>
Mon, 7 Aug 2023 03:50:34 +0000 (03:50 +0000)
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 <yujian@whamcloud.com>
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/51765
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Timothy Day <timday@amazon.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
lnet/utils/lnetconfig/liblnetconfig.c
lustre/tests/statx.c
lustre/utils/lfs_project.c

index ff90496..15d9879 100644 (file)
@@ -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);
 
index 93466f1..7a98b33 100644 (file)
@@ -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');
index 83fe428..8e03afc 100644 (file)
@@ -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)