Report a malformed .nup table of contents as a damaged file instead of hanging or crashing.

The table's sha256 is checked, but a table whose counts and offsets do not fit it went on: a directory listed as its
own child made tree() go round for good (the build hung at "check"), and a name pool placed before the entries, an
entry index past the lists, a name that is not UTF-8 or damaged zstd data raised struct.error, IndexError,
UnicodeDecodeError or ZstdError, which make_game.py does not catch, so the player got a traceback. All of them are
NupError now, which make_game.py and the launcher report as a damaged dump.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-08 09:27:56 -05:00
parent 95903c361a
commit 96b33aefd2

View File

@@ -43,9 +43,20 @@ class Nup:
toc = f.read(toc_size)
if len(toc) != toc_size or hashlib.sha256(toc).digest() != toc_sha:
raise NupError('the table of contents is damaged (its sha256 does not match)')
# The sha256 only says the table is what the file's writer wrote: counts and offsets that do not fit it end in
# a NupError, which the callers report, not in struct.error or IndexError
try:
self._parse(toc)
except (struct.error, IndexError, ValueError, OverflowError, MemoryError) as e:
raise NupError(f'the table of contents is malformed ({type(e).__name__}: {e})') from None
def _parse(self, toc):
tmagic, npkg, ndir, nfile, pool_off, pool_len, _ = struct.unpack_from('<IIQQQQQ', toc, 0)
if tmagic != 0x01434F54:
raise NupError('unknown table of contents')
if (npkg * PACKAGE.size + ndir * DIRECTORY.size + nfile * FILE.size + 0x30 > len(toc)
or pool_off + pool_len > len(toc)):
raise NupError('the table of contents is malformed (its counts or name pool run past its end)')
at = 0x30
self.packages = []
for i in range(npkg):
@@ -56,26 +67,40 @@ class Nup:
at += ndir * DIRECTORY.size
self.files = [FILE.unpack_from(toc, at + i * FILE.size) for i in range(nfile)]
at += nfile * FILE.size
if pool_off < at:
raise NupError('the table of contents is malformed (its name pool overlaps its entries)')
self.children = struct.unpack_from(f'<{(pool_off - at) // 4}I', toc, at)
self.pool = toc[pool_off:pool_off + pool_len]
self.ndir = ndir
def _name(self, off, n):
return self.pool[off:off + n].decode('utf-8')
try:
return self.pool[off:off + n].decode('utf-8')
except UnicodeDecodeError:
raise NupError(f'a name in the table of contents is not UTF-8 (at {off:#x})') from None
def tree(self, dir_index):
"""(path relative to the directory, file index) for every file below it."""
"""(path relative to the directory, file index) for every file below it. A directory met twice (one listed
as its own child or ancestor) is a NupError, where it went round for good."""
stack = [(dir_index, '')]
seen = set()
while stack:
d, prefix = stack.pop()
_no, _nl, first, count, _ = self.dirs[d]
for c in self.children[first:first + count]:
if c < self.ndir:
no, nl = self.dirs[c][0], self.dirs[c][1]
stack.append((c, prefix + self._name(no, nl) + '/'))
else:
fi = c - self.ndir
yield prefix + self._name(self.files[fi][0], self.files[fi][1]), fi
if d in seen:
raise NupError(f'directory {d} of the table of contents is listed below itself')
seen.add(d)
try:
_no, _nl, first, count, _ = self.dirs[d]
entries = self.children[first:first + count]
for c in entries:
if c < self.ndir:
no, nl = self.dirs[c][0], self.dirs[c][1]
stack.append((c, prefix + self._name(no, nl) + '/'))
else:
fi = c - self.ndir
yield prefix + self._name(self.files[fi][0], self.files[fi][1]), fi
except IndexError:
raise NupError(f'directory {d} of the table of contents names an entry it does not have') from None
def size(self, fi):
return self.files[fi][2]
@@ -112,7 +137,10 @@ class Nup:
if not b:
break
left -= len(b)
out = d.decompress(b)
try:
out = d.decompress(b)
except zstandard.ZstdError as e:
raise NupError(f'file {fi}: damaged ({e})') from None
if out:
got += len(out)
h.update(out)