mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-17 09:35:46 +01:00
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.
This commit is contained in:
@@ -18,8 +18,8 @@
|
|||||||
/* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */
|
/* 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) {
|
static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) {
|
||||||
assert(src > dst);
|
assert(src > dst);
|
||||||
u32 *table = (u32 *)buf;
|
u32 *table = (u32 *)buf; // BUG: strict aliasing
|
||||||
size_t len = src - dst; // acquire the amount of bytes that we shall move
|
size_t len = src - dst; // acquire the amount of bytes that we shall move
|
||||||
assert(!(len % SECTOR));
|
assert(!(len % SECTOR));
|
||||||
|
|
||||||
// count how many bytes we need to move, whilst updating location data
|
// 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. */
|
* Returns the bytes removed by this function. */
|
||||||
static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
|
static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
|
||||||
// load the table data
|
// load the table data
|
||||||
u32 *table = (u32 *)buf;
|
u32 *table = (u32 *)buf; // BUG: strict aliasing
|
||||||
size_t slen, bidx, blen;
|
size_t slen, bidx, blen;
|
||||||
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
|
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
|
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) {
|
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
|
||||||
assert(start < end && end < CHUNKS);
|
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 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
|
||||||
u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
|
u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
|
||||||
src += (be32toh(table[end]) & 0xFF) * 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)
|
// zeroes-out the chunk data within this range. (and set the timestamp)
|
||||||
u32 ts = htobe32(time(NULL));
|
u32 ts = htobe32(time(NULL));
|
||||||
for (int i = start; i <= end; i++) {
|
for (int i = start; i <= end; i++) {
|
||||||
table[i] = 0;
|
table[i] = 0; // BUG: strict aliasing
|
||||||
table[i + CHUNKS] = ts;
|
table[i + CHUNKS] = ts; // BUG: strict aliasing
|
||||||
}
|
}
|
||||||
|
|
||||||
// move the remaining chunks down
|
// move the remaining chunks down
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt
|
|||||||
if (size == 1) return buf;
|
if (size == 1) return buf;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while (i < len) {
|
while (i < len) {
|
||||||
|
// BUG: strict aliasing
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case 2: *(u16 *)((*out)->dat + i) = be16toh(*(u16 *)((*out)->dat + i)); break;
|
case 2: *(u16 *)((*out)->dat + i) = be16toh(*(u16 *)((*out)->dat + i)); break;
|
||||||
case 4: *(u32 *)((*out)->dat + i) = be32toh(*(u32 *)((*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++;
|
buf++;
|
||||||
i32 len = (i32)be32toh(*(u32 *)buf);
|
i32 len = (i32)be32toh(*(u32 *)buf); // BUG: strict aliasing
|
||||||
buf += 4;
|
buf += 4;
|
||||||
return procarr(buf, len, size, out);
|
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;
|
i32 nmem;
|
||||||
uint size;
|
uint size;
|
||||||
|
|
||||||
|
// BUG: strict aliasing
|
||||||
switch (*buf) {
|
switch (*buf) {
|
||||||
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
|
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
|
||||||
case NBT_I16: *(u16 *)out = be16toh(*(u16 *)ptr); return ptr + 2;
|
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) {
|
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;
|
const u8 *tag = ptr;
|
||||||
ptr++;
|
ptr++;
|
||||||
|
// BUG: strict aliasing
|
||||||
switch (*tag) {
|
switch (*tag) {
|
||||||
case NBT_END: break;
|
case NBT_END: break;
|
||||||
case NBT_I8: ptr += (i32)be32toh(*(u32 *)ptr) * 1; 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
|
// TODO: handle out of bounds... Might not be required if we use flexible array member
|
||||||
(*dpt)++;
|
(*dpt)++;
|
||||||
tags[*dpt] = *tag;
|
tags[*dpt] = *tag;
|
||||||
lens[*dpt] = (i32)be32toh(*(u32 *)ptr);
|
lens[*dpt] = (i32)be32toh(*(u32 *)ptr); // BUG: strict aliasing
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ptr += 4;
|
ptr += 4;
|
||||||
@@ -136,9 +139,10 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *
|
|||||||
*dpt -= !lens[*dpt];
|
*dpt -= !lens[*dpt];
|
||||||
} else {
|
} else {
|
||||||
type = *tag;
|
type = *tag;
|
||||||
ptr += be16toh(*(u16 *)(tag + 1)) + 3;
|
ptr += be16toh(*(u16 *)(tag + 1)) + 3; // BUG: strict aliasing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BUG: strict aliasing
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case NBT_I8: ptr += 1; break;
|
case NBT_I8: ptr += 1; break;
|
||||||
case NBT_I16: ptr += 2; break;
|
case NBT_I16: ptr += 2; break;
|
||||||
|
|||||||
Reference in New Issue
Block a user