Page 1 of 1

crc32 bug on newlisp 32-bit

Posted: Sun Jul 20, 2014 6:17 am
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));
 }

Re: crc32 bug on newlisp 32-bit

Posted: Mon Jul 21, 2014 2:27 pm
by Lutz
Thanks Kosh, for this discovery and the fix. Online here:

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