Skip to content

Commit 7159bfa

Browse files
committed
avoid some theoretical aliasing UB
in theory, uint8_t is an integer type which does not benefit from the same luxury as character types in terms of being able to freely alias other types. in practice, it's a typedef to `unsigned char` and so it works out. i've mostly been actively ignoring this part of the standard since (A) `uint8_t` often better reflects the intent, in this case we expect 8 bit octets not a fuzzy "character" type and (B) implementing such optimization would break lot of real world code and should realistically be considered malicious (not that it has stopped gcc/clang from implementing nonsense/dangerous optimizations). the README does make claim of being UB free, so i'll just update the code to match the document. Thanks to Michael Forney for reporting. Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66110
1 parent 3f6d984 commit 7159bfa

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

chibihash64-stream.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ typedef struct {
2424

2525
// hacky way to make both chibihash64.h and chibihash64-stream.h usable within same TU
2626
#if !defined(CHIBIHASH64__HGUARD)
27-
static inline uint64_t chibihash64__load32le(const uint8_t *p)
27+
static inline uint64_t chibihash64__load32le(const unsigned char *p)
2828
{
2929
return (uint64_t)p[0] << 0 | (uint64_t)p[1] << 8 |
3030
(uint64_t)p[2] << 16 | (uint64_t)p[3] << 24;
3131
}
32-
static inline uint64_t chibihash64__load64le(const uint8_t *p)
32+
static inline uint64_t chibihash64__load64le(const unsigned char *p)
3333
{
3434
return chibihash64__load32le(p) | (chibihash64__load32le(p+4) << 32);
3535
}
@@ -46,7 +46,7 @@ static inline uint64_t chibihash64__rotl(uint64_t x, int n)
4646
#endif
4747

4848
static inline void
49-
chibihash64__stream_block(uint64_t h[4], const uint8_t *p)
49+
chibihash64__stream_block(uint64_t h[4], const unsigned char *p)
5050
{
5151
const uint64_t K = UINT64_C(0x2B7E151628AED2A7);
5252
for (int i = 0; i < 4; ++i, p += 8) {
@@ -73,7 +73,7 @@ chibihash64_init(uint64_t seed)
7373
static inline void
7474
chibihash64_append(ChibiHash64Ctx *ctx, void *keyIn, ptrdiff_t len)
7575
{
76-
const uint8_t *p = (const uint8_t *)keyIn;
76+
const unsigned char *p = (const unsigned char *)keyIn;
7777
ptrdiff_t l = len;
7878

7979
ctx->total_len += len;

chibihash64.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#include <stdint.h>
1010
#include <stddef.h>
1111

12-
static inline uint64_t chibihash64__load32le(const uint8_t *p)
12+
static inline uint64_t chibihash64__load32le(const unsigned char *p)
1313
{
1414
return (uint64_t)p[0] << 0 | (uint64_t)p[1] << 8 |
1515
(uint64_t)p[2] << 16 | (uint64_t)p[3] << 24;
1616
}
17-
static inline uint64_t chibihash64__load64le(const uint8_t *p)
17+
static inline uint64_t chibihash64__load64le(const unsigned char *p)
1818
{
1919
return chibihash64__load32le(p) | (chibihash64__load32le(p+4) << 32);
2020
}
@@ -26,7 +26,7 @@ static inline uint64_t chibihash64__rotl(uint64_t x, int n)
2626
static inline uint64_t
2727
chibihash64(const void *keyIn, ptrdiff_t len, uint64_t seed)
2828
{
29-
const uint8_t *p = (const uint8_t *)keyIn;
29+
const unsigned char *p = (const unsigned char *)keyIn;
3030
ptrdiff_t l = len;
3131

3232
const uint64_t K = UINT64_C(0x2B7E151628AED2A7); // digits of e

0 commit comments

Comments
 (0)