Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1731.
vmaf_dictionary_set()grew the entry array before handling an overwrite, using anexisting_entrypointer that it had looked up before therealloc(). 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_fullfills eight keys, overwrites one, and checks the new value,cnt == 8andsize == 8. Without the fix it fails in a plain build ("overwrite should not grow the dictionary") and reports a heap-use-after-free atdict.c:86under ASan. With the fix,test_dictpasses in both builds and all 26 meson tests pass in an ASan build on macOS.test_pic_preallocationneeds 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 thatexisting_entryis taken before the growth block and used after it, with nothing refreshing it after therealloc(). The existing overwrite check intest_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 thehdr_vmaf_v1.1.8_*.jsonmodels has eight options, andvmaf_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.0for 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.