michal-grzedzicki / rpms / rpm

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