Tcl Source Code

View Ticket
Login
Ticket UUID: 218287
Title: binary scan problem with 16 bit integers > 0x8000
Type: Bug Created on: 2000-10-26 00:55:36
Submitter: nobody Assigned to: nobody
Subsystem: 12. ByteArray Object Severity:
Priority: 2 Last modified: 2001-04-05 07:21:27
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2001-04-05 00:21:27
Version: obsolete: 8.0.4
Description:
OriginalBugID: 3466 Bug
Version: 8.0.4
SubmitDate: '1999-11-05'
LastModified: '2000-04-04'
Severity: MED
Status: Closed
Submitter: techsupp
ChangedBy: hobbs
RelatedBugIDs: 3229
OS: Linux
FixedDate: '2000-10-25'
ClosedDate: '2000-04-04'


Name:
Bob Findlay

ReproducibleScript:
#!/usr/bin/tclsh
    set bob1 0x8000
    set bob2 [binary format "s1" $bob1]
    binary scan $bob2 s1 bob3
    puts stdout [format "bob1=%X bob3=%X" $bob1 $bob3]

ObservedBehavior:
It seems once the lead bit in a 16 bit integer is set the binary
    scan command seems to prepend the result with 0xffff
    
    This does not seem like the correct behavior.

DesiredBehavior:
I would have expected that binary scan would return with the 16 bit
    value 
    without the prepended 0xffff



The output does come out as (8.3.0):

    bob1=8000 bob3=FFFF8000

which doesn't seem correct.  A work-around (possibly only for
this known case) is:

    format "bob1=%X bob3=%X" $bob1 [expr {$bob3 & ~0xffff0000}]

needs further investigation. 
-- 04/03/2000 hobbs

This turns out to be the correct behavior, based on what the
user is requesting.  For shorts (s or S), numbers >= 0x8000 are
negative (we don't have representation for unsigned shorts -
maybe we should, the same is true for chars and also ints on
systems where ints are > 32 bits).  So, the data is stored into
the ByteArray taking up only 16 bits.  However, when it is
extracted, it is placed into a long value, and thus the sign
extension must be done.  So the short value 0x8000 is
equivalent to the long value 0xFFFF8000 (or 0xFFFFFFFFFFFF8000
on 64bit longs).  Thus, the correct way to handle this, if the
user wanted to work with the shorts being unsigned, is to mask
each return value with 0xFFFF ($bob3 & 0xFFFF).  The above
work-around also works, but only on 32bit systems, so use the
latter instead.

You can see the code and logic for this in tclBinary.c:ScanNumber.
We will put a note in the binary doc for 8.3.1 as this is a bit
tricky (but definitely correct). 
-- 04/04/2000 hobbs

The following has been added to the binary.n man page (scan section):

It is important to note that the c, s, and S (and i and I on
64bit systems) will be scanned into long data  size  values.
In doing this, values that have their high bit set (0x80 for
chars, 0x8000 for shorts, 0x80000000 for ints), will be sign
extended.  Thus the following will occur:
set signShort [binary format s1 0x8000]
binary scan $signShort s1 val;  # val == 0xFFFF8000
If  you  want  to  work-around the sign extension of values,
mask the return value to the correct size, for example  with
shorts:
set val [expr {$val & 0xFFFF}];  # val == 0x8000
 
-- 04/04/2000 hobbs