diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2022-12-09 23:23:24 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2022-12-09 23:23:24 (GMT) |
commit | 4aa0db664d74cf15c8c60532ee55a42877181c4a (patch) | |
tree | f095c765d93ba9d7cf4d680d54527de3c6fd5b52 | |
parent | 4f69a6f297bc3013c0c00a24db52fd34a59f902e (diff) | |
parent | b81e0a54597c2f425003b55b489acbdb1f9babeb (diff) | |
download | tcl-4aa0db664d74cf15c8c60532ee55a42877181c4a.zip tcl-4aa0db664d74cf15c8c60532ee55a42877181c4a.tar.gz tcl-4aa0db664d74cf15c8c60532ee55a42877181c4a.tar.bz2 |
Fix [b8f575aa23]: eof erroneously flagged with not reported encoding read errorjan-b8f575aa23
-rw-r--r-- | generic/tclIO.c | 4 | ||||
-rw-r--r-- | tests/io.test | 43 |
2 files changed, 45 insertions, 2 deletions
diff --git a/generic/tclIO.c b/generic/tclIO.c index 8e4ecee..8a551f3 100644 --- a/generic/tclIO.c +++ b/generic/tclIO.c @@ -7588,7 +7588,7 @@ Tcl_Eof( ChannelState *statePtr = ((Channel *) chan)->state; /* State of real channel structure. */ - return GotFlag(statePtr, CHANNEL_EOF) ? 1 : 0; + return (GotFlag(statePtr, CHANNEL_EOF) && !GotFlag(statePtr, CHANNEL_ENCODING_ERROR)) ? 1 : 0; } /* @@ -8283,7 +8283,7 @@ Tcl_SetChannelOption( statePtr->inputEncodingFlags = TCL_ENCODING_START; statePtr->outputEncodingState = NULL; statePtr->outputEncodingFlags = TCL_ENCODING_START; - ResetFlag(statePtr, CHANNEL_NEED_MORE_DATA); + ResetFlag(statePtr, CHANNEL_NEED_MORE_DATA|CHANNEL_ENCODING_ERROR); UpdateInterest(chanPtr); return TCL_OK; } else if (HaveOpt(2, "-eofchar")) { diff --git a/tests/io.test b/tests/io.test index c58bbce..65ebcbd 100644 --- a/tests/io.test +++ b/tests/io.test @@ -9065,6 +9065,49 @@ test io-75.6 {invalid utf-8 encoding read is not ignored (-strictencoding 1)} -s removeFile io-75.6 } -match glob -result {41 1 {error reading "*": illegal byte sequence}} +test io-75.7 {invalid utf-8 encoding eof handling (-strictencoding 1)} -setup { + set fn [makeFile {} io-75.7] + set f [open $fn w+] + fconfigure $f -encoding binary + # \xA1 is invalid in utf-8. -eofchar is not detected, because it comes later. + puts -nonewline $f "A\xA1\x1A" + flush $f + seek $f 0 + fconfigure $f -encoding utf-8 -buffering none -eofchar \x1A -translation lf -strictencoding 1 +} -body { + set d [read $f] + binary scan $d H* hd + lappend hd [eof $f] + lappend hd [catch {read $f} msg] + lappend hd $msg + fconfigure $f -encoding iso8859-1 + lappend hd [read $f];# We changed encoding, so now we can read the \xA1 + close $f + set hd +} -cleanup { + removeFile io-75.6 +} -match glob -result {41 0 1 {error reading "*": illegal byte sequence} ยก} + +test io-75.8 {invalid utf-8 encoding eof handling (-strictencoding 1)} -setup { + set fn [makeFile {} io-75.8] + set f [open $fn w+] + fconfigure $f -encoding binary + # \x81 is invalid in utf-8, but since \x1A comes first, -eofchar takes precedence. + puts -nonewline $f "A\x1A\x81" + flush $f + seek $f 0 + fconfigure $f -encoding utf-8 -buffering none -eofchar \x1A -translation lf -strictencoding 1 +} -body { + set d [read $f] + binary scan $d H* hd + lappend hd [eof $f] + lappend hd [read $f] + close $f + set hd +} -cleanup { + removeFile io-75.6 +} -result {41 1 {}} + # ### ### ### ######### ######### ######### |