Skip to content

libvmaf/dict: don't grow the entry array when overwriting a key - #1732

Open
0z5a wants to merge 1 commit into
Netflix:masterfrom
0z5a:dict-overwrite-when-full
Open

0z5a wants to merge 1 commit into
Netflix:masterfrom
0z5a:dict-overwrite-when-full

Conversation

@0z5a

@0z5a 0z5a commented Nov 18, 2026

Copy link
Copy Markdown

Fixes #1731.

vmaf_dictionary_set() grew the entry array before handling an overwrite, using an existing_entry pointer that it had looked up before the realloc(). When the dictionary was exactly full and the key already existed, the overwrite wrote into the freed array, the override was lost, and the old value was freed a second time on destroy.

This change grows the array only when a new entry is appended (!existing_entry && d->cnt == d->size). An overwrite never needs more space, so it no longer reallocates at all. The other paths are unchanged.

The new test_vmaf_dictionary_overwrite_when_full fills eight keys, overwrites one, and checks the new value, cnt == 8 and size == 8. Without the fix it fails in a plain build ("overwrite should not grow the dictionary") and reports a heap-use-after-free at dict.c:86 under ASan. With the fix, test_dict passes in both builds and all 26 meson tests pass in an ASan build on macOS. test_pic_preallocation needs more than the default 30 s under ASan; it passes with -t 10. The CLI case from #1731 (--model version=hdr_vmaf_v1.1.8_3d0h:adm.adm_enhn_gain_limit=1.0) no longer reports anything and goes on to the normal colorimetry check.

How I found it

I was reading vmaf_dictionary_set() and saw that existing_entry is taken before the growth block and used after it, with nothing refreshing it after the realloc(). The existing overwrite check in test_vmaf_dictionary() runs at nine entries (size 16), so the full case was never tested. I then looked for a dictionary that is exactly full in normal use. Every adm feature in the hdr_vmaf_v1.1.8_*.json models has eight options, and vmaf_model_feature_overload() merges CLI overrides into that dictionary.

Where this can show up in practice

The HDR-VMAF v1.1.8 models are now built in and are the default for PQ sources (#1709). Anyone who overrides an adm option on them, for example adm_enhn_gain_limit=1.0 for a NEG-style score, hits this on the first override. On macOS the release build silently kept the old value. A run like that reports a score computed without the requested option, or it can crash or corrupt the heap depending on what reuses the freed block. Library users who overwrite a key in any dictionary holding exactly 8, 16, 32, … entries are affected the same way. This is my estimate from reading the code; I have not seen a field report.

vmaf_dictionary_set() looks up an existing entry before it grows the
array. When the dictionary is full (cnt == size) and the key already
exists, realloc() can move the array and existing_entry then points
into freed memory: the new value is written there and the old value
is freed a second time when the dictionary is destroyed.

Only grow the array when a new entry is appended. Overwriting a key
never needs more space.

The HDR-VMAF v1.1.8 models carry exactly eight adm options, so
overriding any of them (e.g. adm.adm_enhn_gain_limit=1.0 on the
command line) hits this on the first overload.
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.

libvmaf: vmaf_dictionary_set() writes through a stale pointer when overwriting a key in a full dictionary

1 participant