From a84ec176eca207eadcd2a47056ad44df082f1b02 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 1 Oct 2013 16:56:26 +0200 Subject: [PATCH] libmount: make mnt_table_replace_file() more robust [coverity scan] Signed-off-by: Karel Zak --- libmount/src/tab_update.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libmount/src/tab_update.c b/libmount/src/tab_update.c index a508b99034..13f6f6ebec 100644 --- a/libmount/src/tab_update.c +++ b/libmount/src/tab_update.c @@ -640,6 +640,12 @@ int mnt_table_replace_file(struct libmnt_table *tb, const char *filename) mnt_table_write_file(tb, f); + if (fflush(f) != 0) { + rc = -errno; + DBG(UPDATE, mnt_debug("%s: fflush failed: %m", uq)); + goto leave; + } + rc = fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) ? -errno : 0; if (!rc && stat(filename, &st) == 0) @@ -647,19 +653,25 @@ int mnt_table_replace_file(struct libmnt_table *tb, const char *filename) rc = fchown(fd, st.st_uid, st.st_gid) ? -errno : 0; fclose(f); + f = NULL; + if (!rc) - rename(uq, filename); + rc = rename(uq, filename) ? -errno : 0; } else { rc = -errno; close(fd); } +leave: + if (f) + fclose(f); unlink(uq); free(uq); DBG(TAB, mnt_debug_h(tb, "replace done [rc=%d]", rc)); return rc; } + static int add_file_entry(struct libmnt_table *tb, struct libmnt_update *upd) { struct libmnt_fs *fs; -- 2.47.3