Whamcloud - gitweb
LU-15220 utils: avoid gcc-11 -Werror=stringop-truncation warning
authorJian Yu <yujian@whamcloud.com>
Tue, 25 Jan 2022 06:25:34 +0000 (22:25 -0800)
committerAndreas Dilger <adilger@whamcloud.com>
Tue, 25 Jan 2022 15:43:40 +0000 (15:43 +0000)
This patch fixes the following -Werror=stringop-truncation warning:

In function 'hsm_scan_item_alloc',
    inlined from 'hsm_scan_handle_dir' at libhsm_scanner.c:124:10:
libhsm_scanner.c:64:9: error: 'strncpy' output may be truncated
copying 4096 bytes from a string of length 4351 [-Werror=stringop-truncation]
   64 |   strncpy(item->hsi_pathname, pathname, sizeof(item->hsi_pathname));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function 'hsm_scan_item_alloc',
    inlined from 'hsm_scan_process' at libhsm_scanner.c:165:7:
libhsm_scanner.c:64:9: error: 'strncpy' specified bound 4096 equals
destination size [-Werror=stringop-truncation]
   64 |   strncpy(item->hsi_pathname, pathname, sizeof(item->hsi_pathname));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Test-Parameters: trivial testlist=sanity-pcc

Change-Id: I74c6c1de82f18ad2a652034b5952d55d44032b89
Signed-off-by: Jian Yu <yujian@whamcloud.com>
Reviewed-on: https://review.whamcloud.com/46286
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
lustre/utils/libhsm_scanner.c
lustre/utils/liblustreapi_pcc.c

index 54c0345..c5f96eb 100644 (file)
@@ -52,6 +52,13 @@ static int hsm_scan_item_alloc(struct list_head *head,
        struct hsm_scan_item *item;
        int rc;
 
+       if (strlen(pathname) >= PATH_MAX) {
+               rc = -ENAMETOOLONG;
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                           "pathname is too long: %s\n", pathname);
+               return rc;
+       }
+
        item = malloc(sizeof(struct hsm_scan_item));
        if (item == NULL) {
                rc = -ENOMEM;
@@ -61,7 +68,7 @@ static int hsm_scan_item_alloc(struct list_head *head,
        }
 
        item->hsi_depth = depth;
-       strncpy(item->hsi_pathname, pathname, sizeof(item->hsi_pathname));
+       strncpy(item->hsi_pathname, pathname, sizeof(item->hsi_pathname) - 1);
        list_add_tail(&item->hsi_item, head);
 
        return 0;
index 7a37d28..faf8da3 100644 (file)
@@ -426,7 +426,7 @@ int llapi_pcc_state_get(const char *path, struct lu_pcc_state *state)
 
        filename = basename(path_copy);
        state->pccs_namelen = strlen(filename) + 1;
-       strncpy(state->pccs_path, filename, sizeof(state->pccs_path));
+       strncpy(state->pccs_path, filename, sizeof(state->pccs_path) - 1);
 
        rc = llapi_pcc_state_get_fd(fd, state);
        if (rc != 0) {