]> gitweb.michael.orlitzky.com - libsvgtiny.git/commit
src/svgtiny.c: deallocate stylesheets during finalisation
authorMichael Orlitzky <michael@orlitzky.com>
Sat, 7 Jun 2025 16:54:46 +0000 (12:54 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 9 Jun 2025 01:13:07 +0000 (21:13 -0400)
commitd51e63d96cf36fe734b57d2fec4c4f45ddb69866
tree2b39f384d319e275894fd557aae27a9b9f39b847
parent133bdc01dc9ee8edae10975db353efc1bdc30576
src/svgtiny.c: deallocate stylesheets during finalisation

We allocate stylesheets in svgtiny_parse_style_element() and append
them to our global select_ctx, but the select_ctx retains only a const
reference to them and is not responsible for freeing their resources.
Instead, we have to do it during finalisation.

This is a little ugly because we have to de-const the sheet references
before we can destroy them. This relies on the non-local knowledge
that every sheet appended to the context does in fact originate in the
one function svgtiny_parse_style_element(). On the other hand, using
the context to keep track of these sheets saves us the trouble of
maintaining a duplicate array that would differ only in type
signature.

In summary:

  * The sheets are now removed from the context before destroying
    them. This avoids a potential issue where destroying the context
    itself (which we do afterwards) might try to access the sheets.

  * If we fail to obtain a pointer to a sheet, we stop processing that
    sheet. This should not happen, and if something extremely weird
    happens it can hide the problem, but that's still better than the
    segfault we'd get from trying to operate on an invalid pointer.

  * The sheets are removed/destroyed in reverse order, last-in
    first-out.

  * Extremely unlikely error paths call assert(). Production builds
    will still catch and propagate errors.
src/svgtiny.c