X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f From: "Juan Manuel Guerrero" Organization: Darmstadt University of Technology To: djgpp-workers AT delorie DOT com Date: Thu, 25 Nov 2004 11:02:30 +0200 MIME-Version: 1.0 Subject: A fix for popen(). Message-ID: <41A5BBC6.18442.2294B0C@localhost> X-mailer: Pegasus Mail for Windows (v4.02a, DE v4.02 R1) Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Content-description: Mail message body X-TUD-HRZ-MailScanner: Found to be clean X-TUD-HRZ-MailScanner-SpamCheck: Reply-To: djgpp-workers AT delorie DOT com Please look at the following code snippet from popen.c: /* redirect stdout */ if (!(l1->fp = freopen(temp_name, "wb", stdout))) goto error; /* make sure file is removed on abnormal exit */ l1->fp->_flag |= _IORMONCL; l1->fp->_name_to_remove = temp_name; /* execute command */ l1->exit_status = system(cm); /* don't remove file while closing */ l1->fp->_flag &= ~_IORMONCL; l1->fp->_name_to_remove = NULL; /* close file */ fclose(l1->fp); /* reopen real stdout */ if (dup2(fd, fileno(stdout)) == -1) goto error; As can be seen l1->fp contains the file pointer of stdout that has been redirected to the temp file. After the system() call, the file pointer is passed to fclose(). When fclose() returns the file descriptor of stdout's file pointer has been set to -1. Now, the call of fileno() with stdout's file pointer closed will return exactly this file descriptor of -1. If dup2() is called with a file descriptor of -1, the call always will fail making popen() always fail for pipe reading mode too. The failure of popen() is one reason why the eval test of the testsuite of sed 4.1.2 fails if the package has been compiled with djdev204 and partially works if compiled with djdev203. djdev203 does not contain this bug. Unfortunatly now that this bug has been fixed a new one appers. Now the e command dies with a SIGSEGV in free() of djdev204. This must still be investigated. This can be solved with the small patch below. dup2() closes the new file descriptor anyway so there is no reason to call fclose() at all. Please note that I have no write access, so someone else must take care about this issue. I assume that djdev204 will not be released quite soon, so the best way to fix sed 4.1.2 is to disable the evaluate command at all. Regards, Juan M. Guerrero diff -aruU4 djgpp.orig/src/libc/posix/stdio/popen.c djgpp/src/libc/posix/stdio/popen.c --- djgpp.orig/src/libc/posix/stdio/popen.c 2004-06-02 04:08:32.000000000 +0000 +++ djgpp/src/libc/posix/stdio/popen.c 2004-11-25 02:56:38.000000000 +0000 @@ -122,11 +122,8 @@ /* reopen real stdout */ if (dup2(fd, fileno(stdout)) == -1) goto error; - /* close duplicate stdout */ - close(fd); - /* if cmd couldn't be run, make sure we return NULL */ if (l1->exit_status == -1) goto error; }