From accc6b2ae999a22f2993b26e6a980a0f9a8fe0c2 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Mon, 12 Sep 2022 14:28:58 +0000 Subject: (adapted) Testcase and fix for [1073daf086]: Bug in handling illegal utf-8 sequence --- generic/tclIO.c | 19 ++++++++----------- tests/io.test | 4 ++-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/generic/tclIO.c b/generic/tclIO.c index 6b7ccdf..d228d50 100644 --- a/generic/tclIO.c +++ b/generic/tclIO.c @@ -4278,6 +4278,7 @@ Write( char *nextNewLine = NULL; int endEncoding, saved = 0, total = 0, flushed = 0, needNlFlush = 0; char safe[BUFFER_PADDING]; + int encodingError = 0; if (srcLen) { WillWrite(chanPtr); @@ -4294,7 +4295,7 @@ Write( nextNewLine = (char *)memchr(src, '\n', srcLen); } - while (srcLen + saved + endEncoding > 0) { + while (srcLen + saved + endEncoding > 0 && !encodingError) { ChannelBuffer *bufPtr; char *dst; int result, srcRead, dstLen, dstWrote, srcLimit = srcLen; @@ -4335,16 +4336,8 @@ Write( statePtr->outputEncodingFlags &= ~TCL_ENCODING_START; if ((result != TCL_OK) && (srcRead + dstWrote == 0)) { - /* - * We're reading from invalid/incomplete UTF-8. - */ - - ReleaseChannelBuffer(bufPtr); - if (total == 0) { - Tcl_SetErrno(EINVAL); - return -1; - } - break; + encodingError = 1; + result = TCL_OK; } bufPtr->nextAdded += dstWrote; @@ -4442,6 +4435,10 @@ Write( } } + if (encodingError) { + Tcl_SetErrno(EINVAL); + return -1; + } return total; } diff --git a/tests/io.test b/tests/io.test index edadfed..94d8764 100644 --- a/tests/io.test +++ b/tests/io.test @@ -8434,7 +8434,7 @@ test io-60.1 {writing illegal utf sequences} {fileevent testbytestring} { set out [open $path(script) w] puts $out "catch {load $::tcltestlib Tcltest}" puts $out { - puts [testbytestring \xE2] + puts ABC[testbytestring \xE2] exit 1 } proc readit {pipe} { @@ -8458,7 +8458,7 @@ test io-60.1 {writing illegal utf sequences} {fileevent testbytestring} { # cut of the remainder of the error stack, especially the filename set result [lreplace $result 3 3 [lindex [split [lindex $result 3] \n] 0]] list $x $result -} {1 {gets {} catch {error writing "stdout": invalid argument}}} +} {1 {gets ABC catch {error writing "stdout": invalid argument}}} test io-61.1 {Reset eof state after changing the eof char} -setup { set datafile [makeFile {} eofchar] -- cgit v0.12