Skip to content

refactor: Extract maximum transformed pixel depth calculation - #912

Open
17krishna8 wants to merge 3 commits into
pnggroup:libpng18from
17krishna8:refactor/max-pixel-depth
Open

refactor: Extract maximum transformed pixel depth calculation#912
17krishna8 wants to merge 3 commits into
pnggroup:libpng18from
17krishna8:refactor/max-pixel-depth

Conversation

@17krishna8

Copy link
Copy Markdown

Consolidates the maximum-transformed-pixel-depth computation flagged by the existing WARNING/TODO in png_read_start_row (pngrutil.c): the same value must agree with png_read_transform_info (pngrtran.c) and png_do_read_transformations, or memory overwrites become possible. This moves the pngrutil.c instance into a single documented static helper, png_read_max_pixel_depth(), as the first step of making that invariant enforceable instead of folklore.

No functional change. Validated: ASan+UBSan build, full CTest suite 37/37, pngtest, plus a 20-run APNG stress corpus (sequential + progressive APIs). Follow-up to #911.

The CDfcTL entry declared max_length=25 with min_length=26, which no
chunk length can satisfy: png_handle_chunk rejects length < 26 as "too
short" and everything else as "too long".

An fcTL chunk is exactly 26 bytes (4-byte sequence number + 22 bytes of
frame data), so both bounds must be 26, matching the CDacTL pattern
where an acTL is exactly 8 bytes.  The entry is currently unreachable
because the APNG handlers are #defined to NULL and such chunks are
routed to unknown-chunk handling, but the table must stay correct so
that enabling native table-driven APNG dispatch does not silently
reject every valid fcTL.
When png_handle_fcTL declined an fcTL chunk (for example an oversized
frame description for the leading frame), the progressive reader
reported "Missing required fcTL chunk in APNG stream" even though the
fcTL was present in the stream; the misleading message sent users
hunting for a nonexistent missing-chunk problem.

The sequential reader already aborts such streams with the accurate
"Out-of-order sequence number"/"Oversized frame in fcTL" diagnostics
produced by the handler itself, so only the follow-up message here
needs to describe the actual situation: the chunk was seen but not
accepted.
png_read_start_row() open-codes the computation of the maximum pixel
depth that a row can have after all enabled read transformations.  The
accompanying warning notes that png_read_transform_info (pngrtran.c)
performs similar calculations and that the two must agree, otherwise
memory overwrites become possible; the TODO asks for this to be fixed.

As a first step, move the computation into its own function,
png_read_max_pixel_depth(), so that:
- the logic exists in exactly one place within pngrutil.c,
- the invariant it maintains is documented next to the code,
- follow-up work can share or assert on this value instead of
  re-deriving it.

No functional change; behavior is identical for all configurations.
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.

1 participant