|
2022-05-17
| ||
| 19:45 | • Closed ticket [e2eac30e41]: incorrect result of convertfrom with 4-byte utf8-cha... artifact: 15597b2250 user: jan.nijtmans | |
|
2022-04-27
| ||
| 20:37 | • Ticket [e2eac30e41]: 3 changes artifact: d050f04be6 user: jan.nijtmans | |
|
2022-03-29
| ||
| 11:44 | • Ticket [e2eac30e41]: 3 changes artifact: 851c100a91 user: anonymous | |
|
2022-03-24
| ||
| 16:18 | • Ticket [e2eac30e41]: 4 changes artifact: 7b247f5d10 user: jan.nijtmans | |
| 11:47 | • Ticket [e2eac30e41]: 3 changes artifact: 6cc640ec07 user: anonymous | |
| 11:42 | • New ticket [e2eac30e41]. artifact: cd75bcb693 user: anonymous | |
incorrect result of convertfrom with 4-byte utf8-characters
There is a problem in Tcl_ExternalToUtfDString() (and Tcl_UtfToExternalDString()) when it is called with a string containing more than 50 4-byte UTF-8 characters. Below one can see the buffer content before and after the call to Tcl_ExternalToUtfDString(). In the "after" line, 5 bytes from the end there is an incorrect value of x66
before: (len 204): f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95
after: (len 204): f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 95 f0 9f 92 66 f0 9f 92 95
The problem happens, when the static buffer size of Tcl_DString with 200 bytes (50x 4 bytes) runs out and requires the switch to the dynamically allocated buffer.
Below is a fix which circumvents the problem by preallocating the Tcl_DString sufficiently long, such that the buffer growing case does not happen in such cases. This is also better performance-wise for inputs with more than 200 bytes, since it avoids a memcpy() for buffer growing. But the provided fix just avoids that the real issue will kick in.
I have learned about a problem by receiving a buffer overflow bug report for NaviServer, where the setup is non-trivial. Below is a very simple plain tcl case showing the problem (in Tcl 8.7a5)
% encoding convertfrom utf-8 [string repeat \xF0\x9f\x92\x95 49]
πππππππππππππππππππππππππππππππππππππππππππππππππ
% encoding convertfrom utf-8 [string repeat \xF0\x9f\x92\x95 51]
πππππππππππππππππππππππππππππππππππππππππππππππππΓ°ΕΈβπ
With 49 such characters, all is fine. With 51 4-byte chars, one sees the incorrect bytes at the end of the string.
all the best
-gustaf
============================================================================================================
--- tclEncoding.c-original 2022-03-13 15:35:55.000000000 +0100
+++ tclEncoding.c 2022-03-13 15:35:55.000000000 +0100
@@ -1128,10 +1128,6 @@
const Encoding *encodingPtr;
int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars;
- Tcl_DStringInit(dstPtr);
- dst = Tcl_DStringValue(dstPtr);
- dstLen = dstPtr->spaceAvl - 1;
-
if (encoding == NULL) {
encoding = systemEncoding;
}
@@ -1143,6 +1139,12 @@
srcLen = encodingPtr->lengthProc(src);
}
+ Tcl_DStringInit(dstPtr);
+ Tcl_DStringSetLength(dstPtr, srcLen);
+ dst = Tcl_DStringValue(dstPtr);
+ dstLen = dstPtr->spaceAvl - 1;
+ Tcl_DStringSetLength(dstPtr, 0);
+
flags = TCL_ENCODING_START | TCL_ENCODING_END;
if (encodingPtr->toUtfProc == UtfToUtfProc) {
flags |= TCL_ENCODING_MODIFIED | TCL_ENCODING_UTF;
@@ -1320,10 +1322,6 @@
const Encoding *encodingPtr;
int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars;
- Tcl_DStringInit(dstPtr);
- dst = Tcl_DStringValue(dstPtr);
- dstLen = dstPtr->spaceAvl - 1;
-
if (encoding == NULL) {
encoding = systemEncoding;
}
@@ -1334,6 +1332,13 @@
} else if (srcLen < 0) {
srcLen = strlen(src);
}
+
+ Tcl_DStringInit(dstPtr);
+ Tcl_DStringSetLength(dstPtr, srcLen);
+ dst = Tcl_DStringValue(dstPtr);
+ Tcl_DStringSetLength(dstPtr, 0);
+ dstLen = dstPtr->spaceAvl - 1;
+
flags = TCL_ENCODING_START | TCL_ENCODING_END;
while (1) {
result = encodingPtr->fromUtfProc(encodingPtr->clientData, src,
============================================================================================================
Thanks for the bug-report.
The tricky thing is that the encoding functions Tcl_ExternalToUtf() and Tcl_UtfToExternal() should work with a buffersize of TCL_UTF_MAX=3, which is not enough to handle utf-8 sequences of 4-bytes. In Tcl 8.6 that was no problem, because Tcl 8.6 uses the cesu-8 encoding internall, in which such characters are split (internally) in two surrogates. But in Tcl 8.7, this changed, which is causing this bug.
There are two ways to solve it. 1) Implement a small buffer in the channel state. 2) Increase TCL_UTF_MAX to 4 internally.
The second fix is currently ongoing in branch full-utf-for-87, but it needs a TIP.
I already checked: it looks like this bug is - indeed - fixed in full-utf-for-87:
$ tclsh8.7 % tcl::build-info 8.7a6+e75009fecdf4135dbff928da58b8964909760bc755c7713dc2f6bb4f824c158c.gcc-1102 % encoding convertfrom utf-8 [string repeat \xF0\x9f\x92\x95 51] πππππππππππππππππππππππππππππππππππππππππππππππππππ
Hope this helps, Jan Nijtmans
Jan, many thanks, Perfectly, the updated version works fine. From my point of view, this ticket can be closed (not sure, whether the bug submitter should close the ticket, or the maintainer). Let us hope that the fixed version makes it to some release soon.
TIP #622 is in Voting state now.
Since TIP #622 is merged to Tcl 8.7 now, closing.