From dd7cd48fab975a8f107a92c4af405aed06740beb Mon Sep 17 00:00:00 2001 From: mdejong Date: Wed, 27 Dec 2006 01:25:34 +0000 Subject: * generic/tclEncoding.c (EscapeFromUtfProc): Clear the TCL_ENCODING_END flag when end bytes are written. This fix keep this method from writing escape bytes for an encoding like iso2022-jp multiple times when the escape byte overlap with the end of the IO buffer. * tests/io.test: Add test for escape byte overlap issue. --- ChangeLog | 12 ++++++++++ generic/tclEncoding.c | 17 +++++++++++--- tests/io.test | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index b7496e6..ba6b105 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2006-12-26 Mo DeJong + + * generic/tclEncoding.c (EscapeFromUtfProc): + Clear the TCL_ENCODING_END flag when end + bytes are written. This fix keep this method + from writing escape bytes for an encoding + like iso2022-jp multiple times when the + escape byte overlap with the end of the + IO buffer. + * tests/io.test: Add test for escape + byte overlap issue. + 2006-12-19 Donal K. Fellows * unix/tclUnixThrd.c (Tcl_GetAllocMutex, TclpNewAllocMutex): Add diff --git a/generic/tclEncoding.c b/generic/tclEncoding.c index 69c7f28..11c838a 100644 --- a/generic/tclEncoding.c +++ b/generic/tclEncoding.c @@ -8,7 +8,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclEncoding.c,v 1.51 2006/11/13 08:23:07 das Exp $ + * RCS: @(#) $Id: tclEncoding.c,v 1.52 2006/12/27 01:25:35 mdejong Exp $ */ #include "tclInt.h" @@ -2981,7 +2981,7 @@ EscapeFromUtfProc( if (flags & TCL_ENCODING_START) { state = 0; - if (dst + dataPtr->initLen > dstEnd) { + if ((dst + dataPtr->initLen) > dstEnd) { *srcReadPtr = 0; *dstWrotePtr = 0; return TCL_CONVERT_NOSPACE; @@ -3089,7 +3089,17 @@ EscapeFromUtfProc( if ((result == TCL_OK) && (flags & TCL_ENCODING_END)) { unsigned int len = dataPtr->subTables[0].sequenceLen; - if (dst + dataPtr->finalLen + (state?len:0) > dstEnd) { + /* + * Certain encodings like iso2022-jp need to write + * an escape sequence after all characters have + * been converted. This logic checks that enough + * room is available in the buffer for the escape bytes. + * The TCL_ENCODING_END flag is cleared after a final + * escape sequence has been added to the buffer so + * that another call to this method does not attempt + * to append escape bytes a second time. + */ + if ((dst + dataPtr->finalLen + (state?len:0)) > dstEnd) { result = TCL_CONVERT_NOSPACE; } else { if (state) { @@ -3100,6 +3110,7 @@ EscapeFromUtfProc( memcpy((VOID *) dst, (VOID *) dataPtr->final, (size_t) dataPtr->finalLen); dst += dataPtr->finalLen; + state &= ~TCL_ENCODING_END; } } diff --git a/tests/io.test b/tests/io.test index 7d1933e..2ba2183 100644 --- a/tests/io.test +++ b/tests/io.test @@ -13,7 +13,7 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: io.test,v 1.74 2006/11/03 11:45:34 dkf Exp $ +# RCS: @(#) $Id: io.test,v 1.75 2006/12/27 01:25:35 mdejong Exp $ if {[catch {package require tcltest 2}]} { puts stderr "Skipping tests in [info script]. tcltest 2 required." @@ -117,6 +117,66 @@ test io-1.8 {Tcl_WriteChars: WriteChars} { contents $path(test2) } " \x1b\$B\$O\x1b(B" +test io-1.9 {Tcl_WriteChars: WriteChars} { + # When closing a channel with an encoding that appends + # escape bytes, check for the case where the escape + # bytes overflow the current IO buffer. The bytes + # should be moved into a new buffer. + + set data "1234567890 [format %c 12399]" + + set sizes [list] + + # With default buffer size + set f [open $path(test2) w] + fconfigure $f -encoding iso2022-jp + puts -nonewline $f $data + close $f + lappend sizes [file size $path(test2)] + + # With buffer size equal to the length + # of the data, the escape bytes would + # go into the next buffer. + + set f [open $path(test2) w] + fconfigure $f -encoding iso2022-jp -buffersize 16 + puts -nonewline $f $data + close $f + lappend sizes [file size $path(test2)] + + # With buffer size that is large enough + # to hold 1 byte of escaped data, but + # not all 3. This should not write + # the escape bytes to the first buffer + # and then again to the second buffer. + + set f [open $path(test2) w] + fconfigure $f -encoding iso2022-jp -buffersize 17 + puts -nonewline $f $data + close $f + lappend sizes [file size $path(test2)] + + # With buffer size that can hold 2 out of + # 3 bytes of escaped data. + + set f [open $path(test2) w] + fconfigure $f -encoding iso2022-jp -buffersize 18 + puts -nonewline $f $data + close $f + lappend sizes [file size $path(test2)] + + # With buffer size that can hold all the + # data and escape bytes. + + set f [open $path(test2) w] + fconfigure $f -encoding iso2022-jp -buffersize 19 + puts -nonewline $f $data + close $f + lappend sizes [file size $path(test2)] + + set sizes +} {19 19 19 19 19} + test io-2.1 {WriteBytes} { # loop until all bytes are written -- cgit v0.12