crc32 bug on newlisp 32-bit

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?

crc32 bug on newlisp 32-bit

Postby kosh » Sun Jul 20, 2014 6:17 am

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));
 }
kosh
 
Posts: 67
Joined: Sun Sep 13, 2009 5:38 am
Location: Japan

Re: crc32 bug on newlisp 32-bit

Postby Lutz » Mon Jul 21, 2014 2:27 pm

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

http://www.newlisp.org/downloads/develo ... nprogress/
Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California


Return to newLISP and the O.S.

Who is online

Users browsing this forum: No registered users and 1 guest

cron