|
2022-07-14
| ||
| 11:58 | • Ticket [b79df322a9] Tcl_NewUnicodeObj truncates strings status still Closed ... artifact: b745e036be user: jan.nijtmans | |
| 10:53 | Same bug-fix as [b79df322a9], but then for Tcl_AppendUnicodeToObj() check-in: f3a9189fb4 user: jan.nijtmans tags: core-8-branch | |
|
2022-07-13
| ||
| 14:45 | • Closed ticket [b79df322a9]: Tcl_NewUnicodeObj truncates strings plus 6 other... artifact: 2140d1b594 user: jan.nijtmans | |
|
2022-07-12
| ||
| 12:31 | Fix [b79df322a9]. Tcl_GetUnicode/Tcl_NewUnicodeObj crash. Add tests check-in: f562dcb464 user: apnadkarni tags: core-8-branch | |
| 12:25 | • Ticket [b79df322a9] Tcl_NewUnicodeObj truncates strings status still Open wi... artifact: bd4d6ef1c0 user: jan.nijtmans | |
| 12:23 | Fix [b79df322a9] follow-up: Tcl_NewUnicodeObj truncates strings closed check-in: 0e4cbe7bef user: jan.nijtmans tags: bug-b79df322a9 | |
| 11:50 | • Ticket [b79df322a9] Tcl_NewUnicodeObj truncates strings status still Open wi... artifact: ac6bf210a9 user: jan.nijtmans | |
| 11:21 | • Open ticket [b79df322a9]. artifact: 2f7d97db77 user: apnadkarni | |
| 11:20 | • Ticket [b79df322a9]: 4 changes artifact: 926cf93eb8 user: apnadkarni | |
| 10:37 | • Ticket [b79df322a9]: 4 changes artifact: 9b1ec5a041 user: apnadkarni | |
|
2022-06-30
| ||
| 13:05 | • Closed ticket [b79df322a9]. artifact: 8973d8fd3c user: apnadkarni | |
| 13:04 | • Ticket [b79df322a9]: 3 changes artifact: 9eb1d9bd28 user: apnadkarni | |
| 12:58 | • Pending ticket [b79df322a9]. artifact: bded36d755 user: jan.nijtmans | |
| 12:08 | • Open ticket [b79df322a9]. artifact: d8fd460018 user: apnadkarni | |
| 11:35 | • Closed ticket [b79df322a9]. artifact: f5f8b1c874 user: jan.nijtmans | |
| 11:31 | Fix [b79df322a9]: Tcl_NewUnicodeObj truncates strings check-in: 936c4f4b1c user: jan.nijtmans tags: core-8-branch | |
|
2022-06-29
| ||
| 14:04 | • Ticket [b79df322a9] Tcl_NewUnicodeObj truncates strings status still Open wi... artifact: f4c7f39285 user: apnadkarni | |
| 14:03 | • Ticket [b79df322a9]: 3 changes artifact: cc12a24c71 user: apnadkarni | |
| 14:01 | • New ticket [b79df322a9]. artifact: 0ce01f1766 user: apnadkarni | |
Tcl_NewUnicodeObj truncates strings
The Tcl_NewUnicodeObj function truncates passed in strings to half their length. Culprit is a memcpy of nchars instead of of nchars*sizeof(unsigned short). However, it is not clear to me that this is the only issue in that module. The new String internal rep hosts the string in the unicode[] array, typed as Tcl_UniChar. The Tcl_NewUnicodeObj on the other hand manipulates data as unsigned short, not Tcl_UniChar. I assume this is because Tcl_NewUnicodeObj is intended to deal with UTF-16 in all cases, even when Tcl_UniChar is 4 bytes (TCL_UTF_MAX==4). If that is the case, then the line stringPtr->unicode[numChars] = 0; will overwrite memory since the allocation is done assuming 2-byte units and sizeof(unicode[0]) is 4 bytes. On the other hand, if Tcl_UniChar is always 2 bytes then why is the function defined using unsigned shorts. I have similar doubts about other usage in the module. For example in SetUTF16StringFromAny (around line 215), the code is allocating in unsigned short units but terminating as an array of Tcl_UniChar. So also Tcl_SetUnicodeObj. May be others, I did not check the whole module. I have not provided a patch because I'm not sure if my analysis is correct (other than the memcpy fix). Can the String.unicode[] array contain either 2-byte or 4-byte units? If so distinguishing using a union may be clearer.
f3a9189fb4Same bug-fix as [b79df322a9], but then for Tcl_AppendUnicodeToObj() check-in2022-07-14 10:53:55f562dcb464Fix [b79df322a9]. Tcl_GetUnicode/Tcl_NewUnicodeObj crash. Add tests check-in2022-07-12 12:31:570e4cbe7befFix [b79df322a9] follow-up: Tcl_NewUnicodeObj truncates strings check-in2022-07-12 12:23:54936c4f4b1cFix [b79df322a9]: Tcl_NewUnicodeObj truncates strings check-in2022-06-30 11:31:15
As an aside, I was surprised the test suite does not catch such an obvious bug. Grepping the code, looks like the function is never used internally.
Thanks for catching this bug. Indeed, the new Tcl_NewUnicodeObj() is not used internally any more, that made it possible for this bug to slip through. It came in through TIP #622, which is - apparently - not tested enough yet.
Jan, Re-opening because the second part of my ticket was not addressed. Is the line stringPtr->unicode[numChars] = 0; not a problem when Tcl_UniChar maps to an int? If not, why not? /Ashok
See: tclStringRep.h.
Answer: 'unicode' here is an array of 'unsigned short', not an array of Tcl_UniChar's
Oops. Mea culpa. I was looking at the UniCharString structure in tclStringObj.c which has it as array of Tcl_UniChar
I added testunicodestring command to the test suite to ensure Tcl_*Unicode* is tested as Tcl itself does not use it. Re-opening for the record because I now get a crash. Investigating...
Ok, there are multiple issues.
First, Tcl_GetUnicodeFromObj is missing a numChars < 0 test.
Second, I take back my mea culpa. I was right earlier. TclGetUnicodeFromObj (which takes unsigned short strings) calls SetStringFromAny which converts the object's internal representation to a UniCharString, not a String which is what TclGetUnicodeFromObj expects with obviously errnoneous results.
Fixing and adding tests.
Indeed, this should have been:
Index: generic/tclStringObj.c
==================================================================
--- generic/tclStringObj.c
+++ generic/tclStringObj.c
@@ -982,11 +982,11 @@
* rep's unichar length should be stored. If
* NULL, no length is stored. */
{
String *stringPtr;
- SetStringFromAny(NULL, objPtr);
+ SetUTF16StringFromAny(NULL, objPtr);
stringPtr = GET_STRING(objPtr);
if (lengthPtr != NULL) {
*lengthPtr = stringPtr->numChars;
}
It should even be, slighty more complicated: [0e4cbe7bef99f2a7]
Should be fixed [here]
Same bugfix: [f3a9189fb47b2491], but now for Tcl_AppendUnicodeToObj() too.