Index: generic/tclCmdMZ.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclCmdMZ.c,v
retrieving revision 1.44
diff -b -c -r1.44 tclCmdMZ.c
*** generic/tclCmdMZ.c	2001/09/13 11:56:19	1.44
--- generic/tclCmdMZ.c	2001/09/13 23:44:54
***************
*** 471,477 ****
      int start, end, subStart, subEnd, match;
      Tcl_RegExp regExpr;
      Tcl_RegExpInfo info;
!     Tcl_Obj *resultPtr, *varPtr, *objPtr;
      Tcl_UniChar ch, *wsrc, *wfirstChar, *wstring, *wsubspec, *wend;
  
      static char *options[] = {
--- 471,477 ----
      int start, end, subStart, subEnd, match;
      Tcl_RegExp regExpr;
      Tcl_RegExpInfo info;
!     Tcl_Obj *resultPtr, *subPtr, *objPtr;
      Tcl_UniChar ch, *wsrc, *wfirstChar, *wstring, *wsubspec, *wend;
  
      static char *options[] = {
***************
*** 553,572 ****
  
      objv += idx;
  
      /*
!      * Get the length of the string that we are matching before
!      * getting the regexp to avoid shimmering problems.
       */
  
      objPtr	= objv[1];
      wstring	= Tcl_GetUnicodeFromObj(objPtr, &wlen);
!     wsubspec	= Tcl_GetUnicodeFromObj(objv[2], &wsublen);
!     varPtr	= objv[3];
! 
!     regExpr = Tcl_GetRegExpFromObj(interp, objv[0], cflags);
!     if (regExpr == NULL) {
! 	return TCL_ERROR;
      }
  
      result = TCL_OK;
      resultPtr = Tcl_NewUnicodeObj(wstring, 0);
--- 553,581 ----
  
      objv += idx;
  
+     regExpr = Tcl_GetRegExpFromObj(interp, objv[0], cflags);
+     if (regExpr == NULL) {
+ 	return TCL_ERROR;
+     }
+ 
      /*
!      * Make sure to avoid problems where the objects are shared.  This
!      * can cause RegExpObj <> UnicodeObj shimmering that causes data
!      * corruption.  [Bug #461322]
       */
  
+     if (objv[1] == objv[0]) {
+ 	objPtr = Tcl_DuplicateObj(objv[1]);
+     } else {
  	objPtr = objv[1];
+     }
      wstring = Tcl_GetUnicodeFromObj(objPtr, &wlen);
!     if (objv[2] == objv[0]) {
! 	subPtr = Tcl_DuplicateObj(objv[2]);
!     } else {
! 	subPtr = objv[2];
      }
+     wsubspec = Tcl_GetUnicodeFromObj(subPtr, &wsublen);
  
      result = TCL_OK;
      resultPtr = Tcl_NewUnicodeObj(wstring, 0);
***************
*** 598,604 ****
  	    break;
  	}
  	if ((numMatches == 0) && (offset > 0)) {
! 	    /* Copy the initial portion of the string in if an offset
  	     * was specified.
  	     */
  	    Tcl_AppendUnicodeToObj(resultPtr, wstring, offset);
--- 607,614 ----
  	    break;
  	}
  	if ((numMatches == 0) && (offset > 0)) {
! 	    /*
! 	     * Copy the initial portion of the string in if an offset
  	     * was specified.
  	     */
  	    Tcl_AppendUnicodeToObj(resultPtr, wstring, offset);
***************
*** 695,703 ****
      } else if (offset < wlen) {
  	Tcl_AppendUnicodeToObj(resultPtr, wstring + offset, wlen - offset);
      }
!     if (Tcl_ObjSetVar2(interp, varPtr, NULL, resultPtr, 0) == NULL) {
  	Tcl_AppendResult(interp, "couldn't set variable \"",
! 		Tcl_GetString(varPtr), "\"", (char *) NULL);
  	result = TCL_ERROR;
      } else {
  	/*
--- 705,713 ----
      } else if (offset < wlen) {
  	Tcl_AppendUnicodeToObj(resultPtr, wstring + offset, wlen - offset);
      }
!     if (Tcl_ObjSetVar2(interp, objv[3], NULL, resultPtr, 0) == NULL) {
  	Tcl_AppendResult(interp, "couldn't set variable \"",
! 		Tcl_GetString(objv[3]), "\"", (char *) NULL);
  	result = TCL_ERROR;
      } else {
  	/*
***************
*** 709,714 ****
--- 719,726 ----
      }
  
      done:
+     if (objv[1] == objv[0]) { Tcl_DecrRefCount(objPtr); }
+     if (objv[2] == objv[0]) { Tcl_DecrRefCount(subPtr); }
      Tcl_DecrRefCount(resultPtr);
      return result;
  }
