~aperezdc/dmon

50b2d4d3f788bf884cc2875016739cd7d3e632f7 — Adrian Perez de Castro 3 years ago 65565f9
dlog: Avoid trying to close an invalid file descriptor

Avoid a call to close() for the output file descriptor if the file has
not yet been opened. This can happen if no input at all is provided or
--skip-empty/-e is enabled and only empty lines are read.

The issue was reproducible as follows:

  % ./dlog -e < /dev/null
  WARN   Closing log: Bad file descriptor  [dlog.c:163, main]
  %
1 files changed, 1 insertions(+), 1 deletions(-)

M dlog.c
M dlog.c => dlog.c +1 -1
@@ 161,7 161,7 @@ dlog_main (int argc, char **argv)
        dbuf_clear(&linebuf);
    }

    if (close (log_fd) != 0)
    if (log_fd >= 0 && close(log_fd) != 0)
        clog_warning("Closing log: %s", strerror(errno));

    exit (EXIT_SUCCESS);