462fb2
			     BASH PATCH REPORT
462fb2
			     =================
462fb2
462fb2
Bash-Release:	4.2
462fb2
Patch-ID:	bash42-003
462fb2
462fb2
Bug-Reported-by:	Clark J. Wang <dearvoid@gmail.com>
462fb2
Bug-Reference-ID:	<AANLkTikZ_rVV-frR8Fh0PzhXnMKnm5XsUR-F3qtPPs5G@mail.gmail.com>
462fb2
Bug-Reference-URL:	http://lists.gnu.org/archive/html/bug-bash/2011-02/msg00136.html
462fb2
462fb2
Bug-Description:
462fb2
462fb2
When using the pattern replacement and pattern removal word expansions, bash
462fb2
miscalculates the possible match length in the presence of an unescaped left
462fb2
bracket without a closing right bracket, resulting in a failure to match
462fb2
the pattern.
462fb2
462fb2
Patch (apply with `patch -p0'):
462fb2
462fb2
*** ../bash-4.2-patched/lib/glob/gmisc.c	2011-02-05 16:11:17.000000000 -0500
462fb2
--- lib/glob/gmisc.c	2011-02-18 23:53:42.000000000 -0500
462fb2
***************
462fb2
*** 78,83 ****
462fb2
       size_t wmax;
462fb2
  {
462fb2
!   wchar_t wc, *wbrack;
462fb2
!   int matlen, t, in_cclass, in_collsym, in_equiv;
462fb2
  
462fb2
    if (*wpat == 0)
462fb2
--- 78,83 ----
462fb2
       size_t wmax;
462fb2
  {
462fb2
!   wchar_t wc;
462fb2
!   int matlen, bracklen, t, in_cclass, in_collsym, in_equiv;
462fb2
  
462fb2
    if (*wpat == 0)
462fb2
***************
462fb2
*** 119,123 ****
462fb2
  	case L'[':
462fb2
  	  /* scan for ending `]', skipping over embedded [:...:] */
462fb2
! 	  wbrack = wpat;
462fb2
  	  wc = *wpat++;
462fb2
  	  do
462fb2
--- 119,123 ----
462fb2
  	case L'[':
462fb2
  	  /* scan for ending `]', skipping over embedded [:...:] */
462fb2
! 	  bracklen = 1;
462fb2
  	  wc = *wpat++;
462fb2
  	  do
462fb2
***************
462fb2
*** 125,140 ****
462fb2
  	      if (wc == 0)
462fb2
  		{
462fb2
! 	          matlen += wpat - wbrack - 1;	/* incremented below */
462fb2
! 	          break;
462fb2
  	        }
462fb2
  	      else if (wc == L'\\')
462fb2
  		{
462fb2
! 		  wc = *wpat++;
462fb2
! 		  if (*wpat == 0)
462fb2
! 		    break;
462fb2
  		}
462fb2
  	      else if (wc == L'[' && *wpat == L':')	/* character class */
462fb2
  		{
462fb2
  		  wpat++;
462fb2
  		  in_cclass = 1;
462fb2
  		}
462fb2
--- 125,148 ----
462fb2
  	      if (wc == 0)
462fb2
  		{
462fb2
! 		  wpat--;			/* back up to NUL */
462fb2
! 	          matlen += bracklen;
462fb2
! 	          goto bad_bracket;
462fb2
  	        }
462fb2
  	      else if (wc == L'\\')
462fb2
  		{
462fb2
! 		  /* *wpat == backslash-escaped character */
462fb2
! 		  bracklen++;
462fb2
! 		  /* If the backslash or backslash-escape ends the string,
462fb2
! 		     bail.  The ++wpat skips over the backslash escape */
462fb2
! 		  if (*wpat == 0 || *++wpat == 0)
462fb2
! 		    {
462fb2
! 		      matlen += bracklen;
462fb2
! 		      goto bad_bracket;
462fb2
! 		    }
462fb2
  		}
462fb2
  	      else if (wc == L'[' && *wpat == L':')	/* character class */
462fb2
  		{
462fb2
  		  wpat++;
462fb2
+ 		  bracklen++;
462fb2
  		  in_cclass = 1;
462fb2
  		}
462fb2
***************
462fb2
*** 142,145 ****
462fb2
--- 150,154 ----
462fb2
  		{
462fb2
  		  wpat++;
462fb2
+ 		  bracklen++;
462fb2
  		  in_cclass = 0;
462fb2
  		}
462fb2
***************
462fb2
*** 147,152 ****
462fb2
  		{
462fb2
  		  wpat++;
462fb2
  		  if (*wpat == L']')	/* right bracket can appear as collating symbol */
462fb2
! 		    wpat++;
462fb2
  		  in_collsym = 1;
462fb2
  		}
462fb2
--- 156,165 ----
462fb2
  		{
462fb2
  		  wpat++;
462fb2
+ 		  bracklen++;
462fb2
  		  if (*wpat == L']')	/* right bracket can appear as collating symbol */
462fb2
! 		    {
462fb2
! 		      wpat++;
462fb2
! 		      bracklen++;
462fb2
! 		    }
462fb2
  		  in_collsym = 1;
462fb2
  		}
462fb2
***************
462fb2
*** 154,157 ****
462fb2
--- 167,171 ----
462fb2
  		{
462fb2
  		  wpat++;
462fb2
+ 		  bracklen++;
462fb2
  		  in_collsym = 0;
462fb2
  		}
462fb2
***************
462fb2
*** 159,164 ****
462fb2
  		{
462fb2
  		  wpat++;
462fb2
  		  if (*wpat == L']')	/* right bracket can appear as equivalence class */
462fb2
! 		    wpat++;
462fb2
  		  in_equiv = 1;
462fb2
  		}
462fb2
--- 173,182 ----
462fb2
  		{
462fb2
  		  wpat++;
462fb2
+ 		  bracklen++;
462fb2
  		  if (*wpat == L']')	/* right bracket can appear as equivalence class */
462fb2
! 		    {
462fb2
! 		      wpat++;
462fb2
! 		      bracklen++;
462fb2
! 		    }
462fb2
  		  in_equiv = 1;
462fb2
  		}
462fb2
***************
462fb2
*** 166,174 ****
462fb2
--- 184,196 ----
462fb2
  		{
462fb2
  		  wpat++;
462fb2
+ 		  bracklen++;
462fb2
  		  in_equiv = 0;
462fb2
  		}
462fb2
+ 	      else
462fb2
+ 		bracklen++;
462fb2
  	    }
462fb2
  	  while ((wc = *wpat++) != L']');
462fb2
  	  matlen++;		/* bracket expression can only match one char */
462fb2
+ bad_bracket:
462fb2
  	  break;
462fb2
  	}
462fb2
***************
462fb2
*** 214,219 ****
462fb2
       size_t max;
462fb2
  {
462fb2
!   char c, *brack;
462fb2
!   int matlen, t, in_cclass, in_collsym, in_equiv;
462fb2
  
462fb2
    if (*pat == 0)
462fb2
--- 236,241 ----
462fb2
       size_t max;
462fb2
  {
462fb2
!   char c;
462fb2
!   int matlen, bracklen, t, in_cclass, in_collsym, in_equiv;
462fb2
  
462fb2
    if (*pat == 0)
462fb2
***************
462fb2
*** 255,259 ****
462fb2
  	case '[':
462fb2
  	  /* scan for ending `]', skipping over embedded [:...:] */
462fb2
! 	  brack = pat;
462fb2
  	  c = *pat++;
462fb2
  	  do
462fb2
--- 277,281 ----
462fb2
  	case '[':
462fb2
  	  /* scan for ending `]', skipping over embedded [:...:] */
462fb2
! 	  bracklen = 1;
462fb2
  	  c = *pat++;
462fb2
  	  do
462fb2
***************
462fb2
*** 261,276 ****
462fb2
  	      if (c == 0)
462fb2
  		{
462fb2
! 	          matlen += pat - brack - 1;	/* incremented below */
462fb2
! 	          break;
462fb2
  	        }
462fb2
  	      else if (c == '\\')
462fb2
  		{
462fb2
! 		  c = *pat++;
462fb2
! 		  if (*pat == 0)
462fb2
! 		    break;
462fb2
  		}
462fb2
  	      else if (c == '[' && *pat == ':')	/* character class */
462fb2
  		{
462fb2
  		  pat++;
462fb2
  		  in_cclass = 1;
462fb2
  		}
462fb2
--- 283,306 ----
462fb2
  	      if (c == 0)
462fb2
  		{
462fb2
! 		  pat--;			/* back up to NUL */
462fb2
! 		  matlen += bracklen;
462fb2
! 		  goto bad_bracket;
462fb2
  	        }
462fb2
  	      else if (c == '\\')
462fb2
  		{
462fb2
! 		  /* *pat == backslash-escaped character */
462fb2
! 		  bracklen++;
462fb2
! 		  /* If the backslash or backslash-escape ends the string,
462fb2
! 		     bail.  The ++pat skips over the backslash escape */
462fb2
! 		  if (*pat == 0 || *++pat == 0)
462fb2
! 		    {
462fb2
! 		      matlen += bracklen;
462fb2
! 		      goto bad_bracket;
462fb2
! 		    }
462fb2
  		}
462fb2
  	      else if (c == '[' && *pat == ':')	/* character class */
462fb2
  		{
462fb2
  		  pat++;
462fb2
+ 		  bracklen++;
462fb2
  		  in_cclass = 1;
462fb2
  		}
462fb2
***************
462fb2
*** 278,281 ****
462fb2
--- 308,312 ----
462fb2
  		{
462fb2
  		  pat++;
462fb2
+ 		  bracklen++;
462fb2
  		  in_cclass = 0;
462fb2
  		}
462fb2
***************
462fb2
*** 283,288 ****
462fb2
  		{
462fb2
  		  pat++;
462fb2
  		  if (*pat == ']')	/* right bracket can appear as collating symbol */
462fb2
! 		    pat++;
462fb2
  		  in_collsym = 1;
462fb2
  		}
462fb2
--- 314,323 ----
462fb2
  		{
462fb2
  		  pat++;
462fb2
+ 		  bracklen++;
462fb2
  		  if (*pat == ']')	/* right bracket can appear as collating symbol */
462fb2
! 		    {
462fb2
! 		      pat++;
462fb2
! 		      bracklen++;
462fb2
! 		    }
462fb2
  		  in_collsym = 1;
462fb2
  		}
462fb2
***************
462fb2
*** 290,293 ****
462fb2
--- 325,329 ----
462fb2
  		{
462fb2
  		  pat++;
462fb2
+ 		  bracklen++;
462fb2
  		  in_collsym = 0;
462fb2
  		}
462fb2
***************
462fb2
*** 295,300 ****
462fb2
  		{
462fb2
  		  pat++;
462fb2
  		  if (*pat == ']')	/* right bracket can appear as equivalence class */
462fb2
! 		    pat++;
462fb2
  		  in_equiv = 1;
462fb2
  		}
462fb2
--- 331,340 ----
462fb2
  		{
462fb2
  		  pat++;
462fb2
+ 		  bracklen++;
462fb2
  		  if (*pat == ']')	/* right bracket can appear as equivalence class */
462fb2
! 		    {
462fb2
! 		      pat++;
462fb2
! 		      bracklen++;
462fb2
! 		    }
462fb2
  		  in_equiv = 1;
462fb2
  		}
462fb2
***************
462fb2
*** 302,310 ****
462fb2
--- 342,354 ----
462fb2
  		{
462fb2
  		  pat++;
462fb2
+ 		  bracklen++;
462fb2
  		  in_equiv = 0;
462fb2
  		}
462fb2
+ 	      else
462fb2
+ 		bracklen++;
462fb2
  	    }
462fb2
  	  while ((c = *pat++) != ']');
462fb2
  	  matlen++;		/* bracket expression can only match one char */
462fb2
+ bad_bracket:
462fb2
  	  break;
462fb2
  	}
462fb2
*** ../bash-4.2-patched/patchlevel.h	Sat Jun 12 20:14:48 2010
462fb2
--- patchlevel.h	Thu Feb 24 21:41:34 2011
462fb2
***************
462fb2
*** 26,30 ****
462fb2
     looks for to find the patch level (for the sccs version string). */
462fb2
  
462fb2
! #define PATCHLEVEL 2
462fb2
  
462fb2
  #endif /* _PATCHLEVEL_H_ */
462fb2
--- 26,30 ----
462fb2
     looks for to find the patch level (for the sccs version string). */
462fb2
  
462fb2
! #define PATCHLEVEL 3
462fb2
  
462fb2
  #endif /* _PATCHLEVEL_H_ */