Overview
| Artifact ID: | cd75bcb693bcd3ed0dd0d37794b2f059df366c683681904d67de60438cc7faf1 |
|---|---|
| Ticket: | e2eac30e4192a9d26fad9b1771d7b391d7e2592b
incorrect result of convertfrom with 4-byte utf8-characters |
| User & Date: | anonymous on 2022-03-24 11:42:26 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/plain"
- comment changed 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 changed to: "8.7a5"
- is_private changed to: "0"
- login: "anonymous"
- priority changed to: "5 Medium"
- private_contact changed to: "ef1993a98c1daa778fe0b246a7af12b3076f2240"
- resolution changed to: "None"
- severity changed to: "Severe"
- status changed to: "Open"
- submitter changed to: "anonymous"
- subsystem changed to: "- New Builtin Commands"
- title changed to:
incorrect result of convertfrom with 4-byte utf8-characters
- type changed to: "Bug"