libass: Fix cache lookup problem causing memory bloat
commit312d9e4b104741b834aa5d71b02228d0cd988a4e
authorUoti Urpala <uau@glyph.nonexistent.invalid>
Sun, 21 Dec 2008 21:32:07 +0000 (21 23:32 +0200)
committerUoti Urpala <uau@glyph.nonexistent.invalid>
Sun, 21 Dec 2008 22:46:52 +0000 (22 00:46 +0200)
treec287118d1de9612fca06239205d7d745b0147c2d
parentfccb0a7e45b61128262a396f2426bd7168fd198c
libass: Fix cache lookup problem causing memory bloat

The cache code did hash lookups by storing key values in struct fields
and then hashing and comparing the struct as a single memory block. In
at least one case such a struct contained uninitialized padding bytes
which prevented the complete memory area of the struct from matching
even though the fields did. As a result the code failed to find
existing objects in the cache and stored new versions of them, causing
gigabytes of memory use in some circumstances. Initializing the struct
memory to zero before writing the fields avoided such memory use in
tests but is not guaranteed to work if I interpret the C standard
correctly (the compiler is allowed to write garbage over padding bytes
when changing struct member values).

Change the code to use struct-specific hashing and comparison
functions that work field by field to guarantee correct behavior.
Create these by replacing the struct definition with a template that
lists the fields and can be used the generate each of struct
definition, hash function and compare function with some preprocessor
magic (otherwise every field would need to be listed separately in all
three).
libass/ass_cache.c
libass/ass_cache.h
libass/ass_cache_template.c [new file with mode: 0644]