Skip to content

Commit

Permalink
avoid undefined behavior
Browse files Browse the repository at this point in the history
unaligned reads are UB, use (__builtin_) memcpy

Signed-off-by: Kimball Thurston <kdt3rd@gmail.com>
  • Loading branch information
kdt3rd committed Mar 2, 2024
1 parent 15d4494 commit 9df25db
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/lib/OpenEXRCore/internal_huf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1007,13 +1007,23 @@ readUInt (const uint8_t* b)

#ifdef __APPLE__
# include <libkern/OSByteOrder.h>
# define READ64(c) OSSwapInt64 (*(const uint64_t*) (c))
# define SWAP64(c) OSSwapInt64 (c)
#elif defined(linux)
# include <byteswap.h>
# define READ64(c) bswap_64 (*(const uint64_t*) (c))
# define SWAP64(c) bswap_64 (c)
#elif defined(_MSC_VER)
# include <stdlib.h>
# define READ64(c) _byteswap_uint64 (*(const uint64_t*) (c))
# define SWAP64(c) _byteswap_uint64 (c)
#endif

#ifdef SWAP64
static inline uint64_t READ64(const uint8_t *src)
{
uint64_t v;
// unaligned reads are UB
memcpy (&v, src, sizeof(uint64_t));
return SWAP64 (v);
}
#else
# define READ64(c) \
((uint64_t) (c)[0] << 56) | ((uint64_t) (c)[1] << 48) | \
Expand Down

0 comments on commit 9df25db

Please sign in to comment.