Whamcloud - gitweb
LU-16446 utils: fix issues with llapi_get_lmm_from_path() 91/57391/3
authorFrederick Dilger <fdilger@whamcloud.com>
Thu, 12 Dec 2024 08:11:10 +0000 (01:11 -0700)
committerOleg Drokin <green@whamcloud.com>
Sat, 18 Jan 2025 22:05:01 +0000 (22:05 +0000)
size_t was being used for lmmlen when it should have been ssize_t
so that it can hold a negative return value.
comp_v1 was returned by llapi_get_lmm_from_path() but not freed.
lmmbuf was allocated in llapi_get_lmm_from_path() but leaked on error.
file p was opened in llapi_get_lmm_from_path() but leaked on error.

Test-Parameters: trivial
CoverityID: 451724 ("Unsigned compared against 0")
CoverityID: 451710 ("Resource leak")
CoverityID: 451707 ("Resource leak")
Fixes: a796b10fa2 ("LU-16446 utils: specify total count for mirror extend")
Signed-off-by: Frederick Dilger <fdilger@whamcloud.com>
Change-Id: I729d2c2abb8789a82d61858b034dcadf733a4197
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/57391
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>
lustre/utils/lfs.c
lustre/utils/liblustreapi.c

index 4a55370..ff0633a 100755 (executable)
@@ -4206,27 +4206,30 @@ create_mirror:
                        }
 
                        if (mirror_total_mode) {
-                               char *path = argv[argc-1];
+                               char *path = argv[argc - 1];
                                struct lov_comp_md_v1 *comp_v1;
+                               int have_mirrors;
 
-                               result = llapi_get_lmm_from_path(path, (struct lov_user_md_v1 **)&comp_v1);
+                               result = llapi_get_lmm_from_path(path,
+                                       (struct lov_user_md_v1 **)&comp_v1);
                                if (result) {
                                        fprintf(stderr,
                                                "error: %s: cannot get layout from %s: %s\n",
                                                progname, path, strerror(-result));
                                        goto error;
                                }
+                               have_mirrors = comp_v1->lcm_mirror_count;
+                               free(comp_v1);
 
-                               if (comp_v1->lcm_mirror_count >= mirror_count)
+                               if (have_mirrors >= mirror_count)
                                        mirror_count = 0;
                                else
-                                       mirror_count -= comp_v1->lcm_mirror_count;
+                                       mirror_count -= have_mirrors;
 
-                               if (!mirror_count) {
+                               if (mirror_count == 0) {
                                        fprintf(stderr,
-                                               "warning: the file '%s' already has %d mirrors. No new mirrors will be created\n",
-                                               path,
-                                               comp_v1->lcm_mirror_count);
+                                               "warning: '%s' already has %d mirrors, no new mirrors will be created\n",
+                                               path, have_mirrors);
                                        break;
                                }
                        }
index 60206f5..27f2de7 100644 (file)
@@ -1963,7 +1963,7 @@ retry_getfileinfo:
  */
 int llapi_get_lmm_from_path(const char *path, struct lov_user_md_v1 **lmmbuf)
 {
-       size_t lmmlen;
+       ssize_t lmmlen;
        int p = -1;
        int rc = 0;
 
@@ -1972,14 +1972,23 @@ int llapi_get_lmm_from_path(const char *path, struct lov_user_md_v1 **lmmbuf)
                return -EINVAL;
 
        p = open_parent(path);
+       if (p < 0)
+               return -errno;
 
        *lmmbuf = calloc(1, lmmlen);
-       if (*lmmbuf == NULL)
-               return -errno;
+       if (*lmmbuf == NULL) {
+               rc = -errno;
+               goto out_close;
+       }
 
        rc = get_lmd_info_fd(path, p, 0, *lmmbuf, lmmlen, GET_LMD_STRIPE);
-       if (p != -1)
-               close(p);
+       if (rc < 0) {
+               free(*lmmbuf);
+               *lmmbuf = NULL;
+       }
+out_close:
+       close(p);
+
        return rc;
 }