Skip to content

Commit

Permalink
Fix for [eabcbd08274f2d22], [read] error persists on channel with strict
Browse files Browse the repository at this point in the history
encoding after encoding is changed to binary.
  • Loading branch information
pooryorick committed Apr 16, 2023
1 parent 416b889 commit bc4ced9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion generic/tclIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -4616,7 +4616,7 @@ Tcl_GetsObj(

if (GotFlag(statePtr, CHANNEL_ENCODING_ERROR)) {
UpdateInterest(chanPtr);
ResetFlag(statePtr, CHANNEL_ENCODING_ERROR);
ResetFlag(statePtr, CHANNEL_EOF|CHANNEL_BLOCKED);
Tcl_SetErrno(EILSEQ);
return TCL_INDEX_NONE;
}
Expand Down Expand Up @@ -5941,6 +5941,7 @@ DoReadChars(
int factor = UTF_EXPANSION_FACTOR;

if (GotFlag(statePtr, CHANNEL_ENCODING_ERROR)) {
ResetFlag(statePtr, CHANNEL_EOF|CHANNEL_STICKY_EOF|CHANNEL_BLOCKED);
/* TODO: We don't need this call? */
UpdateInterest(chanPtr);
Tcl_SetErrno(EILSEQ);
Expand Down Expand Up @@ -6117,6 +6118,7 @@ DoReadChars(
* succesfully red before the error. Return an error so that callers
* like [read] can also return an error.
*/
ResetFlag(statePtr, CHANNEL_EOF|CHANNEL_ENCODING_ERROR);
Tcl_SetErrno(EILSEQ);
copied = -1;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/io.test
Original file line number Diff line number Diff line change
Expand Up @@ -9322,6 +9322,30 @@ test io-75.8 {invalid utf-8 encoding eof handling (-profile strict)} -setup {
removeFile io-75.8
} -result {41 1 {}}

test io-75.8.eoflater {invalid utf-8 encoding eof handling (-profile strict)} -setup {
set res {}
set fn [makeFile {} io-75.8]
set f [open $fn w+]
fconfigure $f -encoding binary
# \x81 is invalid in utf-8. -eofchar is not detected, because it comes later.
puts -nonewline $f A\x81\x1A
flush $f
seek $f 0
fconfigure $f -encoding utf-8 -buffering none -eofchar \x1A \
-translation lf -profile strict
} -body {
after 1
set status [catch {read $f} cres copts]
lappend res $status
lappend res [eof $f]
chan configure $f -encoding iso8859-1
lappend res [read $f]
close $f
set res
} -cleanup {
removeFile io-75.8
} -result "1 0 \x81"

test io-75.9 {unrepresentable character write passes and is replaced by ?} -setup {
set fn [makeFile {} io-75.9]
set f [open $fn w+]
Expand Down

0 comments on commit bc4ced9

Please sign in to comment.