Skip to content

cache_meta: keep a build buffer when growing it fails - #59

Merged
reinauer merged 1 commit into
mainfrom
fix/meta-cache-leak
Nov 26, 2026
Merged

reinauer merged 1 commit into
mainfrom
fix/meta-cache-leak

Conversation

@reinauer

Copy link
Copy Markdown
Owner

cache_meta: keep a build buffer when growing it fails

The first time a directory is listed, the handler collects its entries in two buffers and makes them larger as it goes. If there was no memory for the larger copy, the old buffer was lost: both callers of meta_grow() stored its result straight into the pointer they were growing, so the pointer became NULL and the cleanup had nothing to free.

  • Fix: the callers take the result in a variable of their own and replace the pointer only if the allocation worked. The old buffer stays with the build, which frees it on the way out as after every other failure.
  • When it happened: the string pool starts at 16 bytes per estimated entry, so it does grow on ordinary discs. For the leak the system had to be out of memory at that moment; each time, one build buffer was lost.

Found while counting the handler's allocations with a patch on exec's allocator. Nothing reported it.

Testing

  • New unit test test_cache_meta_nomem: it builds the cache with an allocator that fails from the Nth allocation on and counts the blocks that are not given back, letting one more allocation succeed each round until the build goes through. On main it fails with one block left; with the fix it passes.
  • Host: make check, golden-check, malformed-check and fuzz-check pass.
  • Builds: 68000, 68020, ROM and OS4 build without warnings; tests/aros/check_abi.sh passes.
  • Emulators: the 5,028-check regression tool passes on OS 3.2 (WinUAE), AmigaOS 4.1 FE Update 3 (QEMU pegasos2) and AROS i386 (QEMU).
  • Speed: CDBench on OS 3.2 (68020 build, three runs) is unchanged: Discovery cold 9,845 and ExAll 12,877 entries/s on main and with the fix.
  • Not tested: the failure itself on an Amiga; only the host test makes the allocation fail.

Size: +4 bytes on the 68000 and 68020 handlers, ROM unchanged.

meta_build() collects a directory in two buffers and makes them larger
as it goes. Both callers of meta_grow() stored its result straight
into the pointer they were growing. When there was no memory for the
larger copy, that pointer became NULL, the buffer it had pointed to was
never freed, and the build went on to fail.

Take the result in a variable of its own and replace the pointer only
if there is one. The old buffer then stays with the build, which frees
it on the way out as it does after every other failure.

The string pool starts at 16 bytes per estimated entry and names are
often longer, so it does grow on ordinary discs. For the leak the
handler had to run out of memory at that moment.

Add a unit test that lets one more allocation succeed each round and
counts the blocks that are not given back.
@reinauer
reinauer merged commit d9439c3 into main Nov 26, 2026
3 checks passed
@reinauer
reinauer deleted the fix/meta-cache-leak branch November 27, 2026 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant