Whamcloud - gitweb
LU-18560 utils: Fix ldiskfs_write_dd Coverity warnings 49/57449/2
authorMarc Vef <mvef@whamcloud.com>
Sun, 15 Dec 2024 20:15:49 +0000 (21:15 +0100)
committerOleg Drokin <green@whamcloud.com>
Sat, 18 Jan 2025 22:05:26 +0000 (22:05 +0000)
Coverity has reported issues with the "ldiskfs_write_ldd()" function.
In particular OVERRUN, INTEGER_OVERFLOW, NO_EFFECT, and
ARRAY_VS_SINGLETON warnings.

This patch fixes these warnings.

Fixes: 7687e22af35d ("LU-9936 utils: Improve ldiskfs_write_dd() writing mount data")
Signed-off-by: Marc Vef <mvef@whamcloud.com>
Change-Id: Id1379d9c6d53e697ff2ee12570b4569c4bce5e73
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/57449
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Mikhail Pershin <mpershin@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/utils/libmount_utils_ldiskfs.c

index 70b267c..01036df 100644 (file)
@@ -142,10 +142,10 @@ int ldiskfs_write_ldd(struct mkfs_opts *mop)
        char mntpt[] = "/tmp/mntXXXXXX";
        char filepnm[192];
        char *dev;
-       int ret = 0;
+       size_t total_written, remaining;
+       ssize_t write_cnt;
        int fd;
-       size_t total_written;
-       size_t write_cnt;
+       int ret = 0;
 
        /* Mount this device temporarily in order to write these files */
        if (!mkdtemp(mntpt)) {
@@ -223,10 +223,13 @@ int ldiskfs_write_ldd(struct mkfs_opts *mop)
                ret = errno;
                goto out_umnt;
        }
+
        total_written = 0;
-       while (total_written < sizeof(mop->mo_ldd)) {
-               write_cnt = write(fd, &mop->mo_ldd + total_written,
-                                 sizeof(mop->mo_ldd) - total_written);
+       remaining = sizeof(mop->mo_ldd);
+       while (remaining > 0) {
+               write_cnt = write(fd,
+                                 (const char *)&mop->mo_ldd + total_written,
+                                 remaining);
                if (write_cnt < 0) {
                        fprintf(stderr,
                                "%s: Unable to write to file (%s): %s\n",
@@ -235,7 +238,9 @@ int ldiskfs_write_ldd(struct mkfs_opts *mop)
                        goto close_fd;
                }
                total_written += write_cnt;
+               remaining -= write_cnt;
        }
+
        if (fsync(fd) < 0) {
                fprintf(stderr, "%s: Unable to fsync file (%s): %s\n", progname,
                        filepnm, strerror(errno));