Artifacts Associated With Ticket 1410553fffffffffffffffffffffffffffffffff
Ticket change [a510d35ac3] (rid 110310) by twylite on 2006-01-20 08:13:58:
- assignee initialized to: "msofer"
- cmimetype initialized to: "text/plain"
- comment initialized to:
Under certain circumstances the Tcl_GetRange() function will incorrectly treat the internal representation as a String (of byte-wide characters) rather than Unicode, resulting in incorrect behaviour. The check is based on a comparison between strPtr->numChars and objPtr->length. The functions "append" and "string length" may be involved in causing a desynchronisation between numChars and length. Bug present in 8.4.11, 8.4.12, 8.5a4 (from CVS 2006/01/19 ) on Win32. Bug reproduction code with actual and expected output is below. --- This code reproduces a bug that we found in a serial receive loop. The loop would read characters from a serial channel and append them one by one to a receive buffer; if an trigger character (ETX = \x03) was found it would witch from data mode to CRC mode. It appears that doing the "string length" when a NUL character (\x00) has a side-effect whereby a subsequent "string range" misidentifies the end of the string. Notes: - It seems that the trigger character can be any non-NUL value. - The indexes to "string range" can be relative to end, or absolute. Both fail. - Not doing the "string length" negates the problem - Doing a "string bytelength" before the "string range" negates the problem - Only the first "string range" is affected; calling the same command again immediately after the first negates the problem - The problem is only reproducable when there is a \x00 in the string before the character that triggers the "string length" - If the \x00 immediately precedes the trigger character, the "string range" brings it out as \x80 instead of \x00. I strongly suspect that this has to do with the internal (unicode) encoding of a NUL. Further investigation: Stepping through the code in a debugger, I found that under the conditions given above, Tcl_GetRange() executes on a different path. if (stringPtr->numChars == objPtr->length) { // Use Tcl_GetString and Tcl_NewStringObj } else { // Use Tcl_NewUnicodeObj } It would appear that this is actually a Unicode object, as the behaviour is correct when the Unicode path is followed. The ->numChars and ->length happen (coincidently) to match on account of the encoding of \x00 and the number of additional characters added after the trigger character. My guess is along the lines of "string length" updating one of these values; whereas "append" updates the other. --- CODE TO REPRODUCE --- proc test_stringbug {} { # set bytes "\x02 \x49 \x4B \x04 \x00 \x00 \x03 \x15 \x57" # set bytes "\x00 \x01 \x57 \x03 \x41" set bytes "\x00 \x03 \x41" set rxBuffer {} foreach ch $bytes { append rxBuffer $ch #1 Take this if{} out and it works if { $ch eq "\x03" } { string length $rxBuffer } } #2 Add this "string bytelength" in and it works # string bytelength $rxBuffer set rxCRC [string range $rxBuffer end-1 end] binary scan [join $bytes {}] "H*" input_hex puts "Input is ...... $input_hex" binary scan $rxBuffer "H*" rxBuffer_hex puts "rxBuffer is ... $rxBuffer_hex" binary scan $rxCRC "H*" rxCRC_hex puts "rxCRC is ...... $rxCRC_hex" } catch { console show ; update ; wm wi . } puts "Tcl version: $tcl_patchLevel" test_stringbug --- OUTPUT --- rxCRC should be the last two bytes (4 hex characters) of rxBuffer D:\>tclkit-sh-8411.exe d:\tcl_string_bug.tcl Tcl version: 8.4.11 Input is ...... 000341 rxBuffer is ... 000341 rxCRC is ...... 8003 D:\>tclsh84-12.exe d:\tcl_string_bug.tcl Tcl version: 8.4.12 Input is ...... 000341 rxBuffer is ... 000341 rxCRC is ...... 8003 D:\>tclsh85sg.exe d:\tcl_string_bug.tcl Tcl version: 8.5a4 Input is ...... 000341 rxBuffer is ... 000341 rxCRC is ...... 8003 --- - foundin initialized to: "obsolete: 8.4.12"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "twylite"
- subsystem initialized to: "10. Objects"
- title initialized to:
String numchars and object length can be desynchronised
- type initialized to: "Bug"
Ticket change [c5bb5d55f8] (rid 112050) by dkf on 2006-01-20 16:32:28:
- priority changed to: "8"
Ticket change [fb54e3e1af] (rid 92043) by pspjuth on 2006-01-20 20:58:11:
- icomment:
Logged In: YES user_id=98900 The fault is in Tcl_GetRange. objPtr->length is not valid unless there is a string rep and Tcl_GetRange do not check if objPtr->bytes != NULL first. It would probably work with: if (objPtr->bytes != NULL && stringPtr->numChars == objPtr->length) { - login: "pspjuth"
- mimetype: "text/plain"
- username: "pspjuth"
- icomment:
Ticket change [a60efddc45] (rid 98534) by twylite on 2006-01-20 21:21:32:
- icomment:
Logged In: YES user_id=91629 Thanks :) I didn't really understand the problem because I'm not familiar with Tcl internals. I have tried the solution you suggest against Tcl 8.5a4 (from CVS 2006/01/19), and it corrects the problem.
- login: "twylite"
- mimetype: "text/plain"
- username: "twylite"
- icomment:
Ticket change [8c15b49de7] (rid 127690) by msofer on 2006-01-23 18:25:46:
- closedate changed to: "2006-01-23T11:25:46"
- closer changed to: "msofer"
- icomment:
Logged In: YES user_id=148712 Thanks to both you guys; fixed in HEAD and cor-8-4-branch. Leaving as "Pending" to remind me to add a test.
- login: "msofer"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Pending"
- username: "msofer"
Ticket change [4f1eb6126f] (rid 125771) by dkf on 2006-01-23 18:44:36:
- icomment:
Logged In: YES user_id=79902 Test added to HEAD
- login: "dkf"
- mimetype: "text/plain"
- resolution changed to: "None"
- status changed to: "Open"
- username: "dkf"
- icomment:
Ticket change [66bbdc3b11] (rid 102770) by msofer on 2006-01-23 19:17:17:
- closedate changed to: "2006-01-23T12:17:17"
- closer changed to: "msofer"
- icomment:
Logged In: YES user_id=148712 test added to core-8-4-branch
- login: "msofer"
- mimetype: "text/plain"
- status changed to: "Closed"
- username: "msofer"
Ticket change [2f407abd11] (rid 192832) by jan.nijtmans on 2021-03-29 10:02:57:
- closedate changed to: "2459302.91872601"
- closer changed to: "jan.nijtmans"
- comment changed to:
Under certain circumstances the Tcl_GetRange() function will incorrectly treat the internal representation as a String (of byte-wide characters) rather than Unicode, resulting in incorrect behaviour. The check is based on a comparison between strPtr->numChars and objPtr->length. The functions "append" and "string length" may be involved in causing a desynchronisation between numChars and length. Bug present in 8.4.11, 8.4.12, 8.5a4 (from CVS 2006/01/19 ) on Win32. Bug reproduction code with actual and expected output is below. --- This code reproduces a bug that we found in a serial receive loop. The loop would read characters from a serial channel and append them one by one to a receive buffer; if an trigger character (ETX = \x03) was found it would witch from data mode to CRC mode. It appears that doing the "string length" when a NUL character (\x00) has a side-effect whereby a subsequent "string range" misidentifies the end of the string. Notes: - It seems that the trigger character can be any non-NUL value. - The indexes to "string range" can be relative to end, or absolute. Both fail. - Not doing the "string length" negates the problem - Doing a "string bytelength" before the "string range" negates the problem - Only the first "string range" is affected; calling the same command again immediately after the first negates the problem - The problem is only reproducable when there is a \x00 in the string before the character that triggers the "string length" - If the \x00 immediately precedes the trigger character, the "string range" brings it out as \x80 instead of \x00. I strongly suspect that this has to do with the internal (unicode) encoding of a NUL. Further investigation: Stepping through the code in a debugger, I found that under the conditions given above, Tcl_GetRange() executes on a different path. if (stringPtr->numChars == objPtr->length) { // Use Tcl_GetString and Tcl_NewStringObj } else { // Use Tcl_NewUnicodeObj } It would appear that this is actually a Unicode object, as the behaviour is correct when the Unicode path is followed. The ->numChars and ->length happen (coincidently) to match on account of the encoding of \x00 and the number of additional characters added after the trigger character. My guess is along the lines of "string length" updating one of these values; whereas "append" updates the other. --- CODE TO REPRODUCE --- proc test_stringbug {} { # set bytes "\x02 \x49 \x4B \x04 \x00 \x00 \x03 \x15 \x57" # set bytes "\x00 \x01 \x57 \x03 \x41" set bytes "\x00 \x03 \x41" set rxBuffer {} foreach ch $bytes { append rxBuffer $ch #1 Take this if{} out and it works if { $ch eq "\x03" } { string length $rxBuffer } } #2 Add this "string bytelength" in and it works # string bytelength $rxBuffer set rxCRC [string range $rxBuffer end-1 end] binary scan [join $bytes {}] "H*" input_hex puts "Input is ...... $input_hex" binary scan $rxBuffer "H*" rxBuffer_hex puts "rxBuffer is ... $rxBuffer_hex" binary scan $rxCRC "H*" rxCRC_hex puts "rxCRC is ...... $rxCRC_hex" } catch { console show ; update ; wm wi . } puts "Tcl version: $tcl_patchLevel" test_stringbug --- OUTPUT --- rxCRC should be the last two bytes (4 hex characters) of rxBuffer D:\>tclkit-sh-8411.exe d:\tcl_string_bug.tcl Tcl version: 8.4.11 Input is ...... 000341 rxBuffer is ... 000341 rxCRC is ...... 8003 D:\>tclsh84-12.exe d:\tcl_string_bug.tcl Tcl version: 8.4.12 Input is ...... 000341 rxBuffer is ... 000341 rxCRC is ...... 8003 D:\>tclsh85sg.exe d:\tcl_string_bug.tcl Tcl version: 8.5a4 Input is ...... 000341 rxBuffer is ... 000341 rxCRC is ...... 8003 --- - login: "jan.nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- severity changed to: "Minor"