fix(koptreflow): margin calculation, memory leaks, and div-by-zero - #68
Open
tachibana-shin wants to merge 5 commits into
Open
fix(koptreflow): margin calculation, memory leaks, and div-by-zero#68tachibana-shin wants to merge 5 commits into
tachibana-shin wants to merge 5 commits into
Conversation
marbot used box[1] (top margin) instead of box[3] (bottom margin), causing the bottom margin to be calculated with the wrong value.
When k2pdfopt_reflow_bmp() is called multiple times, the previous rboxa/nboxa/rnai/nnai were overwritten without being freed, causing memory leaks.
wrectmaps entries accumulated across calls to k2pdfopt_reflow_bmp() because the rectmaps were not cleared before the loop that adds new entries.
srcdpiw, srcdpih, and zoom are used as divisors without checking for zero. When any of these is zero, the division causes undefined behavior. Skip nlbox creation when divisors are not positive.
Validate pix_data pointer and bitmap dimensions before accessing pixel memory. Returns early on null data or non-positive dimensions instead of causing out-of-bounds reads.
Frenzie
approved these changes
Jul 22, 2026
Member
|
@benoit-pierre You said you're also working on something. Should these PRs merged first or after? |
Member
|
I would be great if could please hold off. |
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.
Problem 1: Copy-paste error in margin calculation
In
lib/koptreflow.c:110, the bottom margin was calculated using the wrong index:Both used
box[1](top margin). Theboxarray is indexed as:box[0]=left,box[1]=top,box[2]=right,box[3]=bottom. The bottom margin should usebox[3].Fix
Problem 2: Memory leak on reflow
When
k2pdfopt_reflow_bmp()is called multiple times (e.g. reflowing different pages), the previousrboxa,nboxa,rnai,nnaiBOXA/NUMA objects are overwritten without being freed. Similarly,wrectmapsentries accumulate across calls.Fix
Free previous allocations before creating new ones:
Problem 3: Division by zero
In the rectmaps loop,
rectmap->srcdpiw,rectmap->srcdpih, andkctx->zoomare used as divisors without checking for zero:Fix
Guard with zero checks:
Problem 4: Unchecked input in pixmap_to_bmp()
pixmap_to_bmp()does not validatepix_datapointer or bitmap dimensions before accessing pixel memory.Fix
This change is