-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Segmentation fault when loading an SVG buffer on v3. #2486
Comments
Thanks, this probably happened in the NAPI change then. I'll fix it this weekend and add some tests for SVG. |
Any update on this? |
Are you sure that it ever worked? I dug into this (after being delayed for a while because I didn't know 2.11.2 is totally broken with modern node versions) and here's what I found: you need to specify // test.js
const { loadImage } = require("canvas");
loadImage(Buffer.from(`<svg width="1" height="1" xmlns="http://www.w3.org/2000/svg"><path d="M1,1"/></svg>`)); Of course, it shouldn't be crashing. There is a problem in the error path where Even without the crash, the error is totally unhelpful, so I'll add some friendlier messaging. |
My SVGs specify a // test.js
const { loadImage, createCanvas } = require("canvas");
const { writeFileSync } = require("fs");
loadImage(
Buffer.from(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<rect fill="red" width="1" height="1" />
</svg>`),
).then((img) => {
const canvas = createCanvas(10, 10),
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
writeFileSync("image.png", canvas.toBuffer());
}); This works properly on 2.11.2 What node version are you considering modern? Works fine on v20.18.2 Thanks for the fix! |
Just tested this commit and still having issues. Run using latest commit: Run using v2.11.2: Logging the width/height of the image is correct.
Another example with a more complicated SVG: // test.js
const { loadImage, createCanvas } = require("canvas");
const { writeFileSync } = require("fs");
loadImage(
Buffer.from(`
<svg xmlns="http://www.w3.org/2000/svg" viewBox="709 0 64 64" fill="#00c805">
<path
d="M737.61 50.7l-.427.14c-2.74.909-6.796 2.311-10.435 3.982-.193.093-.32.35-.32.35-.07.157-.153.349-.246.564l-.012.03c-.408.925-.968 2.316-1.213 2.887l-.187.448a.18.18 0 00.047.204.167.167 0 00.123.052.234.234 0 00.081-.017l.438-.21c.997-.471 2.257-1.188 3.575-1.816l.047-.023a2417.36 2417.36 0 007.058-3.348s.274-.145.414-.419l1.277-2.561a.187.187 0 00-.22-.263zm-10.22-3.964l1.195-2.3.035-.063a158.215 158.215 0 0120.107-29.3l.216-.25a.257.257 0 00.03-.28.252.252 0 00-.252-.128l-.327.047a161.577 161.577 0 00-15.38 2.893c-.509.14-.836.471-.906.547a172.233 172.233 0 00-10.738 14.274 1.26 1.26 0 00-.18.845c.034.262.833 6.403 2.046 11.119-3.01 8.645-5.699 20.037-5.699 20.037a.256.256 0 00.035.216.239.239 0 00.2.099h1.714c.11 0 .204-.065.245-.163l.116-.32a159.515 159.515 0 015.95-14.054c.507-1.065 1.592-3.219 1.592-3.219z" />
<path
d="M751.241 16.942l-.006-.326a.254.254 0 00-.164-.227.257.257 0 00-.274.07l-.216.244a155.428 155.428 0 00-22.69 34.58l-.14.291a.247.247 0 00.04.28.253.253 0 00.275.058l.297-.123a149.858 149.858 0 0115.487-5.431c.31-.087.572-.309.718-.594 2.27-4.413 7.536-12.959 7.536-12.959.134-.192.1-.477.1-.477s-.905-10.217-.963-15.386z" />
<path
d="M762.983 2.201c-1.29-1.117-3.162-1.641-6.067-1.705-2.636-.058-5.769.512-9.32 1.676a3.62 3.62 0 00-1.337.85A172.051 172.051 0 00735.82 13.7l-.258.28a.247.247 0 00-.029.285c.053.093.164.14.268.116l.374-.081a160.34 160.34 0 0116.053-2.591 1.233 1.233 0 011.37 1.252 147.38 147.38 0 00.572 15.677l.03.338a.249.249 0 00.186.215c.017.006.035.006.058.012a.266.266 0 00.21-.105l.192-.274a148.818 148.818 0 019.654-12.295c.385-.436.485-.71.554-1.106 1.074-6.858-.583-11.934-2.07-13.22z" />
</svg>`),
).then((img) => {
const canvas = createCanvas(img.width, img.height),
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
writeFileSync("image.png", canvas.toBuffer());
console.log(img.width, img.height);
});
My guess is that it uses the viewBox to determine the width/height of the SVG. In this case being 64x64. This example also segfaults on v3, backtrace if needed:
|
@chearon Should I open a new issue regarding this? I'm unable to re-open this one. |
Sorry to hear that. Reopening until I can take a closer look. |
Librsvg says directly that
I am surprised to hear about the segfaulting though. |
Odd but alright. Just the segfaulting is the issue then. |
When loading an SVG from a buffer on v3.1.0, i get a segmentation fault. This works properly on v2.x. I am running this on Ubuntu 22.04 with Node 20 managed by NVM.
Reproduction steps:
Backtrace:
The text was updated successfully, but these errors were encountered: