diff --git a/src/Formats/model/x/Build.luau b/src/Formats/model/x/Build.luau index 2acb8f3..16d41e6 100644 --- a/src/Formats/model/x/Build.luau +++ b/src/Formats/model/x/Build.luau @@ -34,11 +34,25 @@ local function str(c: Vals): string end return x end +-- a non-negative integer (an index) +local function index(c: Vals, limit: number): number + local n = num(c) + if n < 0 or n ~= math.floor(n) or n > limit then + error("bad index " .. n, 0) + end + return n +end + +-- a count of items that follow (each takes at least one value: a count past the data is refused before anything +-- is allocated for it) local function count(c: Vals, limit: number): number local n = num(c) if n < 0 or n ~= math.floor(n) or n > limit then error("bad count " .. n, 0) end + if n > #c.v - c.i + 1 then + error("truncated: a count of " .. n .. " runs past the data", 0) + end return n end @@ -146,7 +160,7 @@ local function mesh(ctx: Ctx, m: Node, triangulate: boolean): (number?, Bones?) end local face = table.create(n) :: { number } for k = 1, n do - local idx = count(c, 2 ^ 32) + local idx = index(c, 2 ^ 32) if idx >= nv then error("face index " .. idx .. " out of range", 0) end @@ -176,7 +190,7 @@ local function mesh(ctx: Ctx, m: Node, triangulate: boolean): (number?, Bones?) local n = count(kc, ctx.maxVertices) local face = table.create(n) :: { number } for q = 1, n do - face[q] = count(kc, 2 ^ 32) + face[q] = index(kc, 2 ^ 32) end nfs[f] = face end @@ -194,7 +208,7 @@ local function mesh(ctx: Ctx, m: Node, triangulate: boolean): (number?, Bones?) local n = count(kc, ctx.maxVertices) local col = table.create(nv * 4, 1) :: { number } for _ = 1, n do - local idx = count(kc, 2 ^ 32) + local idx = index(kc, 2 ^ 32) local r, g, b, a = num(kc), num(kc), num(kc), num(kc) if idx < nv then col[idx * 4 + 1], col[idx * 4 + 2], col[idx * 4 + 3], col[idx * 4 + 4] = r, g, b, a @@ -202,10 +216,10 @@ local function mesh(ctx: Ctx, m: Node, triangulate: boolean): (number?, Bones?) end table.insert(colors, col) elseif k.type == "MeshMaterialList" then - count(kc, 2 ^ 20) -- material count (the objects below are authoritative) + num(kc) -- material count (the objects below are authoritative) local ni = count(kc, ctx.maxVertices) for q = 1, ni do - faceMat[q] = count(kc, 2 ^ 20) + faceMat[q] = index(kc, 2 ^ 20) end for _, mk in k.kids do if mk.type == "@ref" then @@ -220,7 +234,7 @@ local function mesh(ctx: Ctx, m: Node, triangulate: boolean): (number?, Bones?) local n = count(kc, ctx.maxVertices) local verts = table.create(n) :: { number } for q = 1, n do - verts[q] = count(kc, 2 ^ 32) + verts[q] = index(kc, 2 ^ 32) end local weights = table.create(n) :: { number } for q = 1, n do diff --git a/tests/model/x.luau b/tests/model/x.luau index 119cbdc..8cc3524 100644 --- a/tests/model/x.luau +++ b/tests/model/x.luau @@ -287,6 +287,8 @@ do t:check(e4 and e4.code == Err.CORRUPT, "face index out of range: " .. tostring(e4)) local _, e5 = decode("xof 0303txt 0032 Frame A { ") t:check(e5 and e5.code == Err.CORRUPT, "unterminated: " .. tostring(e5)) + local _, e7 = decode("xof 0303txt 0032 Mesh { 33554431; 1; }") + t:check(e7 and e7.code == Err.TRUNCATED, "a vertex count past the data is refused before allocating: " .. tostring(e7)) local _, e6 = decode(CUBE, { maxValues = 10 }) t:check(e6 and e6.code == Err.LIMIT, "maxValues: " .. tostring(e6)) t:eq(X.sniff(Bytes.reader(buffer.fromstring(CUBE))), 1, "sniff")