Skip to content

fix(bmp): correct bpp shift and prevent overflow - #69

Open
tachibana-shin wants to merge 1 commit into
koreader:masterfrom
tachibana-shin:fix/bmp-wrong-shift-bit
Open

fix(bmp): correct bpp shift and prevent overflow#69
tachibana-shin wants to merge 1 commit into
koreader:masterfrom
tachibana-shin:fix/bmp-wrong-shift-bit

Conversation

@tachibana-shin

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

Copy link
Copy Markdown
  • Correct bit shift to calculate bytes per pixel
  • Change tc to long to prevent integer overflow

Fix: bpp>>8bpp>>3 in bmp_clear_outside_crop_border

Bug: bytes_per_pix was always 0, causing all memset() calls in this function to be no-ops (writing 0 bytes). Color bitmap crop borders were never cleared to white.

Root cause: bpp is bits per pixel (8 or 24, per willus.h:517). Dividing by 256 (>>8) is wrong — should be dividing by 8 (>>3) to convert bits to bytes.

Evidence:

  1. Struct definition (willus.h:517):
   int bpp;  /* Bits per pixel (only 8 or 24 allowed) */
  1. Same function — bytes_per_pix is used to calculate memset size (k2bmp.c:244-280):
p = bmp_rowptr_from_top(src, i);
memset(p, 255, n * bytes_per_pix);  // left border
// ...
memset(p, 255, src->width * bytes_per_pix);  // top/bottom rows

With bytes_per_pix=0, memset writes 0 bytes — color borders are never cleared.
4. Consistent with bmp_bytewidth (bmp.c:1410):

return(bmp->bpp==24 ? bmp->width*3 : bmp->width);

Same conversion: bpp 24 → 3 bytes, bpp 8 → 1 byte.
Before: bpp>>8: 8→0, 24→0 (always 0, wrong)
After: bpp>>3: 8→1, 24→3 (correct bytes per pixel)


This change is Reviewable

- Correct bit shift to calculate bytes per pixel
- Change tc to long to prevent integer overflow

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, sounds good.

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