Add a mod's BYML key offsets in size_t, so an offset near 4 GiB no longer passes the bounds check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-09 05:57:17 -05:00
parent 02a4b024a6
commit 9b4f461e6f
2 changed files with 7 additions and 5 deletions

View File

@@ -73,8 +73,10 @@ static int scale_sensitivity(uint8_t *p, size_t n, float k, float was[8]) {
if (!u32_at(p, n, root + 4 + 8 * (size_t)i, &e) || (e & 0xffffff) >= nkeys ||
!u32_at(p, n, keys + 4 + 4 * (size_t)(e & 0xffffff), &at))
return 0;
const char *name = (const char *)p + keys + at;
if (keys + at + sizeof "StickSensitivity" > n || strcmp(name, "StickSensitivity")) continue;
// in size_t: a mod's key offset near 2^32 would wrap the 32-bit sum below n
if ((size_t)keys + at + sizeof "StickSensitivity" > n) continue;
const char *name = (const char *)p + (size_t)keys + at;
if (strcmp(name, "StickSensitivity")) continue;
if (e >> 24 != 0xc0 || !u32_at(p, n, root + 8 + 8 * (size_t)i, &arr) || !u32_at(p, n, arr, &ah) ||
(ah & 0xff) != 0xc0)
return 0;

View File

@@ -208,9 +208,9 @@ static int u32_at(const Byml *b, size_t off, uint32_t *v) {
static const char *key_name(const Byml *b, uint32_t k) {
uint32_t head, at;
if (!u32_at(b, b->keys, &head) || (head & 0xff) != 0xc2 || k >= head >> 8 || !u32_at(b, b->keys + 4 + 4 * k, &at) ||
b->keys + at >= b->n || !memchr(b->p + b->keys + at, 0, b->n - b->keys - at))
return NULL;
return (const char *)b->p + b->keys + at;
(size_t)b->keys + at >= b->n || !memchr(b->p + (size_t)b->keys + at, 0, b->n - ((size_t)b->keys + at)))
return NULL; // in size_t: a mod's table offset near 2^32 would wrap the 32-bit sum below n
return (const char *)b->p + (size_t)b->keys + at;
}
// The offset of the value of hash node `node`'s entry `name`, which must have type `type`; 0 when there is none.