From b72b8eaee1de5e58451e58021bc3e09ba9a63e46 Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 3 Sep 2025 22:39:15 +0200 Subject: [PATCH] add comments to point at where strict aliasing rule violations occur in data scripts this is definitely not all of them, a thorough rewrite must take place to fix as many as we can. --- src/dat/mcx.c | 12 ++++++------ src/dat/nbt.c | 10 +++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/dat/mcx.c b/src/dat/mcx.c index dfd05f2..d2a8955 100644 --- a/src/dat/mcx.c +++ b/src/dat/mcx.c @@ -18,8 +18,8 @@ /* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */ static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) { assert(src > dst); - u32 *table = (u32 *)buf; - size_t len = src - dst; // acquire the amount of bytes that we shall move + u32 *table = (u32 *)buf; // BUG: strict aliasing + size_t len = src - dst; // acquire the amount of bytes that we shall move assert(!(len % SECTOR)); // count how many bytes we need to move, whilst updating location data @@ -36,7 +36,7 @@ static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) { * Returns the bytes removed by this function. */ static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) { // load the table data - u32 *table = (u32 *)buf; + u32 *table = (u32 *)buf; // BUG: strict aliasing size_t slen, bidx, blen; slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk bidx = (be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at @@ -62,7 +62,7 @@ size_t mcx_delchunk(u8 *restrict buf, int chunk) { size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) { assert(start < end && end < CHUNKS); - u32 *table = (u32 *)buf; + u32 *table = (u32 *)buf; // BUG: strict aliasing u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR; u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR; src += (be32toh(table[end]) & 0xFF) * SECTOR; @@ -70,8 +70,8 @@ size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) { // zeroes-out the chunk data within this range. (and set the timestamp) u32 ts = htobe32(time(NULL)); for (int i = start; i <= end; i++) { - table[i] = 0; - table[i + CHUNKS] = ts; + table[i] = 0; // BUG: strict aliasing + table[i + CHUNKS] = ts; // BUG: strict aliasing } // move the remaining chunks down diff --git a/src/dat/nbt.c b/src/dat/nbt.c index d8de46c..a2157eb 100644 --- a/src/dat/nbt.c +++ b/src/dat/nbt.c @@ -30,6 +30,7 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt if (size == 1) return buf; size_t i = 0; while (i < len) { + // BUG: strict aliasing switch (size) { case 2: *(u16 *)((*out)->dat + i) = be16toh(*(u16 *)((*out)->dat + i)); break; case 4: *(u32 *)((*out)->dat + i) = be32toh(*(u32 *)((*out)->dat + i)); break; @@ -59,7 +60,7 @@ static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict *re } buf++; - i32 len = (i32)be32toh(*(u32 *)buf); + i32 len = (i32)be32toh(*(u32 *)buf); // BUG: strict aliasing buf += 4; return procarr(buf, len, size, out); } @@ -71,6 +72,7 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) { i32 nmem; uint size; + // BUG: strict aliasing switch (*buf) { case NBT_I8: *(u8 *)out = *ptr; return ptr + 1; case NBT_I16: *(u16 *)out = be16toh(*(u16 *)ptr); return ptr + 2; @@ -102,6 +104,7 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) { static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) { const u8 *tag = ptr; ptr++; + // BUG: strict aliasing switch (*tag) { case NBT_END: break; case NBT_I8: ptr += (i32)be32toh(*(u32 *)ptr) * 1; break; @@ -114,7 +117,7 @@ static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, // TODO: handle out of bounds... Might not be required if we use flexible array member (*dpt)++; tags[*dpt] = *tag; - lens[*dpt] = (i32)be32toh(*(u32 *)ptr); + lens[*dpt] = (i32)be32toh(*(u32 *)ptr); // BUG: strict aliasing break; } ptr += 4; @@ -136,9 +139,10 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 * *dpt -= !lens[*dpt]; } else { type = *tag; - ptr += be16toh(*(u16 *)(tag + 1)) + 3; + ptr += be16toh(*(u16 *)(tag + 1)) + 3; // BUG: strict aliasing } + // BUG: strict aliasing switch (type) { case NBT_I8: ptr += 1; break; case NBT_I16: ptr += 2; break;