Repository navigation
Closed
Description
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:
// test.js
const { loadImage } = require("canvas");
loadImage(Buffer.from(`<svg xmlns="http://www.w3.org/2000/svg"><path d="M1,1"/></svg>`));$ node test.js
Segmentation fault (core dumped)Backtrace:
$ gdb node
(gdb) run test.js
Starting program: /home/meow/.nvm/versions/node/v20.9.0/bin/node test.js
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7600640 (LWP 966381)]
[New Thread 0x7ffff6c00640 (LWP 966383)]
[New Thread 0x7ffff6200640 (LWP 966384)]
[New Thread 0x7ffff5800640 (LWP 966385)]
[New Thread 0x7ffff4e00640 (LWP 966386)]
[New Thread 0x7ffff7e66640 (LWP 966388)]
Thread 1 "node" received signal SIGSEGV, Segmentation fault.
0x00007ffff6dd685d in g_type_check_instance_is_fundamentally_a () from /home/meow/Documents/test/node_modules/.pnpm/canvas@3.1.0/node_modules/canvas/build/Release/libgobject-2.0.so.0
(gdb) bt
#0 0x00007ffff6dd685d in g_type_check_instance_is_fundamentally_a ()
from /home/meow/Documents/test/node_modules/.pnpm/canvas@3.1.0/node_modules/canvas/build/Release/libgobject-2.0.so.0
#1 0x00007ffff6db52d5 in g_object_unref ()
from /home/meow/Documents/test/node_modules/.pnpm/canvas@3.1.0/node_modules/canvas/build/Release/libgobject-2.0.so.0
#2 0x00007ffff7b4be5f in Image::loadSVGFromBuffer(unsigned char*, unsigned int) ()
from /home/meow/Documents/test/node_modules/.pnpm/canvas@3.1.0/node_modules/canvas/build/Release/canvas.node
#3 0x00007ffff7b4d332 in Image::loadFromBuffer(unsigned char*, unsigned int) ()
from /home/meow/Documents/test/node_modules/.pnpm/canvas@3.1.0/node_modules/canvas/build/Release/canvas.node
#4 0x00007ffff7b4db80 in Image::SetSource(Napi::CallbackInfo const&) ()
from /home/meow/Documents/test/node_modules/.pnpm/canvas@3.1.0/node_modules/canvas/build/Release/canvas.node
#5 0x00007ffff7b4e748 in Napi::details::CallbackData<void (*)(Napi::CallbackInfo const&), void>::Wrapper(napi_env__*, napi_callback_info__*) ()
from /home/meow/Documents/test/node_modules/.pnpm/canvas@3.1.0/node_modules/canvas/build/Release/canvas.node
#6 0x0000000000c37df9 in v8impl::(anonymous namespace)::FunctionCallbackWrapper::Invoke(v8::FunctionCallbackInfo<v8::Value> const&) ()
#7 0x0000000000f26ecf in v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) ()
#8 0x0000000000f2773d in v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, unsigned long*, int) ()
#9 0x0000000000f27c05 in v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) ()
#10 0x0000000001931df6 in Builtins_CEntry_Return1_ArgvOnStack_BuiltinExit ()
#11 0x00000000018a3d1c in Builtins_InterpreterEntryTrampoline ()
#12 0x00000273029c04e9 in ?? ()
#13 0x0000389ecc9e2911 in ?? ()
#14 0x0000000600000000 in ?? ()
#15 0x00000273029c05b9 in ?? ()
#16 0x00002d6278e8cf69 in ?? ()
#17 0x00002d6278e8cd39 in ?? ()
#18 0x0000389ecc9e2911 in ?? ()
#19 0x000029a60631f5a9 in ?? ()
#20 0x0000003f00000000 in ?? ()
#21 0x000006684f2dd069 in ?? ()
#22 0x0000000000000003 in ?? ()
#23 0x0000377cdb30d999 in ?? ()
#24 0x0000377cdb30d919 in ?? ()
Metadata
Metadata
Assignees
Labels
No labels
Activity
chearon commentedon Feb 7, 2025
Thanks, this probably happened in the NAPI change then. I'll fix it this weekend and add some tests for SVG.
itzTheMeow commentedon Feb 16, 2025
Any update on this?
chearon commentedon Feb 23, 2025
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
widthandheight. This does work:Of course, it shouldn't be crashing. There is a problem in the error path where
g_object_unrefis called twice. I don't know why you see a crash in 3.1.0 but not in 2.11.2 because I got the opposite! It's probably undefined behavior that gets us different results because of different versions of glib.Even without the crash, the error is totally unhelpful, so I'll add some friendlier messaging.
add #2486 to changelog
double-free via g_object_unref that could crash
itzTheMeow commentedon Feb 27, 2025
My SVGs specify a
viewBox, so they work properly. I thought I ran my test case on both versions- maybe I didn't.This works properly on 2.11.2
What node version are you considering modern? Works fine on v20.18.2
Thanks for the fix!
itzTheMeow commentedon Feb 27, 2025
Just tested this commit and still having issues.
My previous test case works on 2.11.2, but not on 3.
Run using latest commit:

Removing the width/height and keeping the viewBox results in a segfault.
Run using v2.11.2:


While removing the width/height produces the correct image still:
Logging the width/height of the image is correct.

Another example with a more complicated SVG:
Resulting image:

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:
itzTheMeow commentedon Mar 3, 2025
@chearon Should I open a new issue regarding this? I'm unable to re-open this one.
chearon commentedon Mar 3, 2025
Sorry to hear that. Reopening until I can take a closer look.
chearon commentedon Mar 3, 2025
Librsvg says directly that
viewBoxis not enough, so we can't support that:I am surprised to hear about the segfaulting though.
itzTheMeow commentedon Mar 5, 2025
Odd but alright. Just the segfaulting is the issue then.
chearon commentedon Apr 6, 2025
@itzTheMeow You were right about 3.x not supporting viewBox-only and I figured out why. 3.0 includes the commit f3184ba which changed the librsvg APIs we use. The old one did read the viewBox and the new API doesn't (old:
rsvg_handle_render_cairo, new:rsvg_handle_render_document). You could open a new issue for that if you want, but idk if it's possible with non-deprecated APIs.However, I don't think there's still a crashing bug. I tried your example and it correctly threw the error instead of crashing. Your stack trace is not possible in the new code. Are you sure you got it to rebuild correctly?
Going to close this proactively but I'll reopen it again if you can find a repro.
shijiatongxue commentedon Apr 15, 2025
Hi @chearon, I also encountered this problem when loading svg without width.
When I repeat this multiple times, the node service will report a segmentation fault.
here is minimal reproducible code in nodejs.
Here is the crash log.
Add explicit dimensions to SVGs.
Add explicit dimensions to SVGs. (#5293)
chearon commentedon Apr 26, 2025
@shijiatongxue was that using 3.1.0? I haven't released 3.1.1 yet, sorry.
shijiatongxue commentedon May 6, 2025
No. node-canvas version is 2.11.2
peterkmurphy commentedon May 14, 2025
Good day, @chearon. When would version 3.1.1 of this library come out? One of the projects I'm working on is migrating from Node 18 to Node 22, which means the
canvaslibrary used inside must migrate from 2 to 3. Unfortunately, it's been bitten by this bug.A timeline (or a workaround) for this would be appreciated, thank you.
chearon commentedon Jun 19, 2025
@peterkmurphy 3.1.1 is published now