[PATCH] printf: check if stdout is closed or not

Leesoo Ahn yisooan at fedoraproject.org
Tue Apr 14 12:39:58 UTC 2020


Signed-off-by: Leesoo Ahn <yisooan at fedoraproject.org>
---
 coreutils/printf.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/coreutils/printf.c b/coreutils/printf.c
index a20fc3301..0fe249399 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -405,6 +405,7 @@ int printf_main(int argc UNUSED_PARAM, char **argv)
 	int conv_err;
 	char *format;
 	char **argv2;
+	int flag;
 
 	/* We must check that stdout is not closed.
 	 * The reason for this is highly non-obvious.
@@ -415,9 +416,15 @@ int printf_main(int argc UNUSED_PARAM, char **argv)
 	 * even if libc receives EBADF on write attempts, it feels determined
 	 * to output data no matter what. So it will try later,
 	 * and possibly will clobber future output. Not good. */
-// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
-	if (fcntl(1, F_GETFL) == -1)
+	if ((flag = fcntl(STDOUT_FILENO, F_GETFL)) == -1)
 		return 1; /* match coreutils 6.10 (sans error msg to stderr) */
+	switch (flag & O_ACCMODE) {
+	case O_WRONLY:
+	case O_RDWR:
+		break;
+	default:
+		return 1;
+	}
 	//if (dup2(1, 1) != 1) - old way
 	//	return 1;
 
-- 
2.24.1



More information about the busybox mailing list