Skip to content
Tcl Source Code History Of Ticket 1410553fffffffff
Login

History Of Ticket 1410553fffffffff

Artifacts Associated With Ticket 1410553fffffffff

  1. Ticket change [a510d35ac3] (rid 110310) by twylite on 2006-01-20 08:13:58:

    1. assignee initialized to: "msofer"
    2. cmimetype initialized to: "text/plain"
    3. 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
      
      ---
      
      
    4. foundin initialized to: "obsolete: 8.4.12"
    5. priority initialized to: "5 Medium"
    6. resolution initialized to: "None"
    7. status initialized to: "Open"
    8. submitter initialized to: "twylite"
    9. subsystem initialized to: "10. Objects"
    10. title initialized to:
      String numchars and object length can be desynchronised
      
    11. type initialized to: "Bug"
  2. Ticket change [c5bb5d55f8] (rid 112050) by dkf on 2006-01-20 16:32:28:

    1. priority changed to: "8"
  3. Ticket change [fb54e3e1af] (rid 92043) by pspjuth on 2006-01-20 20:58:11:

    1. 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) {
      
      
      
      
    2. login: "pspjuth"
    3. mimetype: "text/plain"
    4. username: "pspjuth"
  4. Ticket change [a60efddc45] (rid 98534) by twylite on 2006-01-20 21:21:32:

    1. 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.
      
      
      
    2. login: "twylite"
    3. mimetype: "text/plain"
    4. username: "twylite"
  5. Ticket change [8c15b49de7] (rid 127690) by msofer on 2006-01-23 18:25:46:

    1. closedate changed to: "2006-01-23T11:25:46"
    2. closer changed to: "msofer"
    3. 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.
      
      
    4. login: "msofer"
    5. mimetype: "text/plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Pending"
    8. username: "msofer"
  6. Ticket change [4f1eb6126f] (rid 125771) by dkf on 2006-01-23 18:44:36:

    1. icomment:
      Logged In: YES 
      user_id=79902
      
      Test added to HEAD
      
      
    2. login: "dkf"
    3. mimetype: "text/plain"
    4. resolution changed to: "None"
    5. status changed to: "Open"
    6. username: "dkf"
  7. Ticket change [66bbdc3b11] (rid 102770) by msofer on 2006-01-23 19:17:17:

    1. closedate changed to: "2006-01-23T12:17:17"
    2. closer changed to: "msofer"
    3. icomment:
      Logged In: YES 
      user_id=148712
      
      test added to core-8-4-branch
      
      
    4. login: "msofer"
    5. mimetype: "text/plain"
    6. status changed to: "Closed"
    7. username: "msofer"
  8. Ticket change [2f407abd11] (rid 192832) by jan.nijtmans on 2021-03-29 10:02:57:

    1. closedate changed to: "2459302.91872601"
    2. closer changed to: "jan.nijtmans"
    3. 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
      
      ---
      
    4. login: "jan.nijtmans"
    5. mimetype: "text/plain"
    6. resolution changed to: "Fixed"
    7. severity changed to: "Minor"