commit 67a00c86f97f672c9fbceba9ad7ac1f747cca10b
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Fri Sep 22 19:44:54 2023 +0200
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Fri Sep 22 20:45:38 2023 +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.

diff --git a/ed.c b/ed.c
index 9443f6e..e14b7ad 100644
--- 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;

Reply via email to