Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a98011c

Browse files
committed
Fix for SF bug #576327: zipfile when sizeof(long) == 8
binascii_crc32(): Make this return a signed 4-byte result across platforms. The other way to make this platform-independent would be to make it return an unsigned unbounded int, but the evidence suggests other code out there treats it like a signed 4-byte int (e.g., existing code writing the result with struct.pack "l" format). Bugfix candidate.
1 parent fbd7994 commit a98011c

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

Modules/binascii.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ binascii_crc32(PyObject *self, PyObject *args)
864864
unsigned char *bin_data;
865865
unsigned long crc = 0UL; /* initial value of CRC */
866866
int len;
867+
long result;
867868

868869
if ( !PyArg_ParseTuple(args, "s#|l:crc32", &bin_data, &len, &crc) )
869870
return NULL;
@@ -872,7 +873,16 @@ binascii_crc32(PyObject *self, PyObject *args)
872873
while(len--)
873874
crc = crc_32_tab[(crc ^ *bin_data++) & 0xffUL] ^ (crc >> 8);
874875
/* Note: (crc >> 8) MUST zero fill on left */
875-
return Py_BuildValue("l", crc ^ 0xFFFFFFFFUL);
876+
877+
result = (long)(crc ^ 0xFFFFFFFFUL);
878+
/* If long is > 32 bits, extend the sign bit. This is one way to
879+
* ensure the result is the same across platforms. The other way
880+
* would be to return an unbounded long, but the evidence suggests
881+
* that lots of code outside this treats the result as if it were
882+
* a signed 4-byte integer.
883+
*/
884+
result |= -(result & (1L << 31));
885+
return PyInt_FromLong(result);
876886
}
877887

878888

0 commit comments

Comments
 (0)