[hackers] [sbase] ed: Open output file for writing || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Fri, 22 Sep 2023 20:48:27 +0200 (CEST)

commit 67a00c86f97f672c9fbceba9ad7ac1f747cca10b
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Fri Sep 22 19:44:54 2023 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
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
_AT_@ -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");
         }
 
_AT_@ -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;
Received on Fri Sep 22 2023 - 20:48:27 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 22 2023 - 20:48:40 CEST