Tcl Source Code

Artifact [6020ef0e70]
Login

Artifact 6020ef0e7064dc729962815cfc68b806dc22f333:

Attachment "506297-84.diff" to ticket [506297ffff] added by andreas_kupries 2002-01-22 03:36:39.
Index: ChangeLog
===================================================================
RCS file: /cvsroot/tcl/tcl/ChangeLog,v
retrieving revision 1.806
diff -u -r1.806 ChangeLog
--- ChangeLog	2002/01/21 16:15:02	1.806
+++ ChangeLog	2002/01/21 20:29:51
@@ -1,3 +1,18 @@
+2002-01-21  Andreas Kupries  <[email protected]>
+
+	* generic/tclIO.c (WriteChars): Fix for SF #506297, reported by
+	  Martin Forssen <[email protected]>. 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-21  Don Porter <[email protected]>
 
 	* generic/tcl.decls (Tcl_TraceCommand,Tcl_UntraceCommand,
Index: generic/tclIO.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclIO.c,v
retrieving revision 1.47
diff -u -r1.47 tclIO.c
--- generic/tclIO.c	2002/01/17 04:37:33	1.47
+++ generic/tclIO.c	2002/01/21 20:29:53
@@ -3129,6 +3129,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 <[email protected]>.
+	     *
+	     * 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.22
diff -u -r1.22 io.test
--- tests/io.test	2001/12/17 22:55:51	1.22
+++ tests/io.test	2002/01/21 20:29:55
@@ -85,6 +85,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