[gnu.bash.bug] return values of the function and pipe

drazen@VLSI.CALTECH.EDU (Drazen Borkovic) (07/11/89)

There are 2 problems:
1. Return  from a function doesn't work correctly. 
   Value  returned from the function is not correct.
2. Value returned by the pipe is not correct.

This was bash version 1.02.
=========================================================
Script started on Tue Jul 11 09:17:54 1989
> type pushd
pushd is a function
pushd () {
local st ; builtin pushd $1 ; st=$? ; stripe ; return $st 
}
> pushd JUNK.DIR ; echo $?
JUNK.DIR: No such file or directory
can only `return' from a function
> pushd JUNK.DIR 
JUNK.DIR: No such file or directory
can only `return' from a function
> echo $?
0
> pushd c ; echo $?
~/c ~ 
can only `return' from a function
> 
> 
> cd ..
> grep JUNK Maxa ; echo $?
1
> cat Maxa | grep JUNK ; echo $?
0
> 
> 
> exit

script done on Tue Jul 11 09:19:20 1989
=========================================================

Drazen Borkovic
CS dept, Caltech 256-80, Pasadena CA 91125
drazen@vlsi.caltech.edu

chet@kiwi.CWRU.EDU (Chet Ramey) (07/13/89)

In article <8907111627.AA08421@vlsi.caltech.edu> drazen@VLSI.CALTECH.EDU (Drazen Borkovic) writes:
>There are 2 problems:
>1. Return  from a function doesn't work correctly. 

You're right, but I could not reproduce this exactly on a Sun-3 (SunOS
3.5), an IBM RT (4.3BSD), or a Vax (Ultrix 3.0).  I never got the "can only
`return' from a function" error you saw. 

>   Value  returned from the function is not correct.

THIS CANNOT BE FIXED WITHOUT A REDESIGN (or at least a rethinking) OF THE
FUNCTION EXECUTION MECHANISM.  bash 1.02 relies on the property of longjmp
that lets you specify what value the corresponding setjmp call will return
when you call it.  YOU CANNOT PORTABLY SPECIFY 0 HERE.  Most longjmps that
I know of (Sun, Ultrix) will change a 0 value to a 1, since setjmp will
only return 0 the first time it is called.  On 4.3 BSD, it appears that you
can make setjmp return 0 by specifying 0 to longjmp, but that just makes
bash recurse infinitely in execute_simple_command(). 

I just tried the YAGV (`Yet Another Global Variable') approach to fixing this,
and it seems to work (at least for a variant of your pushd example) on the 
Sun, the VAX and the RT.  Ugh.  Patches appended, though they are not
Officially Blessed

>2. Value returned by the pipe is not correct.

This looks like a race condition to me.

*** bash-1.02/execute_cmd.c	Sat Jul  1 21:44:45 1989
--- src-1.02/execute_cmd.c	Wed Jul 12 17:23:17 1989
***************
*** 491,494
  /* For catching RETURN in a function. */
  int return_catch_flag = 0;
  jmp_buf return_catch;
  

--- 491,495 -----
  /* For catching RETURN in a function. */
  int return_catch_flag = 0;
+ int return_return_val = 0;
  jmp_buf return_catch;
  
***************
*** 636,640
  		    if (return_val)
  		      {
! 			result = return_val;
  		      }
  		    else

--- 637,641 -----
  		    if (return_val)
  		      {
! 			result = return_return_val;
  		      }
  		    else

*** bash-1.02/builtins.c	Wed Jul  5 21:25:49 1989
--- src-1.02/builtins.c	Wed Jul 12 17:12:46 1989
***************
*** 1642,1645
    extern int last_command_exit_value;
    extern int return_catch_flag;
    extern jmp_buf return_catch;
    int return_val = get_numeric_arg ("return", list);

--- 1662,1666 -----
    extern int last_command_exit_value;
    extern int return_catch_flag;
+   extern int return_return_val;
    extern jmp_buf return_catch;
    int return_val = get_numeric_arg ("return", list);
***************
*** 1648,1651
      return_val = last_command_exit_value;
  
    if (return_catch_flag)
      longjmp (return_catch, return_val);

--- 1669,1673 -----
      return_val = last_command_exit_value;
  
+   return_return_val = return_val;
    if (return_catch_flag)
      longjmp (return_catch, 1);	/* force a 1 */
***************
*** 1649,1653
  
    if (return_catch_flag)
!     longjmp (return_catch, return_val);
    else {
      report_error ("can only `return' from a function");

--- 1671,1675 -----
    return_return_val = return_val;
    if (return_catch_flag)
!     longjmp (return_catch, 1);	/* force a 1 */
    else {
      report_error ("can only `return' from a function");



Chet Ramey			"We are preparing to think about contemplating 
Network Services Group, CWRU	 preliminary work on plans to develop a
chet@cwjcc.INS.CWRU.Edu		 schedule for producing the 10th Edition of 
				 the Unix Programmers Manual." -- Andrew Hume