Skip to content

Commit

Permalink
Error out on --eval if stdout write fails (#1444)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dmnks authored and pmatilai committed Mar 4, 2021
1 parent 9747a6a commit 95a8333
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/poptALL.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions tests/rpmmacro.at
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 95a8333

Please sign in to comment.