Repository navigation
pngset: Release the previous pCAL/sCAL data when png_set_pCAL and png_set_sCAL_s are called again - #987
Open
takerusaito4187 wants to merge 1 commit into
Conversation
png_set_pCAL and png_set_sCAL_s overwrote the pointers stored in the info struct with freshly allocated buffers without releasing the previous allocations, leaking the first call's strings and parameter array when either function was called twice on the same info struct. Follow the order already used by png_set_iCCP and png_set_eXIf_1: allocate and fill the replacement values in locals, release the old data with png_free_data, then store. This also leaves the previously stored data intact if an allocation fails, and makes getter-to-setter aliasing safe.
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.
Summary
png_set_pCALandpng_set_sCAL_soverwrite the pointers stored in theinfo struct with freshly allocated buffers without releasing the previous
allocations. Calling either function twice on the same info struct leaks
the first call's allocations: for pCAL the purpose string, the units
string, the parameter array and each parameter string; for sCAL the width
and height strings.
This change makes both functions release the previous data, so repeated
calls replace the stored values instead of leaking them.
How I found it
I audited all 46
png_set_*functions defined inpngset.cfor thepattern "allocate a replacement value and overwrite the stored pointer
without releasing the old one". The heap-owning setters fall into three
groups:
png_set_PLTE,png_set_tRNSandpng_set_hISTsnapshot the caller'sdata, release the old storage with
png_free_data, then copy;png_set_iCCPandpng_set_eXIf_1allocate and fill the replacementfirst and release the old storage afterwards.
png_set_text_2,png_set_sPLT,png_set_unknown_chunks) grow their arrays and defer releasing the oldarray until after the copy.
png_set_pCALandpng_set_sCAL_swere the only two that overwrotethe stored pointers with no release at all.
The remaining setters store scalars or caller-owned pointers and do not
own heap data in the info struct.
The change
Both functions now follow the order already used by
png_set_iCCPandpng_set_eXIf_1:png_free_data(..., PNG_FREE_PCAL / PNG_FREE_SCAL, 0).Consequences beyond fixing the leak:
(previously a failed call could leave a partially overwritten info
struct). This matches the failure behaviour of
png_set_iCCPandpng_set_eXIf_1.png_get_pCAL/png_get_sCAL_sbackinto the setters (getter-to-setter aliasing) is safe, because the old
storage is released only after the copies are made. In
png_set_pCALthe release happens before
pcal_nparamsis overwritten, sincepng_free_datareleases the parameter strings using the stored countof the data being replaced.
No public API or ABI changes: only the bodies of the two functions in
pngset.care modified.png_set_sCALandpng_set_sCAL_fixedarecovered because they delegate to
png_set_sCAL_s.The affected code is identical in the libpng16 branch (verified against
v1.6.59), so the change should apply there unchanged; I am happy to
retarget or provide a second PR if preferred.
Impact and scope
This is a robustness fix with low severity, stated plainly:
duplicate pCAL or sCAL chunks is rejected by the chunk handlers
("pCAL: duplicate" / "sCAL: duplicate"), so the setters are not called
twice when reading. I verified this dynamically.
png_set_pCALorpng_set_sCAL_s(or thepng_set_sCALwrappers) more than once onthe same info struct, e.g. when reusing an info struct on the write
side. Each repeated call leaks the previous chunk's strings.
Testing
All testing was done locally on x86_64 with an ASan+UBSan build
(
-fno-sanitize-recover=all):png_set_pCALtwice andpng_set_sCAL_stwice on one info struct reported 48 bytes leaked in6 allocations under LeakSanitizer before the change (allocation sites
in
png_set_pCALandpng_set_sCAL_s). After the change the sameprogram exits cleanly with no leak report.
after repeated calls, including when the pCAL parameter count changes
between calls (3 -> 1 -> 3), and when getter results are passed back
into the setters.
Happy to adjust the approach or add a regression test in the project's
preferred form if the maintainers would like one.