Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: no more fd mixups



This patch prevents the user from duping the shell's private file
descriptors (deemed to be fds 10 and above).  I had to do a slight hack
to allow the coprocess file descriptors to be duped, but the extra logic
for that incidentally made it easy to improve the error message for
"<&p" where there is no coprocess.

-zefram

diff -cr ../zsh-/Src/exec.c Src/exec.c
*** ../zsh-/Src/exec.c	Sun Oct 10 14:38:24 1999
--- Src/exec.c	Sun Oct 10 15:21:05 1999
***************
*** 1907,1920 ****
  	    case MERGEOUT:
  		if(fn->fd2 < 10)
  		    closemn(mfds, fn->fd2);
! 		fil = dup(fn->fd2);
  		if (fil == -1) {
  		    char fdstr[4];
  
  		    closemnodes(mfds);
  		    fixfds(save);
! 		    sprintf(fdstr, "%d", fn->fd2);
! 		    zerr("%s: %e", fdstr, errno);
  		    execerr();
  		}
  		addfd(forked, save, mfds, fn->fd1, fil, fn->type == MERGEOUT);
--- 1907,1929 ----
  	    case MERGEOUT:
  		if(fn->fd2 < 10)
  		    closemn(mfds, fn->fd2);
! 		if(fn->fd2 > 9) {
! 		    fil = -1;
! 		    errno = EBADF;
! 		} else {
! 		    int fd = fn->fd2;
! 		    if(fd == -2)
! 			fd = (fn->type == MERGEOUT) ? coprocout : coprocin;
! 		    fil = dup(fd);
! 		}
  		if (fil == -1) {
  		    char fdstr[4];
  
  		    closemnodes(mfds);
  		    fixfds(save);
! 		    if(fn->fd2 != -2)
! 		    	sprintf(fdstr, "%d", fn->fd2);
! 		    zerr("%s: %e", fn->fd2 == -2 ? "coprocess" : fdstr, errno);
  		    execerr();
  		}
  		addfd(forked, save, mfds, fn->fd1, fil, fn->type == MERGEOUT);
diff -cr ../zsh-/Src/glob.c Src/glob.c
*** ../zsh-/Src/glob.c	Sun Oct 10 14:38:24 1999
--- Src/glob.c	Sun Oct 10 15:21:23 1999
***************
*** 1580,1586 ****
  	    if (s[0] == '-' && !s[1])
  		fn->type = CLOSE;
  	    else if (s[0] == 'p' && !s[1]) 
! 		fn->fd2 = (fn->type == MERGEOUT) ? coprocout : coprocin;
  	    else {
  		while (idigit(*s))
  		    s++;
--- 1580,1586 ----
  	    if (s[0] == '-' && !s[1])
  		fn->type = CLOSE;
  	    else if (s[0] == 'p' && !s[1]) 
! 		fn->fd2 = -2;
  	    else {
  		while (idigit(*s))
  		    s++;
END



Messages sorted by: Reverse Date, Date, Thread, Author