sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit 67a00c86f97f672c9fbceba9ad7ac1f747cca10b
parent feeb6e32792b1e4611a9a57476417fcf6b49e6c8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 22 Sep 2023 19:44:54 +0200

ed: Open output file for writing

Fopen() and Popen() were open as read streams, but we were writing
in both cases. In the same way, the FILE pointer returned by popen()
was close with fclose() that can lead to file descriptor leaks and
zombie processes.

Diffstat:
Med.c | 14+++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/ed.c b/ed.c @@ -623,14 +623,16 @@ dowrite(const char *fname, int trunc) { FILE *fp; size_t bytecount = 0; - int i, line; + int i, r, line, sh; if(fname[0] == '!') { + sh = 1; fname++; - if((fp = popen(fname, "r")) == NULL) - error("Bad Exec"); + if((fp = popen(fname, "w")) == NULL) + error("bad exec"); } else { - if ((fp = fopen(fname, "r")) == NULL) + sh = 0; + if ((fp = fopen(fname, "w")) == NULL) error("cannot open input file"); } @@ -642,7 +644,9 @@ dowrite(const char *fname, int trunc) } curln = line2; - if (fclose(fp)) + + r = sh ? pclose(fp) : fclose(fp); + if (r) error("input/output error"); strcpy(savfname, fname); modflag = 0;