Index: ChangeLog
===================================================================
RCS file: /cvsroot/tcl/tcl/ChangeLog,v
retrieving revision 1.226.2.103
diff -u -r1.226.2.103 ChangeLog
--- ChangeLog	2002/01/17 20:35:39	1.226.2.103
+++ ChangeLog	2002/01/21 20:33:34
@@ -1,3 +1,18 @@
+2002-01-21  Andreas Kupries  <andreas_kupries@users.sourceforge.net>
+
+	* generic/tclIO.c (WriteChars): Fix for SF #506297, reported by
+	  Martin Forssen <ruric@users.sourceforge.net>. The encoding
+	  chosen in the script exposing the bug writes out three intro
+	  characters when TCL_ENCODING_START is set, but does not consume
+	  any input as TCL_ENCODING_END is cleared. As some output was
+	  generated the enclosing loop calls UtfToExternal again, again
+	  with START set. Three more characters in the out and still no
+	  use of input ... To break this infinite loop we remove
+	  TCL_ENCODING_START from the set of flags after the first call
+	  (no condition is required, the later calls remove an unset flag,
+	  which is a no-op). This causes the subsequent calls to
+	  UtfToExternal to consume and convert the actual input.
+
 2002-01-17  Andreas Kupries  <andreas_kupries@users.sourceforge.net>
 
 	* generic/tclIOCmd.c (Tcl_GetsObjCmd): Fixed bug #504642 as
Index: generic/tclIO.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclIO.c,v
retrieving revision 1.20.2.12
diff -u -r1.20.2.12 tclIO.c
--- generic/tclIO.c	2001/11/07 04:48:14	1.20.2.12
+++ generic/tclIO.c	2002/01/21 20:33:35
@@ -2746,6 +2746,24 @@
 	    Tcl_UtfToExternal(NULL, encoding, stage, stageLen, flags,
 		    &statePtr->outputEncodingState, dst,
 		    dstLen + BUFFER_PADDING, &stageRead, &dstWrote, NULL);
+
+	    /* Fix for SF #506297, reported by Martin Forssen <ruric@users.sourceforge.net>.
+	     *
+	     * The encoding chosen in the script exposing the bug
+	     * writes out three intro characters when
+	     * TCL_ENCODING_START is set, but does not consume any
+	     * input as TCL_ENCODING_END is cleared. As some output
+	     * was generated the enclosing loop calls UtfToExternal
+	     * again, again with START set. Three more characters in
+	     * the out and still no use of input ... To break this
+	     * infinite loop we remove TCL_ENCODING_START from the set
+	     * of flags after the first call (no condition is
+	     * required, the later calls remove an unset flag, which
+	     * is a no-op). This causes the subsequent calls to
+	     * UtfToExternal to consume and convert the actual input.
+	     */
+
+	    flags &= ~TCL_ENCODING_START;
 	    if (stageRead + dstWrote == 0) {
 		/*
 		 * We have an incomplete UTF-8 character at the end of the
Index: tests/io.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/io.test,v
retrieving revision 1.14.2.1
diff -u -r1.14.2.1 io.test
--- tests/io.test	2001/10/09 12:20:17	1.14.2.1
+++ tests/io.test	2002/01/21 20:33:36
@@ -89,6 +89,19 @@
     close $f
     contents test1
 } "a\x93\xe1\x00"
+test io-1.8 {Tcl_WriteChars: WriteChars} {
+    # This test written for SF bug #506297.
+    #
+    # Executing this test without the fix for the referenced bug
+    # applied to tcl will cause tcl, more specifically WriteChars, to
+    # go into an infinite loop.
+
+    set f [open test2 w] 
+    fconfigure      $f -encoding iso2022-jp 
+    puts -nonewline $f [format %s%c [string repeat " " 4] 12399] 
+    close           $f 
+    contents test2
+} "\x1b(B    \x1b\$@\$O"
 
 test io-2.1 {WriteBytes} {
     # loop until all bytes are written
