liuis / leveldb

Automatically exported from code.google.com/p/leveldb
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

static_cast char to unsigned on BIG_ENDIAN platforms may cause errors #35

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
util/coding_test may fail on some BIG_ENDIAN platforms

uname -ms
HP-UX ia64

gcc version 4.3.3 (GCC)

micro test:
====cast.cpp ====
#include <stdio.h>
int main(int ac, char *av[]) {
  char c = -1;
  printf("cast1=%u cast2=%u",
         static_cast<unsigned int>(c), //!!! wrong conversion
         static_cast<unsigned int>(static_cast<unsigned char>(c)));
}
===================
#g++ cast.cpp -o cast
#./cast
cast1=4294967295 cast2=255

===================
Problem in  file "util\coding.h"
inline uint32_t DecodeFixed32(const char* ptr) {
...
    return ((static_cast<uint32_t>(ptr[0]))
            | (static_cast<uint32_t>(ptr[1]) << 8)
            | (static_cast<uint32_t>(ptr[2]) << 16)
            | (static_cast<uint32_t>(ptr[3]) << 24));
}

Should be:
    return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0])))
            | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8)
            | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16)
            | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));

Original issue reported on code.google.com by Alexande...@gmail.com on 23 Aug 2011 at 6:31

GoogleCodeExporter commented 9 years ago
Thanks for this! Could you please try out the patch here:
http://code.google.com/p/leveldb/issues/detail?id=36

And see if that fixes your problem?

Let me know - thanks, Gabor

Original comment by ga...@google.com on 23 Sep 2011 at 10:58

GoogleCodeExporter commented 9 years ago
This patch fixes problem.
Now test "crc32c_test" works without errors.

My platform is HP-UX 11.31 ia64
Also tested on Win32/Win64/Linux i686.

Original comment by Alexande...@gmail.com on 30 Sep 2011 at 11:20

GoogleCodeExporter commented 9 years ago
This issue was closed by revision e028bdfa8137.

Original comment by ga...@google.com on 5 Oct 2011 at 11:28

GoogleCodeExporter commented 9 years ago
This issue was closed by revision 299ccedfeca1.

Original comment by ga...@google.com on 5 Oct 2011 at 11:31

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r53.

Original comment by ga...@google.com on 5 Oct 2011 at 11:35

GoogleCodeExporter commented 9 years ago
Issue 36 has been merged into this issue.

Original comment by dgrogan@chromium.org on 1 Jun 2012 at 11:55