Skip to content

Commit

Permalink
(adapted) Testcase and fix for [1073daf086]: Bug in handling illegal …
Browse files Browse the repository at this point in the history
…utf-8 sequence
  • Loading branch information
jan.nijtmans committed Sep 12, 2022
1 parent 021ff85 commit e57218b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
19 changes: 8 additions & 11 deletions generic/tclIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -4442,6 +4435,10 @@ Write(
}
}

if (encodingError) {
Tcl_SetErrno(EINVAL);
return -1;
}
return total;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/io.test
Original file line number Diff line number Diff line change
Expand Up @@ -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} {
Expand All @@ -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]
Expand Down

0 comments on commit e57218b

Please sign in to comment.