From 56157cbd82ed59060393d45e68245108674bfe5b Mon Sep 17 00:00:00 2001 From: "Erik M. Bray" Date: Tue, 4 Dec 2018 12:44:23 +0000 Subject: [PATCH] prevent infinite recursions in echoandcheck SyWriteandcheck in cases where the default stdio output streams cannot be written to --- src/sysfiles.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/sysfiles.c b/src/sysfiles.c index ac6ca58f37..b77f0d2074 100644 --- a/src/sysfiles.c +++ b/src/sysfiles.c @@ -153,19 +153,24 @@ ssize_t echoandcheck(int fid, const char *buf, size_t count) { int ret; if (syBuf[fid].type == gzip_socket) { ret = gzwrite(syBuf[fid].gzfp, buf, count); - if (ret < 0) + if (ret < 0) { ErrorQuit( "Could not write to compressed file, see 'LastSystemError();'\n", 0L, 0L); + } } else { ret = write(syBuf[fid].echo, buf, count); - if (ret < 0) - ErrorQuit("Could not write to file descriptor %d, see " - "'LastSystemError();'\n", - syBuf[fid].fp, 0L); + if (ret < 0) { + if (syBuf[fid].fp == fileno(stdout) || syBuf[fid].fp == fileno(stderr)) { + Panic("Could not write to stdout/stderr."); + } else { + ErrorQuit("Could not write to file descriptor %d, see " + "'LastSystemError();'\n", + syBuf[fid].fp, 0L); + } + } } - return ret; } @@ -1540,17 +1545,23 @@ static ssize_t SyWriteandcheck(Int fid, const void * buf, size_t count) int ret; if (syBuf[fid].type == gzip_socket) { ret = gzwrite(syBuf[fid].gzfp, buf, count); - if (ret < 0) + if (ret < 0) { ErrorQuit( "Cannot write to compressed file, see 'LastSystemError();'\n", 0L, 0L); + } } else { ret = write(syBuf[fid].fp, buf, count); - if (ret < 0) - ErrorQuit("Cannot write to file descriptor %d, see " - "'LastSystemError();'\n", - syBuf[fid].fp, 0L); + if (ret < 0) { + if (syBuf[fid].fp == fileno(stdout) || syBuf[fid].fp == fileno(stderr)) { + Panic("Could not write to stdout/stderr."); + } else { + ErrorQuit("Cannot write to file descriptor %d, see " + "'LastSystemError();'\n", + syBuf[fid].fp, 0L); + } + } } return ret;