Skip to content
Tcl Source Code View Ticket
Login

View Ticket

fcopy and encoding errors: no partial write, no transparency write

Closed / Fixeda173f9229fe2fec80dc8d43a16c1d7366fb11945Created 3 years agoUpdated 3 years ago
Descriptionwiki2023-11-06 09:08:05
I am currently writing docs for channel encoding errors. I don't understand, what happens with fcopy. So, allow me, to post this test-report style bug report.

I have made tests with tip-653 branch (which is very similar to core-8-branch, 7 lines diff) with fcopy and encoding errors.

I imagined 3 cases:

  • Read encoding error with different encodings
  • Read encoding error with identical encodings and translation
  • Write encoding errors with different encodings

The test session is as follows:

Test file creation:

In UTF-8, this is at position 0: "A", at position 1: 195, which announces a multi-byte sequence, at position 2 a 65 (ASCII B), which does not continue the multi-byte sequence and thus causes a read encoding error
set f [open test_A_195_B.txt wb]; puts -nonewline $f A\xC3B; close $f

Read encoding error with different encodings

% set fr [open test_A_195_B.txt r]
file12af8f0
% set fw [open out.txt w]
file12af430
% fconfigure $fr -encoding utf-8 -profile strict -translation lf
% fconfigure $fw -encoding unicode -profile strict -translation lf
% catch {fcopy $fr $fw} e d
1
% set d
-code 1 -level 0
-errorstack {INNER {invokeStk1 fcopy file12af8f0 file12af430}}
-errorcode {POSIX EILSEQ {invalid or incomplete multibyte or wide character}}
-errorinfo {...} -errorline 1
% tell $fr
1
% flush $fw
% tell $fw
0
% close $fr
% close $fw

So, the file pointer is advanced by 1 on the read side. But the correct first byte is not written to the destination file.

IMHO, the destination file should contain "A" and the file pointer should be advanced.

Read encoding error with same encodings

Within this test, the only difference to the upper is the fconfigure of the write side:

% fconfigure $fw -encoding utf-8 -profile strict -translation lf

The result is the same: encoding error thrown, read position advanced.

IMHO, the fcopy page states, that with the same parameters (encoding, translation), data is just copyied and no error detection is done. This is not the case. The error is detected.

Encdoing error on write

A file is written, which has a unicode character \u2022 not present in iso8859-1. Then, this file is read with utf-8 encoding and written with iso-8859-1:

% set f [open test_A_2022_B.txt w]; fconfigure $f -encoding utf-8; puts -nonewline $f A\u2022B; close $f
% set fr [open test_A_2022_B.txt r]
file12af870
% set fw [open out.txt w]
file1277800
% fconfigure $fr -encoding utf-8 -profile strict -translation lf
% fconfigure $fw -encoding iso8859-1 -profile strict -translation lf
% catch {fcopy $fr $fw} e d
1
% set d
-code 1 -level 0
-errorstack {INNER {invokeStk1 fcopy file12af870 file1277800}}
-errorcode {POSIX EILSEQ {invalid or incomplete multibyte or wide character}} -errorinfo {...} -errorline 1
% tell $fr
5
% tell $fw
1
% close $fr
% close $fw

The one correct by is written. It is interesting, that the read file position is at 5, while it should be at 1.

Thank you all, Harald

Referenced by3
  • c09d923779Merge [47aed25e8b8a4e42]: Fix [a173f9229f]: fcopy man page: document encoding error behaviour. Depreciate tcl8 encoding profile and same encoding = binary mode. check-in2024-08-24 10:21:10
  • 47aed25e8bTicket [a173f9229f]: fcopy man page: document encoding error behaviour. Depreciate tcl8 encoding profile and same encoding = binary mode check-in2023-11-20 16:45:15
  • 184645bd71 Ticket [a173f9229f]: fcopy man page: document encoding error behaviour. Depreciate tcl8 encoding profile and same encoding = binary mode. check-in2023-11-20 16:31:09
oehharwiki2023-11-13 17:47:32

I think, issue 1 and 3 are bugs. Issue 2 may be a documentation issue.

Starting bug branch [bug-a173f922-fcopy-tell] with a first test with commit [8d4d3146c9].

jan.nijtmanswiki2023-11-16 21:57:27

I agree that 1 is a bug and 2 is a documentation issue.

However, 3 is - indeed - a possibility for data loss. After reading a block of data, the pointer cannot be put back to its original file position (it might come from a socket). So, IMHO, it would be best to use the "-data" key as well for this situation.

This can only happen when all of these are true: - the output channel "-profile" is "strict" - the input and output encoding are different - the output encoding is not UTF-8/UTF-16/UTF-32

So, if any of those 3 conditions is false, we could use the "-data" key here too.

I think this situation is rare: Why would someone want to convert from UTF-8 to another (less capable) encoding? Converting from another encoding to UTF-8 will be much more common.

oehharwiki2023-11-17 08:02:51

Hi Jan,

thank you for the good thoughts.

Even if there is data loss possible on the 3rd case, I would just accept that data loss.

The fcopy logically as:

if {[catch {read $rf} data]} {
    # read error handling
} elseif {[catch {puts -nonewline $wf $data} err]} {
    # write data error
}  

Currently, if we loose data on the puts, it is lost - point.

So, I would go to document for fcopy on encoding errors:

  • On read encoding errors, the data before the error is written. The current read file pointer is on the error position. A recovery is possible by fixing the issue and continue like:
fconfigure $rf -encoding binary
fcopy $rf $wf
  • On write encoding errors, the data before the write error is written. The data causing the error and eventual following data is lost.

Is there a way, that a script may make the difference between read and write encoding error? Both will throw "EILSEQ" error and have the same error message.

Thank you and take care, Harald

oehharwiki2023-11-18 18:15:24

For the same encoding case, the code to detect "verbatim copy" is as follows (tclIO.c Line 9496):

   moveBytes = inStatePtr->inEofChar == '\0'	/* No eofChar to stop input */
	    && inStatePtr->inputTranslation == TCL_TRANSLATE_LF
	    && outStatePtr->outputTranslation == TCL_TRANSLATE_LF
	    && inStatePtr->encoding == outStatePtr->encoding
	    && ENCODING_PROFILE_GET(inStatePtr->inputEncodingFlags) == TCL_ENCODING_PROFILE_TCL8
	    && ENCODING_PROFILE_GET(outStatePtr->outputEncodingFlags) == TCL_ENCODING_PROFILE_TCL8;

So, the conditions are:

  • No EOF char
  • Translation: LF
  • input encoding equal output encoding
  • profile: TCL8

I am not sure, if this should not be profile agnostic. Specially, profile TCL8 may disappear and I don't like to set it anywhere.

But anyway, that is the current code.

Lets document it like that and point.

Take care, Harald

oehharwiki2023-11-20 13:26:19

First bug fixed by commit [b9f611b9a0]. The remaining issues are documentation issues.

oehharwiki2023-11-20 14:47:42

I have thought about the case with same encodings.

The verbatim copy should only happen, if both encodings are binary.

For legacy reasons, it happens for any encoding in tcl8 profile. This may be removed in future versions.

I think, I will document it like that.

Thank you and take care, Harald

oehharwiki2023-11-20 17:03:51

Documentation added to core-8-branch and main. Closing the ticket.

oehhar Closed ticket [a173f9229f]. [History diff]2023-11-20 17:04:02
Log in (anonymous is fine) to add a comment.