Avoid a potential segfault that may occur if we use a temporary file and
fail to build the destination file name from the effective URL.

Also, use the STRDUP macro instead of strdup() for the sake of better
error handling on memory allocation failures.

Signed-off-by: Lukas Fleischer <[email protected]>
---
 lib/libalpm/dload.c |   14 +++++++++-----
 1 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index 731d807..e281578 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -384,12 +384,16 @@ cleanup:
        }
 
        if(ret == 0) {
-               if(rename(tempfile, destfile)) {
-                       _alpm_log(handle, ALPM_LOG_ERROR, _("could not rename 
%s to %s (%s)\n"),
-                                       tempfile, destfile, strerror(errno));
-                       ret = -1;
+               if (destfile) {
+                       if(rename(tempfile, destfile)) {
+                               _alpm_log(handle, ALPM_LOG_ERROR, _("could not 
rename %s to %s (%s)\n"),
+                                               tempfile, destfile, 
strerror(errno));
+                               ret = -1;
+                       } else if(final_file) {
+                               STRDUP(*final_file, strrchr(destfile, '/') + 1, 
RET_ERR(handle, ALPM_ERR_MEMORY, -1));
+                       }
                } else if(final_file) {
-                       *final_file = strdup(strrchr(destfile, '/') + 1);
+                       STRDUP(*final_file, strrchr(tempfile, '/') + 1, 
RET_ERR(handle, ALPM_ERR_MEMORY, -1));
                }
        }
 
-- 
1.7.6


Reply via email to