liblzma: Use unaligned_readXXne functions instead of type punning.

Now gcc -fsanitize=undefined should be clean.

Thanks to Jeffrey Walton.
This commit is contained in:
Lasse Collin 2019-06-01 19:01:21 +03:00
parent 29afef0348
commit efbf6e5f09
2 changed files with 7 additions and 7 deletions

View File

@ -61,8 +61,8 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
// to __builtin_clzll(). // to __builtin_clzll().
#define LZMA_MEMCMPLEN_EXTRA 8 #define LZMA_MEMCMPLEN_EXTRA 8
while (len < limit) { while (len < limit) {
const uint64_t x = *(const uint64_t *)(buf1 + len) const uint64_t x = unaligned_read64ne(buf1 + len)
- *(const uint64_t *)(buf2 + len); - unaligned_read64ne(buf2 + len);
if (x != 0) { if (x != 0) {
# if defined(_M_X64) // MSVC or Intel C compiler on Windows # if defined(_M_X64) // MSVC or Intel C compiler on Windows
unsigned long tmp; unsigned long tmp;
@ -120,8 +120,8 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
// Generic 32-bit little endian method // Generic 32-bit little endian method
# define LZMA_MEMCMPLEN_EXTRA 4 # define LZMA_MEMCMPLEN_EXTRA 4
while (len < limit) { while (len < limit) {
uint32_t x = *(const uint32_t *)(buf1 + len) uint32_t x = unaligned_read32ne(buf1 + len)
- *(const uint32_t *)(buf2 + len); - unaligned_read32ne(buf2 + len);
if (x != 0) { if (x != 0) {
if ((x & 0xFFFF) == 0) { if ((x & 0xFFFF) == 0) {
len += 2; len += 2;
@ -143,8 +143,8 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
// Generic 32-bit big endian method // Generic 32-bit big endian method
# define LZMA_MEMCMPLEN_EXTRA 4 # define LZMA_MEMCMPLEN_EXTRA 4
while (len < limit) { while (len < limit) {
uint32_t x = *(const uint32_t *)(buf1 + len) uint32_t x = unaligned_read32ne(buf1 + len)
^ *(const uint32_t *)(buf2 + len); ^ unaligned_read32ne(buf2 + len);
if (x != 0) { if (x != 0) {
if ((x & 0xFFFF0000) == 0) { if ((x & 0xFFFF0000) == 0) {
len += 2; len += 2;

View File

@ -26,7 +26,7 @@
// reason to not use it when it is supported. // reason to not use it when it is supported.
#ifdef TUKLIB_FAST_UNALIGNED_ACCESS #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
# define not_equal_16(a, b) \ # define not_equal_16(a, b) \
(*(const uint16_t *)(a) != *(const uint16_t *)(b)) (unaligned_read16ne(a) != unaligned_read16ne(b))
#else #else
# define not_equal_16(a, b) \ # define not_equal_16(a, b) \
((a)[0] != (b)[0] || (a)[1] != (b)[1]) ((a)[0] != (b)[0] || (a)[1] != (b)[1])