8000 Respect Buffer view bounds in fromBitmap and quantize by Shrusti-pk · Pull Request #1419 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

Respect Buffer view bounds in fromBitmap and quantize - #1419

Open
Shrusti-pk wants to merge 1 commit into
jimp-dev:mainfrom
Shrusti-pk:buffer-view-bounds
Open

Shrusti-pk wants to merge 1 commit into
jimp-dev:mainfrom
Shrusti-pk:buffer-view-bounds

Conversation

@Shrusti-pk
Copy link
Copy Markdown

What's Changing and Why

Jimp.fromBitmap() and the quantize plugin read data.buffer and ignore byteOffset/byteLength. Node serves small Buffers (under 4 KiB, from Buffer.from() or Buffer.allocUnsafe()) as slices of a shared 8 KiB pool, so .buffer is the whole pool, not the image.

  • fromBitmap: a Buffer also matches the Uint8Array branch, which overwrites the copy made just above it with Buffer.from(data.buffer). The bitmap then aliases the pool: pixels come from unrelated Buffers in the process, and writes to the image overwrite them.
  • quantize: image-q's PointContainer.fromUint8Array reads .buffer itself, so a bounded view is not enough and it now gets a copy. After a small crop() (which uses Buffer.allocUnsafe), quantize() built its palette from pool memory and returned an 8 KiB bitmap.

Repro on Node 22 before this change:

const secret = Buffer.from("SECRET-API-TOKEN-0123456789abcdef");
const pixels = Buffer.from([
  0xff, 0, 0, 0xff, 0xff, 0, 0, 0xff,
  0xff, 0, 0, 0xff, 0xff, 0, 0, 0xff,
]);
const image = Jimp.fromBitmap({ data: pixels, width: 2, height: 2 });

image.bitmap.data.length; // 8192, expected 16
image.bitmap.data.includes("SECRET-API-TOKEN"); // true
image.bitmap.data.fill(0x41);
secret.toString(); // "AAAAAAAA..."

What else might be affected

A Buffer passed to fromBitmap is now actually copied, as that branch intended, so editing the image no longer writes back into the caller's Buffer. Uint8Array/Uint8ClampedArray input is still wrapped without a copy, limited to the view's own bytes.

Tasks

  • Add tests
  • Update Documentation
  • Update jimp.d.ts
  • Add SemVer Label

The new tests fail before the change and pass after. pnpm build, pnpm lint, pnpm test and pnpm test:browser pass locally.

This branch has not been deployed

No deployments
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

0