Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cmumford opened this issue Sep 9, 2014 · 6 comments
Closed
Assignees

Comments

@cmumford
Copy link
Contributor

cmumford commented Sep 9, 2014

Original issue 35 created by Alexander.Klishin on 2011-08-23T06:31:19.000Z:

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));

@cmumford cmumford self-assigned this Sep 9, 2014
@cmumford
Copy link
Contributor Author

cmumford commented Sep 9, 2014

Comment #1 originally posted by gabor@google.com on 2011-09-23T22:58:32.000Z:

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

@cmumford
Copy link
Contributor Author

cmumford commented Sep 9, 2014

Comment #2 originally posted by Alexander.Klishin on 2011-09-30T11:20:01.000Z:

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.

@cmumford
Copy link
Contributor Author

cmumford commented Sep 9, 2014

Comment #3 originally posted by gabor@google.com on 2011-10-05T23:28:00.000Z:

This issue was closed by revision e028bdf.

@cmumford
Copy link
Contributor Author

cmumford commented Sep 9, 2014

Comment #4 originally posted by gabor@google.com on 2011-10-05T23:31:07.000Z:

This issue was closed by revision 299cced.

@cmumford
Copy link
Contributor Author

cmumford commented Sep 9, 2014

Comment #5 originally posted by gabor@google.com on 2011-10-05T23:35:06.000Z:

This issue was closed by revision r53.

@cmumford
Copy link
Contributor Author

cmumford commented Sep 9, 2014

Comment #6 originally posted by dgrogan@chromium.org on 2012-06-01T23:55:51.000Z:

Issue 36 has been merged into this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant