mirror of
				git://git.code.sf.net/p/cdesktopenv/code
				synced 2025-03-09 15:50:02 +00:00 
			
		
		
		
	Fix 'test -t 1' in $(command substitutions)
Standard output (FD 1) tested as being on a terminal within a
command substitution, which makes no sense as the command
substitution is supposed to be catching standard output.
    ksh -c 'v=$(echo begincomsub
                [ -t 1 ] && echo oops
                echo endcomsub)
            echo "$v"'
This should not output "oops".
This is one of the many bugs with ksh93 virtual (non-forked)
subshells. On the abandoned Vashist/Rader ksh2020 branch, this bug
was fixed by changing quite a lot of code, which introduced and/or
exposed another bug:
	https://github.com/att/ast/issues/1079
	8e1e405e
	https://github.com/att/ast/issues/1088
That issue was unresolved when the ksh2020 branch was abandoned.
The safer and more conservative fix is simply forcing the subshell
to fork if we're in a non-forked command substitution and testing
'-t 1'. It is hard to imagine a situation where this would cause a
noticable performance hit.
Note that this fix does not affect ksh93-specific "shared"
non-subshell ${ command substitutions; } which are executed in the
main shell environment, so that variables survive, etcetera.
'test -t 1' continues to wrongly return true there, but command
substitutions of that form cannot be forked because that would
defeat their purpose.
src/cmd/ksh93/bltins/test.c:
- Fix 'test -t 1', '[ -t 1 ]' and '[[ -t 1 ]]' by forking the
  current subshell if it is a virtual/non-forked subshell
  (shp->subshell), and a command substitution (shp->comsub), but
  NOT a "shared" ${ command substitution; } (!shp->subshare).
src/cmd/ksh93/tests/bracket.sh:
- Add two regression tests for this issue, which were adapted from
  the Vashist/Rader ksh2020 branch.
NEWS, src/cmd/ksh93/include/version.h:
- Update.
(cherry picked from commit b8ef05e457ead65b83417699b8dd8632f855e2fa)
			
			
This commit is contained in:
		
							parent
							
								
									b87a7cabc5
								
							
						
					
					
						commit
						cafe33f048
					
				
					 4 changed files with 31 additions and 1 deletions
				
			
		|  | @ -425,6 +425,9 @@ int test_unop(Shell_t *shp,register int op,register const char *arg) | |||
| 	    { | ||||
| 		char *last; | ||||
| 		op = strtol(arg,&last, 10); | ||||
| 		/* To make '-t 1' work in a $(comsub), fork. https://github.com/att/ast/issues/1079 */ | ||||
| 		if (op == 1 && shp->subshell && shp->comsub && !shp->subshare) | ||||
| 			sh_subfork(); | ||||
| 		return(*last?0:tty_check(op)); | ||||
| 	    } | ||||
| 	    case 'v': | ||||
|  |  | |||
|  | @ -17,4 +17,4 @@ | |||
| *                  David Korn <dgk@research.att.com>                   * | ||||
| *                                                                      * | ||||
| ***********************************************************************/ | ||||
| #define SH_RELEASE	"93u+m 2020-05-14" | ||||
| #define SH_RELEASE	"93u+m 2020-05-16" | ||||
|  |  | |||
|  | @ -356,4 +356,19 @@ test ! ! ! 2> /dev/null || err_exit 'test ! ! ! should return 0' | |||
| test ! ! x 2> /dev/null || err_exit 'test ! ! x should return 0' | ||||
| test ! ! '' 2> /dev/null && err_exit 'test ! ! "" should return non-zero' | ||||
| 
 | ||||
| # ====== | ||||
| # Verify that [ -t 1 ] behaves sensibly inside a command substitution. | ||||
| 
 | ||||
| # This is the simple case that doesn't do any redirection of stdout within the command | ||||
| # substitution. Thus the [ -t 1 ] test should be false. | ||||
| var=$(echo begin; { [ -t 1 ] || test -t 1 || [[ -t 1 ]]; } && echo -t 1 is true; echo end) | ||||
| [[ $var == $'begin\nend' ]] || err_exit "test -t 1 in comsub fails" | ||||
| 
 | ||||
| # This is the more complex case that does redirect stdout within the command substitution to the | ||||
| # actual tty. Thus the [ -t 1 ] test should be true. | ||||
| var=$(echo begin; exec >/dev/tty; [ -t 1 ] && test -t 1 && [[ -t 1 ]]) \ | ||||
| && [[ $var == $'begin' ]] \ | ||||
| || err_exit "test -t 1 in comsub with exec >/dev/tty fails" | ||||
| 
 | ||||
| # ====== | ||||
| exit $((Errors<125?Errors:125)) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue