Tcl Source Code

View Ticket
Login
Ticket UUID: 218947
Title: string compare with result from string index does not work p
Type: Bug Created on: 2000-10-26 01:08:29
Submitter: nobody Assigned to: nobody
Subsystem: 12. ByteArray Object Severity:
Priority: 2 Last modified: 2001-04-19 03:33:36
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2001-04-18 20:33:36
Version: obsolete: 8.2
Description:
OriginalBugID: 2871 Bug
Version: 8.2
SubmitDate: '1999-09-23'
LastModified: '1999-10-20'
Severity: SER
Status: Released
Submitter: techsupp
ChangedBy: hobbs
OS: Linux-SuSE
OSVersion: 6.0
Machine: Other
FixedDate: '1999-10-20'
FixedInVersion: 8.2.2
ClosedDate: '2000-10-25'


Name:
Hans Banken

ReproducibleScript:
#!/usr/bin/tclsh
    set str "0123456789\x00\ abcdedfghi"
    puts "str: $str"
    puts "before scan - compare index 10 with \x00: [string compare [string index $str 10] \x00]"
    binary scan $str H* dump
    puts "access with string index: $dump\ncompare index 10 with \x00:
    [string compare [string index $str 10] \x00]"
    puts "access with string range: $dump\ncompare range 10,10 with \x00:
    [string compare [string range $str 10 10] \x00]"

    # Executing this script results in:
    #   str: 0123456789 abcdedfghi
    #   before scan - compare index 10 with : 0
    #   access with string index:
    30313233343536373839002061626364656466676869
    #   compare index 10 with : -1
    #   access with string range:
    30313233343536373839002061626364656466676869
    #   compare range 10,10 with : 0

ObservedBehavior:
You might get wrong results when comparing the result of a string index
    operation



This is tricky.  What is happening is that the user is shimmering
the object between a string and a bytearray.  'string' has an
overloaded use, where it will access a bytearray by bytes, and
strings as strings.  In the first case, you get the unicode
version of \x00.  In the second, you get the true one-byte \x00
(just a null byte), which is not the same as the unicode one
(which is two bytes).  In the third case, STR_RANGE creates
a bytearray obj, which will get converted OK.  The problem appears
to be that STR_INDEX for bytearrays wants to return a string type obj. 
-- 09/24/1999 hobbs
Fixed 10/20/1999 (for 8.3b1) by making STR_INDEX for ByteArrays
return a ByteArray object. 
-- 10/20/1999 hobbs