Skip to content

model: preserve a collection when growth allocation fails - #1590

Open
lusoris wants to merge 1 commit into
Netflix:masterfrom
vidiko:fix/model-collection-allocation
Open

lusoris wants to merge 1 commit into
Netflix:masterfrom
vidiko:fix/model-collection-allocation

Conversation

@lusoris

@lusoris lusoris commented Sep 8, 2026 •

Copy link
Copy Markdown

When vmaf_model_collection_append() grows an already populated collection and realloc() fails, the common fail label clears the caller's collection pointer. That loses access to the existing allocation and models even though realloc() left them intact.

Return -ENOMEM directly from this growth failure. The original collection stays valid, the new model remains with the caller, and a later append can retry. The successful path is unchanged.

The regression injects the failure after filling the initial eight slots, checks the collection pointer, backing array, count, capacity and existing model, retries the append, and destroys the collection. It fails against the original implementation with “allocation failure lost the existing collection”. The test includes model.c, which reaches ref.h and <stdatomic.h>, so it links stdatomic_dependency as test_model does; without it the MSVC build stops with C1189.

Update, 2026-11-17: the commit now also gives the new test stdatomic_dependency; nothing else changed. The branch is based on 4611e47a; the commands below were last run on 9e48141b.

  • CI on a fork branch with the same tree plus one fork-only commit that pins the workflow actions to commit SHAs (the fork requires pinned actions): libvmaf, checkasm, ffmpeg, Windows and Docker all pass. Without the stdatomic_dependency line the Windows run fails with C1189 in both MSVC builds (its MinGW job failed earlier, on a package mirror download).

Validation against upstream 9e48141bd1eb8d2329e09d3744e7c24af53017ca, x86-64 Linux, GCC 16.2.1, Meson 1.12.1. Rebased on master 9e48141 (2026-10-02); the patch is unchanged and the results below were re-run (the negative control was run on 6ec23e8; the code involved is unchanged in 9e48141):

meson setup build-release libvmaf --buildtype=release -Denable_cuda=false -Denable_float=true -Denable_docs=false
ninja -C build-release
meson test -C build-release --print-errorlogs
# 25/25 passed

meson setup build-sanitize libvmaf --buildtype=debug -Db_sanitize=address,undefined -Db_lto=false -Denable_cuda=false -Denable_float=true -Denable_docs=false
ninja -C build-sanitize
ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=halt_on_error=1 meson test -C build-sanitize test_model_collection_alloc --print-errorlogs
# passed; original-source negative control failed as expected

No scoring code or golden-data assertions changed; the three Netflix reference pairs score identically with and without this change (default dispatch and SIMD masked off). Sanitizer acceptance here is for the new regression; separate baseline leaks in upstream test_predict and test_pic_preallocation remain outside this patch.

@lusoris
lusoris force-pushed the fix/model-collection-allocation branch 2 times, most recently from b7dc078 to 5446d13 Compare September 19, 2026 10:07
@lusoris
lusoris force-pushed the fix/model-collection-allocation branch 4 times, most recently from 74450de to f2def31 Compare October 2, 2026 18:38
@lusoris
lusoris force-pushed the fix/model-collection-allocation branch from f2def31 to f2e9135 Compare November 1, 2026 23:34
@lusoris
lusoris force-pushed the fix/model-collection-allocation branch from f2e9135 to 556f709 Compare November 17, 2026 17:00
When vmaf_model_collection_append() cannot grow the model array, it
jumps to the fail label, which sets *model_collection to NULL without
freeing anything. The caller loses its only handle to the collection
and every model already in it leaks. realloc() leaves the old array
valid on failure, so return -ENOMEM and leave the collection as it
was; the caller keeps ownership of the model it tried to append.

test_model_collection_alloc fills a collection to capacity, makes the
growth realloc() fail, checks that the collection pointer, array,
count, capacity and members are unchanged, then retries the append.
The test includes model.c, which reaches ref.h and <stdatomic.h>, so
it links stdatomic_dependency as test_model does; MSVC refuses that
header otherwise.

Signed-off-by: Lusoris <lusoris@pm.me>
@lusoris
lusoris force-pushed the fix/model-collection-allocation branch from 556f709 to 1e8ea5c Compare November 17, 2026 21:50
@lusoris lusoris changed the title fix(model): preserve a collection when growth allocation fails model: preserve a collection when growth allocation fails Nov 17, 2026
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