From dc109345f614497b2d9c24f577bc8cbcdb65ad5f Mon Sep 17 00:00:00 2001 From: Koudai Iwahori Date: Fri, 6 Jan 2023 22:18:17 -0800 Subject: [PATCH] sh: Call clearerr after running builtin command When we run `echo foo > file` and `file` is a device file or FIFO, writing to `file` can cause an error like Invalid Argument or Broken Pipe and set an error flag of stdout. The error flag in stdout persists in subsequent builtin commands and they always print error message `${COMMAND_NAME}: write`. This commit adds `clearerr(stdout)` after the execution of sh builtin command. --- toys/pending/sh.c | 1 + 1 file changed, 1 insertion(+) diff --git a/toys/pending/sh.c b/toys/pending/sh.c index 0f9532e2..10fa7ba3 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -2863,6 +2863,7 @@ static struct sh_process *run_command(void) } toys.rebound = 0; pp->exit = toys.exitval; + clearerr(stdout); if (toys.optargs != toys.argv+1) free(toys.optargs); if (toys.old_umask) umask(toys.old_umask); memcpy(&toys, &temp, jj); -- 2.39.2