Skip to content

Segmentation fault when loading an SVG buffer on v3. #2486

Closed

Description

@itzTheMeow

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 ?? ()

Activity

  1. chearon commented on Feb 7, 2025

    @chearon
    Collaborator

    Thanks, this probably happened in the NAPI change then. I'll fix it this weekend and add some tests for SVG.

  2. itzTheMeow commented on Feb 16, 2025

    @itzTheMeow
    Author

    Any update on this?

  3. chearon commented on Feb 23, 2025

    @chearon
    Collaborator

    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 width and height. This does work:

    // 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 g_object_unref is 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.

  4. itzTheMeow commented on Feb 27, 2025

    @itzTheMeow
    Author

    My SVGs specify a viewBox, so they work properly. I thought I ran my test case on both versions- maybe I didn't.

    // 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!

  5. itzTheMeow commented on Feb 27, 2025

    @itzTheMeow
    Author

    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:
    Image
    Removing the width/height and keeping the viewBox results in a segfault.

    Run using v2.11.2:
    Image
    While removing the width/height produces the correct image still:
    Image

    Logging the width/height of the image is correct.
    Image

    $ node test.js
    10 10
    

    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);
    });
    $ node test.js
    64 64
    

    Resulting image:
    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:

    gdb node
    GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1
    Copyright (C) 2022 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    Type "show copying" and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <https://www.gnu.org/software/gdb/bugs/>.
    Find the GDB manual and other documentation resources online at:
        <http://www.gnu.org/software/gdb/documentation/>.
    
    For help, type "help".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from node...
    (gdb) run test.js
    Starting program: /home/meow/.nvm/versions/node/v20.18.2/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 43888)]
    [New Thread 0x7ffff6c00640 (LWP 43889)]
    [New Thread 0x7ffff6200640 (LWP 43890)]
    [New Thread 0x7ffff5800640 (LWP 43891)]
    [New Thread 0x7ffff4e00640 (LWP 43892)]
    [New Thread 0x7ffff7fba640 (LWP 43893)]
    
    Thread 1 "node" received signal SIGSEGV, Segmentation fault.
    0x00007ffff6dd685d in g_type_check_instance_is_fundamentally_a () from /home/meow/Documents/testcase/node_modules/.pnpm/canvas@https+++codeload.github.com+Automattic+node-canvas+tar.gz+9d5f104cb52644f878eb3b_4ef2929f0daa660aa2ffa12f649e4566/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/testcase/node_modules/.pnpm/canvas@https+++codeload.github.com+Automattic+node-canvas+tar.gz+9d5f104cb52644f878eb3b_4ef2929f0daa660aa2ffa12f649e4566/node_modules/canvas/build/Release/libgobject-2.0.so.0
    #1  0x00007ffff6db52d5 in g_object_unref ()
       from /home/meow/Documents/testcase/node_modules/.pnpm/canvas@https+++codeload.github.com+Automattic+node-canvas+tar.gz+9d5f104cb52644f878eb3b_4ef2929f0daa660aa2ffa12f649e4566/node_modules/canvas/build/Release/libgobject-2.0.so.0
    #2  0x00007ffff7b4be5f in Image::loadSVGFromBuffer(unsigned char*, unsigned int) ()
       from /home/meow/Documents/testcase/node_modules/.pnpm/canvas@https+++codeload.github.com+Automattic+node-canvas+tar.gz+9d5f104cb52644f878eb3b_4ef2929f0daa660aa2ffa12f649e4566/node_modules/canvas/build/Release/canvas.node
    #3  0x00007ffff7b4d332 in Image::loadFromBuffer(unsigned char*, unsigned int) ()
       from /home/meow/Documents/testcase/node_modules/.pnpm/canvas@https+++codeload.github.com+Automattic+node-canvas+tar.gz+9d5f104cb52644f878eb3b_4ef2929f0daa660aa2ffa12f649e4566/node_modules/canvas/build/Release/canvas.node
    #4  0x00007ffff7b4db80 in Image::SetSource(Napi::CallbackInfo const&) ()
       from /home/meow/Documents/testcase/node_modules/.pnpm/canvas@https+++codeload.github.com+Automattic+node-canvas+tar.gz+9d5f104cb52644f878eb3b_4ef2929f0daa660aa2ffa12f649e4566/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/testcase/node_modules/.pnpm/canvas@https+++codeload.github.com+Automattic+node-canvas+tar.gz+9d5f104cb52644f878eb3b_4ef2929f0daa660aa2ffa12f649e4566/node_modules/canvas/build/Release/canvas.node
    #6  0x0000000000c5c2c5 in v8impl::(anonymous namespace)::FunctionCallbackWrapper::Invoke(v8::FunctionCallbackInfo<v8::Value> const&) ()
    #7  0x0000000000f6e70f in v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) ()
    #8  0x0000000000f6ef7d 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  0x0000000000f6f445 in v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) ()
    #10 0x0000000001979df6 in Builtins_CEntry_Return1_ArgvOnStack_BuiltinExit ()
    #11 0x00000000018ebd1c in Builtins_InterpreterEntryTrampoline ()
    #12 0x00000c4a6c9404e9 in ?? ()
    
  6. itzTheMeow commented on Mar 3, 2025

    @itzTheMeow
    Author

    @chearon Should I open a new issue regarding this? I'm unable to re-open this one.

  7. chearon commented on Mar 3, 2025

    @chearon
    Collaborator

    Sorry to hear that. Reopening until I can take a closer look.

  8. reopened this on Mar 3, 2025
  9. chearon commented on Mar 3, 2025

    @chearon
    Collaborator

    Librsvg says directly that viewBox is not enough, so we can't support that:

    The dimensions of the following documents cannot be resolved to pixels directly, and this function would return FALSE for them:

    <!-- Needs a viewport against which to compute the percentages. -->
    <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%"/>
    
    <!-- Does not have intrinsic width/height, just a 1:2 aspect ratio which
        needs to be fitted within a viewport. -->
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 200"/>

    I am surprised to hear about the segfaulting though.

  10. itzTheMeow commented on Mar 5, 2025

    @itzTheMeow
    Author

    Odd but alright. Just the segfaulting is the issue then.

  11. chearon commented on Apr 6, 2025

    @chearon
    Collaborator

    @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.

  12. shijiatongxue commented on Apr 15, 2025

    @shijiatongxue

    Hi @chearon, I also encountered this problem when loading svg without width.

    <svg height="12" viewBox="0 0 0 12" fill="none" xmlns="http://www.w3.org/2000/svg">
    <path d="M5.24524e-07 0L0 12" stroke="#C6C9D2" stroke-linecap="round"/>
    </svg>
    
    Image Image Image

    When I repeat this multiple times, the node service will report a segmentation fault.

    Image

    here is minimal reproducible code in nodejs.

    macOS 14.6.1 (apple m2 pro)

    import { loadImage } from 'canvas'; // canvas@2.11.2
    
    function run() {
      const imageBase64 =
        'data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjEyIiB2aWV3Qm94PSIwIDAgMCAxMiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTUuMjQ1MjRlLTA3IDBMMCAxMiIgc3Ryb2tlPSIjQzZDOUQyIiBzdHJva2UtbGluZWNhcD0icm91bmQiLz4KPC9zdmc+Cg==';
    
      loadImage(imageBase64)
        .then((data) => {
          console.log('ok', data);
        })
        .catch((e) => {
          console.log('e', e);
        });
    }
    
    run();
    

    Here is the crash log.

    PID 22696 received SIGSEGV for address: 0x4345
    0   segfault-handler.node               0x000000010a39d184 _ZL16segfault_handleriP9__siginfoPv + 296
    1   libsystem_platform.dylib            0x000000018d6ae584 _sigtramp + 56
    2   libgobject-2.0.0.dylib              0x0000000112f4e4f0 g_object_unref + 36
    3   canvas.node                         0x0000000112851410 _ZN5Image9clearDataEv + 96
    4   canvas.node                         0x0000000112852764 _ZN5ImageD2Ev + 32
    5   canvas.node                         0x0000000112852820 _ZN5ImageD0Ev + 12
    6   node                                0x0000000105313bb0 _ZN2v88internal13GlobalHandles28InvokeFirstPassWeakCallbacksINS1_4NodeEEEmPNSt3__16vectorINS4_4pairIPT_NS1_22PendingPhantomCallbackEEENS4_9allocatorISA_EEEE + 152
    7   node                                0x0000000105313af8 _ZN2v88internal13GlobalHandles28InvokeFirstPassWeakCallbacksEv + 24
    8   node                                0x000000010535df9c _ZN2v88internal4Heap24PerformGarbageCollectionENS0_16GarbageCollectorENS0_23GarbageCollectionReasonEPKcNS_15GCCallbackFlagsE + 1624
    9   node                                0x000000010535b394 _ZN2v88internal4Heap14CollectGarbageENS0_15AllocationSpaceENS0_23GarbageCollectionReasonENS_15GCCallbackFlagsE + 1472
    10  node                                0x00000001053643e0 _ZN2v88internal4Heap36FinalizeIncrementalMarkingIfCompleteENS0_23GarbageCollectionReasonE + 216
    11  node                                0x0000000105373738 _ZN2v88internal21IncrementalMarkingJob4Task11RunInternalEv + 364
    12  node                                0x000000010508a120 _ZN4node22PerIsolatePlatformData17RunForegroundTaskENSt3__110unique_ptrIN2v84TaskENS1_14default_deleteIS4_EEEE + 216
    13  node                                0x0000000105089000 _ZN4node22PerIsolatePlatformData28FlushForegroundTasksInternalEv + 972
    14  node                                0x0000000105995910 uv__async_io + 320
    15  node                                0x00000001059a8a54 uv__io_poll + 1044
    16  node                                0x0000000105996298 uv_run + 1588
    17  node                                0x0000000104f6d7ec _ZN4node13SpinEventLoopEPNS_11EnvironmentE + 248
    18  node                                0x0000000105066358 _ZN4node16NodeMainInstance3RunEv + 196
    19  node                                0x0000000104fed528 _ZN4node22LoadSnapshotDataAndRunEPPKNS_12SnapshotDataEPKNS_20InitializationResultE + 436
    20  node                                0x0000000104fed748 _ZN4node5StartEiPPc + 460
    21  dyld                                0x000000018d2f3154 start + 2476
    
  13. chearon commented on Apr 26, 2025

    @chearon
    Collaborator

    @shijiatongxue was that using 3.1.0? I haven't released 3.1.1 yet, sorry.

  14. shijiatongxue commented on May 6, 2025

    @shijiatongxue

    @shijiatongxue was that using 3.1.0? I haven't released 3.1.1 yet, sorry.

    No. node-canvas version is 2.11.2

  15. peterkmurphy commented on May 14, 2025

    @peterkmurphy

    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 canvas library 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.

  16. chearon commented on Jun 19, 2025

    @chearon
    Collaborator

    @peterkmurphy 3.1.1 is published now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions