michal-grzedzicki / rpms / rpm

Forked from rpms/rpm 4 months ago
Clone
ef6cd6
From f1634250587479d664b34b6de1a6546b2c2b9de5 Mon Sep 17 00:00:00 2001
ef6cd6
From: Florian Festi <ffesti@redhat.com>
ef6cd6
Date: Mon, 18 Jan 2021 15:02:34 +0100
ef6cd6
Subject: [PATCH] rpm2archive: Add more error handling
ef6cd6
ef6cd6
Cleanly error out if file can't be written instead of segfaulting
ef6cd6
ef6cd6
Resolves: #1091
ef6cd6
---
ef6cd6
 rpm2archive.c | 17 ++++++++++++-----
ef6cd6
 1 file changed, 12 insertions(+), 5 deletions(-)
ef6cd6
ef6cd6
diff --git a/rpm2archive.c b/rpm2archive.c
ef6cd6
index 646f1663d..15c5da016 100644
ef6cd6
--- a/rpm2archive.c
ef6cd6
+++ b/rpm2archive.c
ef6cd6
@@ -119,9 +119,14 @@ static int process_package(rpmts ts, char * filename)
ef6cd6
 
ef6cd6
     /* create archive */
ef6cd6
     a = archive_write_new();
ef6cd6
-    archive_write_add_filter_gzip(a);
ef6cd6
-    archive_write_set_format_pax_restricted(a);
ef6cd6
-
ef6cd6
+    if (archive_write_add_filter_gzip(a) != ARCHIVE_OK) {
ef6cd6
+	fprintf(stderr, "Error: Could not create gzip output filter\n");
ef6cd6
+	exit(EXIT_FAILURE);
ef6cd6
+    }
ef6cd6
+    if (archive_write_set_format_pax_restricted(a) != ARCHIVE_OK) {
ef6cd6
+	fprintf(stderr, "Error: Format pax restricted is not supported\n");
ef6cd6
+	exit(EXIT_FAILURE);
ef6cd6
+    }
ef6cd6
     if (!strcmp(filename, "-")) {
ef6cd6
 	if (isatty(STDOUT_FILENO)) {
ef6cd6
 	    fprintf(stderr, "Error: refusing to output archive data to a terminal.\n");
ef6cd6
@@ -130,9 +135,11 @@ static int process_package(rpmts ts, char * filename)
ef6cd6
 	archive_write_open_fd(a, STDOUT_FILENO);
ef6cd6
     } else {
ef6cd6
 	char * outname = rstrscat(NULL, filename, ".tgz", NULL);
ef6cd6
-	archive_write_open_filename(a, outname);
ef6cd6
+	if (archive_write_open_filename(a, outname) != ARCHIVE_OK) {
ef6cd6
+	    fprintf(stderr, "Error: Can't open output file: %s\n", outname);
ef6cd6
+	    exit(EXIT_FAILURE);
ef6cd6
+	}
ef6cd6
 	_free(outname);
ef6cd6
-	// XXX error handling
ef6cd6
     }
ef6cd6
 
ef6cd6
     entry = archive_entry_new();
ef6cd6
-- 
ef6cd6
2.38.1
ef6cd6