From c21a87f0b5f924d97217e22b171afb83759262cf Mon Sep 17 00:00:00 2001 From: Michal Domonkos Date: Mon, 1 Mar 2021 12:45:10 +0100 Subject: [PATCH] Error out on --eval if stdout write fails (#1444) Since --eval only has one job - to actually *print* something to stdout, we should fail completely if that's not possible due to an external error, instead of just not printing anything. One particular case is redirecting stdout to a file on a file system that's full (generating the ENOSPC error). In order to be able to detect such errors, we need to flush the stream buffer first; that's because normally, if stdout is not referring to an interactive device (a terminal), it is block-buffered, as opposed to line-buffered. --- lib/poptALL.c | 4 ++++ tests/rpmmacro.at | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/poptALL.c b/lib/poptALL.c index 936e37aecd..d2221ad843 100644 --- a/lib/poptALL.c +++ b/lib/poptALL.c @@ -133,6 +133,10 @@ static void rpmcliAllArgCallback( poptContext con, if (rpmExpandMacros(NULL, arg, &val, 0) < 0) exit(EXIT_FAILURE); fprintf(stdout, "%s\n", val); + if (fflush(stdout) == EOF) { + perror(_("Error writing to stdout")); + exit(EXIT_FAILURE); + } free(val); } break; diff --git a/tests/rpmmacro.at b/tests/rpmmacro.at index b5ef0631a4..072b986be9 100644 --- a/tests/rpmmacro.at +++ b/tests/rpmmacro.at @@ -967,3 +967,14 @@ xxx error: bad ]) AT_CLEANUP + +AT_SETUP([no space left on stdout]) +AT_KEYWORDS([macros]) +AT_CHECK([ +runroot rpm --eval 1 >/dev/full +], +[1], +[], +[Error writing to stdout: No space left on device +]) +AT_CLEANUP