From 8018f53e0a12ffdaeb4df3009b4e2cae17cbbb32 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Tue, 18 Jun 2019 17:32:50 +0200 Subject: [PATCH] xbps-rindex/repoflush.c: stop abusing assert(). assert() must not change the program behaviour. Make sure fchmod() and rename() succeed properly and fail gracefully otherwise. --- bin/xbps-rindex/repoflush.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/xbps-rindex/repoflush.c b/bin/xbps-rindex/repoflush.c index 3e792c71..13750e3b 100644 --- a/bin/xbps-rindex/repoflush.c +++ b/bin/xbps-rindex/repoflush.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2013-2015 Juan Romero Pardines. + * Copyright (c) 2013-2019 Juan Romero Pardines. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -46,10 +46,13 @@ repodata_flush(struct xbps_handle *xhp, const char *repodir, char *repofile, *tname, *buf; int rv, repofd = -1; mode_t mask; + bool result; /* Create a tempfile for our repository archive */ repofile = xbps_repo_path_with_name(xhp, repodir, reponame); + assert(repofile); tname = xbps_xasprintf("%s.XXXXXXXXXX", repofile); + assert(tname); mask = umask(S_IXUSR|S_IRWXG|S_IRWXO); if ((repofd = mkstemp(tname)) == -1) return false; @@ -111,11 +114,22 @@ repodata_flush(struct xbps_handle *xhp, const char *repodir, #else fsync(repofd); #endif - assert(fchmod(repofd, 0664) != -1); + if (fchmod(repofd, 0664) == -1) { + unlink(repofile); + close(repofd); + result = false; + goto out; + } close(repofd); - rename(tname, repofile); + if (rename(tname, repofile) == -1) { + unlink(repofile); + result = false; + goto out; + } + result = true; +out: free(repofile); free(tname); - return true; + return result; }