Artifacts Associated With Ticket e2eac30e4192a9d2
Ticket change [cd75bcb693] (rid 199604) by anonymous on 2022-03-24 11:42:26:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
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, ============================================================================================================ - foundin initialized to: "8.7a5"
- is_private initialized to: "0"
- login: "anonymous"
- priority initialized to: "5 Medium"
- private_contact initialized to: "ef1993a98c1daa778fe0b246a7af12b3076f2240"
- resolution initialized to: "None"
- severity initialized to: "Severe"
- status initialized to: "Open"
- submitter initialized to: "anonymous"
- subsystem initialized to: "- New Builtin Commands"
- title initialized to:
incorrect result of convertfrom with 4-byte utf8-characters
- type initialized to: "Bug"
Ticket change [6cc640ec07] (rid 199605) by anonymous on 2022-03-24 11:47:48:
- login: "anonymous"
- mimetype: "text/plain"
- subsystem changed to: "44. UTF-8 Strings"
Ticket change [7b247f5d10] (rid 199630) by jan.nijtmans on 2022-03-24 16:18:35:
- assignee changed to: "jan.nijtmans"
- icomment:
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 [https://core.tcl-lang.org/tcl/timeline?r=full-utf-for-87|full-utf-for-87], but it needs a [https://core.tcl-lang.org/tips/doc/trunk/tip/622.md|TIP]. I already checked: it looks like this bug is - indeed - fixed in [https://core.tcl-lang.org/tcl/timeline?r=full-utf-for-87|full-utf-for-87]: <pre> $ tclsh8.7 % tcl::build-info 8.7a6+e75009fecdf4135dbff928da58b8964909760bc755c7713dc2f6bb4f824c158c.gcc-1102 % encoding convertfrom utf-8 [string repeat \xF0\x9f\x92\x95 51] πππππππππππππππππππππππππππππππππππππππππππππππππππ </pre> Hope this helps, Jan Nijtmans - login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [851c100a91] (rid 199722) by anonymous on 2022-03-29 11:44:28:
- icomment:
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.
- login: "anonymous"
- mimetype: "text/plain"
- icomment:
Ticket change [d050f04be6] (rid 200439) by jan.nijtmans on 2022-04-27 20:37:26:
- icomment:
[https://core.tcl-lang.org/tips/doc/trunk/tip/622.md|TIP #622] is in Voting state now.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [15597b2250] (rid 200865) by jan.nijtmans on 2022-05-17 19:45:09:
- closedate changed to: "2459717.32302838"
- closer changed to: "jan.nijtmans"
- icomment: "Since TIP #622 is merged to Tcl 8.7 now, closing."
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"