mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-17 07:55:45 +01:00
fix: violating strict aliasing rules in most areas in the new code.
Yes, I am aware there are plenty of violations in `conf.c`, but I'll likely fix/rewrite those when I will use it. Since there are some other changes I think I'll want to make.
This commit is contained in:
@@ -11,15 +11,14 @@
|
||||
|
||||
#include "../util/intdef.h"
|
||||
|
||||
#define TABLE 0x2000 // table byte size
|
||||
#define SECTOR 0x1000 // sector size
|
||||
#define TABLE 0x800 // table (total) element count
|
||||
#define CHUNKS 0x400 // amount of chunks in a file
|
||||
|
||||
/* 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(u32 *restrict table, u8 *src, u8 *dst, int src_s, int src_e) {
|
||||
assert(src > dst);
|
||||
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));
|
||||
|
||||
// count how many bytes we need to move, whilst updating location data
|
||||
@@ -34,9 +33,8 @@ static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) {
|
||||
/* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`.
|
||||
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
|
||||
* 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, u32 *restrict table, size_t rmb, int sidx, int eidx) {
|
||||
// load the table data
|
||||
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
|
||||
@@ -49,20 +47,25 @@ static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
|
||||
// move the succeeding chunks over the deleted chunk
|
||||
u8 *dst = buf + bidx - rmb;
|
||||
u8 *src = buf + bidx + blen;
|
||||
mvchunks(buf, src, dst, sidx, eidx - 1);
|
||||
mvchunks(table, src, dst, sidx, eidx - 1);
|
||||
return blen;
|
||||
}
|
||||
|
||||
/* Just call `delchunk` with the parameters and some defaults.
|
||||
/* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well.
|
||||
* This is done instead of `delchunk` being globally linked, because
|
||||
* `delchunk` requests more specific parameters, which is confusing outside this module. */
|
||||
size_t mcx_delchunk(u8 *restrict buf, int chunk) {
|
||||
return delchunk(buf, 0, chunk, CHUNKS);
|
||||
u32 table[TABLE];
|
||||
memcpy(table, buf, sizeof(table));
|
||||
size_t res = delchunk(buf, table, 0, chunk, CHUNKS);
|
||||
memcpy(buf, table, sizeof(table));
|
||||
return res;
|
||||
}
|
||||
|
||||
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
|
||||
assert(start < end && end < CHUNKS);
|
||||
u32 *table = (u32 *)buf; // BUG: strict aliasing
|
||||
u32 table[TABLE];
|
||||
memcpy(table, buf, sizeof(table));
|
||||
u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
|
||||
u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
|
||||
src += (be32toh(table[end]) & 0xFF) * SECTOR;
|
||||
@@ -70,13 +73,14 @@ 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; // BUG: strict aliasing
|
||||
table[i + CHUNKS] = ts; // BUG: strict aliasing
|
||||
table[i] = 0;
|
||||
table[i + CHUNKS] = ts;
|
||||
}
|
||||
|
||||
// move the remaining chunks down
|
||||
if (end < (CHUNKS - 1))
|
||||
mvchunks(buf, src, dst, end, (CHUNKS - 1));
|
||||
mvchunks(table, src, dst, end, (CHUNKS - 1));
|
||||
memcpy(buf, table, sizeof(table));
|
||||
return src - dst;
|
||||
}
|
||||
|
||||
@@ -96,9 +100,14 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk
|
||||
qsort(chunkids, chunkc, sizeof(int), cmp_chunkids);
|
||||
chunkids[chunkc] = CHUNKS; // set the spare chunk to the max chunks, so the rest of the chunks are moved
|
||||
|
||||
u32 table[TABLE];
|
||||
memcpy(table, buf, sizeof(table));
|
||||
|
||||
size_t rmb = 0;
|
||||
for (int i = 0; i < chunkc; i++)
|
||||
rmb += delchunk(buf, rmb, chunkids[i], chunkids[i + 1]);
|
||||
rmb += delchunk(buf, table, rmb, chunkids[i], chunkids[i + 1]);
|
||||
|
||||
memcpy(buf, table, sizeof(table));
|
||||
return rmb;
|
||||
}
|
||||
|
||||
@@ -108,5 +117,5 @@ size_t mcx_calcsize(const u8 *restrict buf) {
|
||||
size_t size = 0;
|
||||
for (uint i = 0; i < CHUNKS; i++)
|
||||
size += *(buf + (i * 4) + 3);
|
||||
return (size * CHUNKS) + TABLE;
|
||||
return (size * CHUNKS) + (TABLE * 4);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user