crc32 bug on newlisp 32-bit

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
kosh
Posts: 72
Joined: Sun Sep 13, 2009 5:38 am
Location: Japan
Contact:

crc32 bug on newlisp 32-bit

Post by kosh »

crc32 function may return wrong value on 32-bit machine.

Code: Select all

> (crc32 "hello")
907060870        ; OK
> (crc32 "HELLO")
-1052482506      ; NG [0xffffffffc1446436]
> (& (crc32 "HELLO") 0x00000000ffffffff)
3242484790       ; OK [0x00000000c144643]
This problem occurs because of difference between internal value and CELL value.

- upadte_crc function (in nl-math.c) returns unsigned 32-bit integer.
- But newlisp's printCell function treats as signed integer.

This solves the crc32 values are always treated as an 64-bit integer.

Code: Select all

--- nl-math.c~	2014-06-04 11:44:59 +0900
+++ nl-math.c	2014-06-20 22:31:07 +0900
@@ -2354,9 +2354,12 @@
 {
 char * data;
 size_t len;
+unsigned int crc;
 
 params = getStringSize(params, &data, &len, TRUE);
-return(stuffInteger(update_crc(0xffffffffL, (unsigned char *)data, (int)len) ^ 0xffffffffL));
+crc = update_crc(0xffffffffL, (unsigned char *)data, (int)len) ^ 0xffffffffL;
+return(stuffInteger64(crc));
 }

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: crc32 bug on newlisp 32-bit

Post by Lutz »

Thanks Kosh, for this discovery and the fix. Online here:

http://www.newlisp.org/downloads/develo ... nprogress/

Locked