Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Tcl_NewUnicodeObj truncates strings

Closed / Fixedb79df322a927dee6517ed87b918b02117d4a8d2eCreated 4 years agoUpdated 4 years ago
Descriptionplain2022-06-29 14:01:55
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.
Referenced by4
apnadkarniplain2022-06-29 14:03:17
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.
apnadkarni Ticket [b79df322a9]: 4 changes [History diff]2022-06-29 14:04:28
jan.nijtmanswiki2022-06-30 11:35:04

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.

apnadkarniplain2022-06-30 12:08:11
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
jan.nijtmanswiki2022-06-30 12:58:45

See: tclStringRep.h.

Answer: 'unicode' here is an array of 'unsigned short', not an array of Tcl_UniChar's

apnadkarniplain2022-06-30 13:04:55
Oops. Mea culpa. I was looking at the UniCharString structure in tclStringObj.c which has it as array of Tcl_UniChar
apnadkarni Closed ticket [b79df322a9]. [History diff]2022-06-30 13:05:12
apnadkarniplain2022-07-12 10:37:22
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...
apnadkarnimarkdown2022-07-12 11:20:56

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.

apnadkarni Open ticket [b79df322a9]. [History diff]2022-07-12 11:21:28
jan.nijtmanswiki2022-07-12 11:50:52

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; }

jan.nijtmanswiki2022-07-12 12:25:19

It should even be, slighty more complicated: [0e4cbe7bef99f2a7]

jan.nijtmanswiki2022-07-13 14:45:19

Should be fixed [here]

jan.nijtmanswiki2022-07-14 11:58:35

Same bugfix: [f3a9189fb47b2491], but now for Tcl_AppendUnicodeToObj() too.

Log in (anonymous is fine) to add a comment.