createImageData() with negative width and height aborts the process
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, node.js
Research direction
Start by reading Context2d::CreateImageData, named in the issue, and trace how it converts width and height before allocating the buffer. Compare the behavior with the cited Canvas specification and reproduce the two negative-size cases from the provided script. Done means both return positive dimensions without throwing or aborting; add or run focused tests for those cases.
Written by the indexing model from the issue text.
Description
Version of node-canvas
v3.2.3, v4.0.0-rc3 (master 6cc138b)
Version of node
v26.13.0
What operating system does the problem occur on?
Linux
Steps to reproduce
ctx.createImageData(sw, sh) should use the absolute values of sw and sh. In node-canvas a negative size is read as a huge unsigned number: one negative argument throws, and two negative arguments abort Node with a fatal out-of-memory error that cannot be caught.
const { createCanvas } = require('canvas');
const ctx = createCanvas(10, 10).getContext('2d');
try { const d = ctx.createImageData(-3, 2); console.log(d.width, d.height); } catch (e) { console.log('threw:', e.message); }
const d = ctx.createImageData(-3, -2);
console.log(d.width, d.height);
Run node repro.js; echo $?:
Actual:
threw: buffer exceeds 2147483647 bytes
FATAL ERROR: v8::ArrayBuffer::New Allocation failed - process out of memory
...
134
Expected (matches Chrome 153):
3 2
3 2
Additional information/comments
Per https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createimagedata, createImageData(sw, sh) must initialize the ImageData object with the absolute magnitude of sw and sh. In Context2d::CreateImageData, the arguments are converted with Uint32Value(), turning -3 into 4294967293; with two negative values Bpp * width * height overflows int64_t, bypasses the nBytes > INT32_MAX check, and passes a negative nBytes to Napi::ArrayBuffer::New.
Credit: https://ucsbplse.github.io/
- Dominant language
- JavaScript
- Stars
- 10.7k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Automattic/node-canvas
-
Feature Text & Fonts
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Automattic/node-canvas#2364 · 3 comments · 2 reactions ·
-
Feature
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Automattic/node-canvas#2621 ·
-
actualBoundingBoxLeft has the sign inverted relative to canvas's own rasteriser (3.2.0, 3.2.3)Possibly taken @florianaltendorfer claimed this 13 days ago. OpenBug
Difficulty 3/5 1-2 days Newbie friendliness 68/100
Automattic/node-canvas#2620 · 1 comment ·
-
Bug
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Automattic/node-canvas#2619 · 1 comment ·
-
The PDF file created by createPDFStream becomes larger in size, and createWriteStream is very slowOpenBug performance
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Automattic/node-canvas#2615 · 2 comments ·
All issues in Automattic/node-canvas
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
capricorn86/happy-dom#2594 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
microsoft/monaco-editor#5575 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Shrinking an array with `length =` and growing it back loses the patches for the truncated indicesPossibly taken @RClaudiuM claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 4 days