[pacman-dev] [PATCH] Check return value of fwrite when copying files

Allan McRae allan at archlinux.org
Sun Jun 27 07:16:17 EDT 2010


Check that writing to destination file actually occurs in
_alpm_copyfile.  Required adding a new error (PM_ERR_WRITE)
as none of the others appeared appropriate.

Prevents compiler warning when using -D_FORTIFY_SOURCE=2.

Signed-off-by: Allan McRae <allan at archlinux.org>
---
 lib/libalpm/alpm.h |    1 +
 lib/libalpm/util.c |   10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 3329132..0c01f21 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -522,6 +522,7 @@ enum _pmerrno_t {
 	PM_ERR_FILE_CONFLICTS,
 	/* Misc */
 	PM_ERR_RETRIEVE,
+	PM_ERR_WRITE,
 	PM_ERR_INVALID_REGEX,
 	/* External library errors */
 	PM_ERR_LIBARCHIVE,
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 383568c..559d62e 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -143,7 +143,15 @@ int _alpm_copyfile(const char *src, const char *dest)
 
 	/* do the actual file copy */
 	while((len = fread(buf, 1, CPBUFSIZE, in))) {
-		fwrite(buf, 1, len, out);
+		size_t nwritten = 0;
+		nwritten = fwrite(buf, 1, len, out);
+		if((nwritten != len) || ferror(out)) {
+			pm_errno = PM_ERR_WRITE;
+			_alpm_log(PM_LOG_ERROR, _("error writing to file '%s': %s\n"),
+					dest, strerror(errno));
+			ret = -1;
+			goto cleanup;
+		}
 	}
 
 	/* chmod dest to permissions of src, as long as it is not a symlink */
-- 
1.7.1



More information about the pacman-dev mailing list