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.