From fc8386be36a32f8462a0d16a2dd3e5e18f7fbc2d Mon Sep 17 00:00:00 2001
From: Demi Marie Obenour <demi@invisiblethingslab.com>
Date: Mon, 12 Apr 2021 11:30:51 -0400
Subject: [PATCH] rpmkeys: exit non-zero on I/O errors

If writing to stdout or stderr fails, rpmkeys should exit with a
non-zero status code.
---
 rpmkeys.c          |  4 ++++
 tests/rpmsigdig.at | 13 +++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/rpmkeys.c b/rpmkeys.c
index 542601c..2c304de 100644
--- a/rpmkeys.c
+++ b/rpmkeys.c
@@ -86,5 +86,9 @@ int main(int argc, char *argv[])
 exit:
     rpmtsFree(ts);
     rpmcliFini(optCon);
+    fflush(stderr);
+    fflush(stdout);
+    if (ferror(stdout) || ferror(stderr))
+	return 255; /* I/O error */
     return ec;
 }
diff --git a/tests/rpmsigdig.at b/tests/rpmsigdig.at
index c8b9f13..429163e 100644
--- a/tests/rpmsigdig.at
+++ b/tests/rpmsigdig.at
@@ -24,6 +24,19 @@ runroot rpmkeys -Kv /data/RPMS/hello-2.0-1.x86_64.rpm /data/RPMS/hello-1.0-1.i38
 [])
 AT_CLEANUP
 
+# ------------------------------
+# Test rpmkeys write errors
+AT_SETUP([[rpmkeys -K no space left on stdout]])
+AT_KEYWORDS([rpmkeys digest])
+AT_CHECK([
+RPMDB_INIT[
+
+runroot rpmkeys -Kv /data/RPMS/hello-2.0-1.x86_64.rpm /data/RPMS/hello-1.0-1.i386.rpm >/dev/full
+]],255,,[[Error writing to log: No space left on device
+]])
+AT_CLEANUP
+
+
 AT_SETUP([rpmkeys -Kv <reconstructed> 1])
 AT_KEYWORDS([rpmkeys digest])
 AT_CHECK([
-- 
1.8.3.1

