[gnu.bash.bug] Variable substitution bug in bash 1.04

freb@goldfnch.MV.COM (Karl Freburger) (01/10/90)

Bug report for bash 1.04 running on an Intel 386 with Bell Technologies
System V/386 release 3.0, gcc 1.34.  bash had been hacked to run on
System V in the ways described at the end of this message.

bash 1.04 does not handle ${1:-word} properly--it ALWAYS uses word for the
substitution, whether $1 is set or not.  A serious bug because it breaks
many shell scripts.

To reproduce the error, put the following in a file (call it 't1'):


# script to show bug in bash 1.04

case "$1" in
  '')  echo "\$1 is not set...  ${1:-default}";;
  *)   echo "\$1 is '$1'...  ${1:-default}";;
esac

Then do:

% bash t1
$1 is not set...  default	(CORRECT)
% bash t1 xxx
$1 is 'xxx'...  default		(WRONG)
% sh t1
$1 is not set...  default	(CORRECT)
% sh t1 xxx
$1 is 'xxx'...  xxx		(CORRECT)

(only do the last two commands if you have a real System V shell available--
the Berkeley Bourne shell uses a different syntax for this substitution, I
think)

	Karl Freburger
	freb@goldfnch.mv.com


As distributed, bash 1.04 would not compile under System V/386 release 3.0,
a fairly vanilla System V.  I had to make the following changes to get it
to run:

    Change config.h to define NO_WAIT_H for SYSV.

    Change glob.c.  USG is used in it for conditional compilation, but is
    not defined anywhere.  Normally, I'd say that USG is the same as SYSV,
    but in glob.c we want to use <dirent.h> for SYSV, not "ndir.h" (I
    don't know why USG doesn't use <dirent.h>).  We want to use <memory.h>
    and <string.h> for SYSV.


Here are the diffs...

*** config.h	Tue Jan  9 22:01:21 1990
--- config.h.orig	Sat Jan  6 13:51:49 1990
***************
*** 66,73 ****
  
  /* Define NO_WAIT_H if your system doesn't seem to have sys/wait.h.
     This is true for HPUX and ALTOS. */
! /* Also true for SYSV */
! #if defined (HPUX) || defined (ALTOS) || defined(SYSV)
  #define NO_WAIT_H
  #endif
  
--- 66,72 ----
  
  /* Define NO_WAIT_H if your system doesn't seem to have sys/wait.h.
     This is true for HPUX and ALTOS. */
! #if defined (HPUX) || defined (ALTOS)
  #define NO_WAIT_H
  #endif
  

*** glob.c	Tue Jan  9 22:04:09 1990
--- glob.c.orig	Sat Jan  6 15:17:39 1990
***************
*** 21,28 ****
  
  #include <sys/types.h>
  
! /* SYSV should use dirent.h */
! #if	defined(USGr3) || defined(DIRENT) || defined(SYSV)
  #include <dirent.h>
  #define direct dirent
  #define	D_NAMLEN(d) strlen((d)->d_name)
--- 21,27 ----
  
  #include <sys/types.h>
  
! #if	defined(USGr3) || defined(DIRENT)
  #include <dirent.h>
  #define direct dirent
  #define	D_NAMLEN(d) strlen((d)->d_name)
***************
*** 35,42 ****
  #	endif	/* USG.  */
  #endif	/* USGr3 or DIRENT.  */
  
! /* SYSV should use memory.h, string.h, memcpy, and strrchr */
! #if defined(USG) || defined(SYSV)
  #include <memory.h>
  #include <string.h>
  #define bcopy(s, d, n) ((void) memcpy ((d), (s), (n)))
--- 34,40 ----
  #	endif	/* USG.  */
  #endif	/* USGr3 or DIRENT.  */
  
! #ifdef USG
  #include <memory.h>
  #include <string.h>
  #define bcopy(s, d, n) ((void) memcpy ((d), (s), (n)))