From f1634250587479d664b34b6de1a6546b2c2b9de5 Mon Sep 17 00:00:00 2001
From: Florian Festi <ffesti@redhat.com>
Date: Mon, 18 Jan 2021 15:02:34 +0100
Subject: [PATCH] rpm2archive: Add more error handling

Cleanly error out if file can't be written instead of segfaulting

Resolves: #1091
---
 rpm2archive.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/rpm2archive.c b/rpm2archive.c
index 646f1663d..15c5da016 100644
--- a/rpm2archive.c
+++ b/rpm2archive.c
@@ -119,9 +119,14 @@ static int process_package(rpmts ts, char * filename)
 
     /* create archive */
     a = archive_write_new();
-    archive_write_add_filter_gzip(a);
-    archive_write_set_format_pax_restricted(a);
-
+    if (archive_write_add_filter_gzip(a) != ARCHIVE_OK) {
+	fprintf(stderr, "Error: Could not create gzip output filter\n");
+	exit(EXIT_FAILURE);
+    }
+    if (archive_write_set_format_pax_restricted(a) != ARCHIVE_OK) {
+	fprintf(stderr, "Error: Format pax restricted is not supported\n");
+	exit(EXIT_FAILURE);
+    }
     if (!strcmp(filename, "-")) {
 	if (isatty(STDOUT_FILENO)) {
 	    fprintf(stderr, "Error: refusing to output archive data to a terminal.\n");
@@ -130,9 +135,11 @@ static int process_package(rpmts ts, char * filename)
 	archive_write_open_fd(a, STDOUT_FILENO);
     } else {
 	char * outname = rstrscat(NULL, filename, ".tgz", NULL);
-	archive_write_open_filename(a, outname);
+	if (archive_write_open_filename(a, outname) != ARCHIVE_OK) {
+	    fprintf(stderr, "Error: Can't open output file: %s\n", outname);
+	    exit(EXIT_FAILURE);
+	}
 	_free(outname);
-	// XXX error handling
     }
 
     entry = archive_entry_new();
-- 
2.27.0

