Tcl Library Source Code

View Ticket
Login
2026-11-15
13:36 • Ticket [2848168fff] signmask incorrect in ::crc::Crc and ::crc::Crc32_tcl status still Open with 3 other changes artifact: 0fca70bc56 user: serhiy.storchaka
13:24
Fix [2848168fff]: ::crc::Crc gave wrong results for non-reflected 32-bit CRCs where tcl_platform(wordSize) is 4 (Windows): the signbit mask cleared bit 24. Values are always masked non-negative, so the mask is not needed. leaf check-in: 69f713b9da user: serhiy.storchaka tags: crc-32bit-wordsize
2009-12-30
08:30 • Ticket [2848168fff] signmask incorrect in ::crc::Crc and ::crc::Crc32_tcl status still Open with 4 other changes artifact: 25cf28f206 user: kchansen
2009-09-02
02:56 • Ticket [2848168fff]: 1 change artifact: 420c66a362 user: kchansen
02:56 • Ticket [2848168fff]: 4 changes artifact: b66e8ab7a5 user: kchansen
2009-09-01
03:19 • New ticket [2848168fff]. artifact: d530516056 user: kchansen

Ticket UUID: 2848168
Title: signmask incorrect in ::crc::Crc and ::crc::Crc32_tcl
Type: Bug Version: None
Submitter: kchansen Created on: 2009-09-01 03:19:10
Subsystem: crc Assigned To: patthoyts
Priority: 9 Immediate Severity:
Status: Open Last Modified: 2026-11-15 13:36:40
Resolution: None Closed By:
    Closed on:
Description:
Both crc16.tcl and crc32.tcl use a variable named "signmask", that is constructed from another variable named "signbit".

For 32-bit data, signbit is 0x8000000.

For 32-bit CRCs, the signmask appears to be designed to mask off the high-byte, i.e. it should end up 0x00ffffff.  Signmask is computed as follows:

set signmask [expr {~$signbit>>7}]

Unfortunately when you paste this code into Tcl, (V8.4.14, V8.5.*, V8.6.*) you don't get 0x00FFFFFF, you get 0xFEFFFFFF.

Now, this is *probably* a bug in EXPR or in the >> operator, but the code does not work the way it is currently written....
User Comments: serhiy.storchaka added on 2026-11-15 13:36:40:

Confirmed on Windows with Tcl 9.1 and 8.6, where tcl_platform(wordSize) is 4: CRC-32/BZIP2 of "123456789" gives F9E2E667 instead of FC891918. The values are always masked non-negative, so the sign mask is not needed. Proposed fix in branch crc-32bit-wordsize, which removes it; test crc16-10.0.


kchansen added on 2009-12-30 08:30:40:
Believe this can be fixed by simply changing the initial value for "v" when originally computing signmask from "1" to "-1":

Change from:
        for {set v 1} {int($v) != 0} {set signbit $v; set v [expr {$v<<1}]} {}

To:
        for {set v -1} {int($v) != 0} {set signbit $v; set v [expr {$v<<1}]} {}

This guarantees that signmask will always be negative and that "~signmask" will always be a non-negative low-order bitmask.

kchansen added on 2009-09-02 02:56:44:
Posted this as potential bug to TCL sourceforge:

ID:
#2848176
Link:
https://sourceforge.net/tracker/?func=detail&aid=2848176&group_id=10894&atid=110894

Response is that since V8.2 this is designed behavior.

CRC Tcl implementations need to be rewritten to behave correctly with new wide support.

If no one has the time let me know and I'll submit a proposed fix.

I elevated the priority to 9 because the current code generates incorrect 32-bit results with the Tcl implementations.