Skip to content

fix(k2proc): fix memset size calculation - #70

Open
tachibana-shin wants to merge 1 commit into
koreader:masterfrom
tachibana-shin:fix/k2proc-allow-wrong
Open

fix(k2proc): fix memset size calculation#70
tachibana-shin wants to merge 1 commit into
koreader:masterfrom
tachibana-shin:fix/k2proc-allow-wrong

Conversation

@tachibana-shin

@tachibana-shin tachibana-shin commented Jul 22, 2026

Copy link
Copy Markdown

bug: memset() only clears a fraction of the allocated pixel_count_array, leaving most of it uninitialized from malloc.

// Before (bug):
memset(pixel_count_array, 0, sizeof(int)*(region->c2+2) + (region->r2+2));
//  = 4*(c2+2) + (r2+2) bytes — only clears ~1/(c2+2) of the array

// After (fix):
memset(pixel_count_array, 0, sizeof(int)*(region->c2+2) * (region->r2+2));
//  = 4*(c2+2)*(r2+2) bytes — clears the entire array
Evidence:
1. Allocation uses * correctly (k2proc.c:1904):
willus_mem_alloc((double **)&pixel_count_array,
    sizeof(int) * (region->c2+2) * (region->r2+2), funcname);
2. Loop accesses the full 2D range (k2proc.c:1917-1933):
rows_per_column = region->r2 + 2;
for (i = 0; i <= region->c2+1; i++)
    cp = &pixel_count_array[i * rows_per_column];  // offset = i * (r2+2)
    for (j = 1; j < jmax; j++)  // jmax <= r2+2
3. memset must zero the same (c2+2)*(r2+2) ints that the loop writes to.

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/koreader/libk2pdfopt/70)
<!-- Reviewable:end -->

@Frenzie

Frenzie commented Jul 22, 2026

Copy link
Copy Markdown
Member

If you have the time, please also try to report those upstream: https://www.willus.com/email.shtml

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.

2 participants