Skip to content
Tcl Source Code View Ticket
Login

View Ticket

endless loop in gets on encoding error in non blocking mode

Closed / Fixedc4eb46a196227040bfb18770ac261d6836e58f0eCreated 3 years agoUpdated 3 years ago
Descriptionwiki2023-11-06 14:59:06

Issue

*gets* hangs on a non blocking file channel when an encoding error should be reported.

Example

set f [open test_A_195_B.txt wb]; puts -nonewline $f A\xC3B; close $f
set f [open test_A_195_B.txt r]
fconfigure $f -encoding utf-8 -profile strict -blocking 0
gets $f

A file is created with a non continued UTF-8 multi-byte sequence. The channel is configured to error out on encoding errors. Then, a gets in non-blocking mode is executed.

TCL hangs on the *gets* line.

Environment

  • Branch: core-8-branch, tip-653
  • System: Windows 10 64 bit GER
  • Compiler: MS-VS 2015 on 32 bit

Thank you all ! Harald

oehharwiki2023-11-12 19:38:27

A debugging session showed an endless loop in tclIO.c. The wihile-loop in line 4750 was never exited.

The exit-point of the blocking case was explicitly excempted for the non blocking case. Line 4920 reads:

	} else if (gs.bytesWrote == 0
		&& GotFlag(statePtr, CHANNEL_ENCODING_ERROR)
		&& !GotFlag(statePtr, CHANNEL_NONBLOCKING)) {
	    /* Set eol to the position that caused the encoding error, and then
	     * continue to gotEOL, which stores the data that was decoded
	     * without error to objPtr.  This allows the caller to do something
	     * useful with the data decoded so far, and also results in the
	     * position of the file being the first byte that was not
	     * successfully decoded, allowing further processing at exactly that
	     * point, if desired.
	     */
	    eol = dstEnd;
	    goto gotEOL;
	}

Removing the condition for NONBLOCKING cures the endless loop.

Commit [831c9a8797] in branch bug-c4eb46a1 implements this with an added test.

This is successful for the error sequence "A_195_B" (as described above). Unfortunately, it is not successful for the error sequence "A\x81", what is used in the original test io-75.6. In this case, the gets returns successfully without error message.

So, two tests were added for both cases (io76.6.1 and io-76.6.2) with commit [90ac5ff8f7].

oehharwiki2023-11-13 16:20:20

It is also effective for the 2nd error sequence. The 2nd error sequence is an incomplete UTF-8 sequence. Then, the end of file will trigger the error, which is reached by a second "gets" command.

The test is adopted to this double "gets".

An additional test to check the recovery capabilities is added. There, the encoding is changed to binary and the gets succeeds.

The branch is merged to core-8-branch by commit [94257f6465].

Now, two new tests fail, which looks unrelated:

==== zlib-8.21 zlib transformation, bug f9eafc3886 FAILED
==== Contents of test case:

    set f [zlib push gzip [open $file w] -header [list comment \u100]]

---- Test completed normally; Return code was: 0
---- Return code should have been one of: 1
==== zlib-8.21 FAILED

---- zlib-8.22 start


==== zlib-8.22 zlib transformation, bug f9eafc3886 FAILED
==== Contents of test case:

    set f [zlib push gzip [open $file w] -header [list filename \u100]]

---- Test completed normally; Return code was: 0
---- Return code should have been one of: 1
==== zlib-8.22 FAILED

I may do another debugger session to find out why.

But the endless loop is cured, which is more important than a failing test.

oehharwiki2023-11-13 16:25:52

Merged to main with commit [054ebbf73b]. No additional failing tests. So, closing this ticket.

Thank you all, Harald

oehhar Ticket [c4eb46a196]: 4 changes [History diff]2023-11-13 16:27:52
Log in (anonymous is fine) to add a comment.