NO

Author Topic: Possible bug with integer?  (Read 1339 times)

Offline bitcoin

  • Member
  • *
  • Posts: 179
Possible bug with integer?
« on: July 22, 2019, 10:54:12 PM »
I have algo to compute CRC32. It similar to ntdll.RtlComputeCrc32 , but in Pelles it return another number. In VS all ok. Why? This is bug or feature?

So, code is

Code: [Select]
unsigned int crc32(unsigned int seed, void *msg, size_t len)
{
unsigned int crc = ~seed;

while (len--) {
crc ^= *((unsigned char *)msg)++;

for (size_t i = 0; i < 8; ++i)
crc = (crc >> 1) ^ (0xEDB88320 & ~(crc & 1) + 1);
}

return ~crc;
}

VS return 1267612143, RtlComputeCrc32 return 1267612143 , but Pelles return 3818104785

Code: [Select]
tmp = crc32(0, "abcdef", 6);

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Possible bug with integer?
« Reply #1 on: July 23, 2019, 08:27:28 AM »
a fixed version ?
Code: [Select]
unsigned int crc32(unsigned int seed, void *msg, size_t len)
{
unsigned int crc = ~seed;
unsigned char *p = msg;

while (len--) {
//crc ^= *((unsigned char *)msg)++;
crc ^= *(p++);

for (size_t i = 0; i < 8; ++i)
crc = (crc >> 1) ^ (0xEDB88320 & (~(crc & 1) + 1));
}

return ~crc;
}
clang:
Code: [Select]
test_crc.c(9,4): error: assignment to cast is illegal, lvalue casts are not supported
                ((unsigned char *)msg)++;
                ~^~~~~~~~~~~~~~~~~~~~~~~
« Last Edit: July 23, 2019, 08:42:53 AM by TimoVJL »
May the source be with you

Offline bitcoin

  • Member
  • *
  • Posts: 179
Re: Possible bug with integer?
« Reply #2 on: July 23, 2019, 02:27:56 PM »
Yes, it works! I don't know C well.