| Ticket UUID: | b79df322a927dee6517ed87b918b02117d4a8d2e | ||
| Title: | Tcl_NewUnicodeObj truncates strings | ||
| Type: | Bug | Created on: | 2022-06-29 14:01:55 |
| Submitter: | apnadkarni | Assigned to: | jan.nijtmans |
| Subsystem: | - New Builtin Object Types | Severity: | Critical |
| Priority: | 9 Immediate | Last modified: | 2022-07-14 11:58:35 |
| Status: | Closed | Closed by: | jan.nijtmans |
| Resolution: | Fixed | Closed on: | 2022-07-14 11:58:35 |
| Version: | 8.7 | ||
| Description: | ||||
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. | ||||
| User Comments: | ||||
apnadkarni added on 2022-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. jan.nijtmans added on 2022-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. apnadkarni added on 2022-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.nijtmans added on 2022-06-30 12:58:45:
See: tclStringRep.h. Answer: 'unicode' here is an array of 'unsigned short', not an array of Tcl_UniChar's apnadkarni added on 2022-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 added on 2022-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... apnadkarni added on 2022-07-12 11:20:56:
Ok, there are multiple issues. First, Second, I take back my mea culpa. I was right earlier. Fixing and adding tests. jan.nijtmans added on 2022-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;
jan.nijtmans added on 2022-07-12 12:25:19:
It should even be, slighty more complicated: [0e4cbe7bef99f2a7] jan.nijtmans added on 2022-07-13 14:45:19:
Should be fixed [6c065977dc05df50|here] jan.nijtmans added on 2022-07-14 11:58:35:
Same bugfix: [f3a9189fb47b2491], but now for Tcl_AppendUnicodeToObj() too. | ||||
