Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid images cause process to crash instead of throwing error #641

Closed
gc opened this issue Feb 25, 2023 · 0 comments · Fixed by #642
Closed

Invalid images cause process to crash instead of throwing error #641

gc opened this issue Feb 25, 2023 · 0 comments · Fixed by #642

Comments

@gc
Copy link

gc commented Feb 25, 2023

When trying to ctx.drawImage an invalid image, the entire process gets instantly/silently crashed, with no error.

The expectation is to throw an error that we can catch.

const { writeFileSync } = require('fs');

async function test(canvasCtor, loadImage, name) {
    try {
        console.log(`Starting to test ${name}`)
        const canvas = new canvasCtor(100, 100);
        const image = await loadImage(('./badimage.png'));
        const ctx = canvas.getContext('2d');
        ctx.drawImage(image, 0, 0);
        writeFileSync(`./${name}.png`, canvas.toBuffer());
    } catch (e) {
        console.error(`${name} failed with: ${e}`)
    }
    console.log(`Finished testing ${name}`);
}

setInterval(() => {
    // Keep the event loop awake until we manually CTRL+C
}, 10_000);


async function main() {
    await test(require("canvas").Canvas, require("canvas").loadImage, "node-canvas")
    await test(require("skia-canvas").Canvas, require("skia-canvas").loadImage, "skia-canvas");
    await test(require("@napi-rs/canvas").Canvas, require("@napi-rs/canvas").loadImage, "@napi-rs/canvas");
}

main();
$ node ./
Starting to test node-canvas
node-canvas failed with: Error: Unsupported image type
Finished testing node-canvas
Starting to test skia-canvas
skia-canvas failed with: Error: Missing Source URL
Finished testing skia-canvas
[Process crashes here]

canvas and skia-canvas both handle it by throwing an error, napi-canvas will crash entirely. Here's a script to reproduce it.

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 a pull request may close this issue.

1 participant