From: Marc Vef Date: Sun, 15 Dec 2024 20:15:49 +0000 (+0100) Subject: LU-18560 utils: Fix ldiskfs_write_dd Coverity warnings X-Git-Tag: 2.16.52~95 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=52105a555f55b399a115f3158b3827eb0db726f6;p=fs%2Flustre-release.git LU-18560 utils: Fix ldiskfs_write_dd Coverity warnings 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 Change-Id: Id1379d9c6d53e697ff2ee12570b4569c4bce5e73 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/57449 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Mikhail Pershin Reviewed-by: Andreas Dilger Reviewed-by: Oleg Drokin --- diff --git a/lustre/utils/libmount_utils_ldiskfs.c b/lustre/utils/libmount_utils_ldiskfs.c index 70b267c..01036df 100644 --- a/lustre/utils/libmount_utils_ldiskfs.c +++ b/lustre/utils/libmount_utils_ldiskfs.c @@ -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));