sammycage / lunasvg

SVG rendering and manipulation library in C++
MIT License
866 stars 124 forks source link

Specific image consistently crashes LunaSVG #100

Closed leonstyhre closed 1 year ago

leonstyhre commented 2 years ago

Hi!

Thanks for this incredible library, I'm currently in the process of evaluating whether to switch from NanoSVG to LunaSVG for my application (es-de.org) but I have run into one showstopper bug. The issue is that at least one image makes the library crash consistently (it's rendered correctly by NanoSVG). I'm providing a link to a gzip'ed version of the image. Could you please look into this? Many thanks!

https://es-de.org/temp/crashes_LunaSVG.svg.gz

sammycage commented 2 years ago

@leonstyhre

No crashes (rendered as expected)

Make sure you are using the latest version@ https://codeload.github.com/sammycage/lunasvg/zip/refs/heads/master

Send me your crash log if this problem persists

Thanks

crashes_LunaSVG svg

leonstyhre commented 2 years ago

Thanks for your quick reply! I tested with v2.3.2 as when I tried the latest commit then nothing is rendered at all, all files are showing up as blank. The images are still rasterized and end up with the correct dimensions and such, but the texture is completely blank.

I found that by reverting the commit 592533914e56bd542585c95ee8c9dabce9e76c9f then everything renders correctly at the latest commit (d13d8e521c21f5f750ef0f6f92163f0131afdd3e) but the crash still occurs with the image posted above.

The crash happens at seemingly random locations but always when calling renderToBitmap()

Some examples of where the crashes occur: plutovg-blend.c at line 800 plutovg-paint.c at line 347 plutovg-rle.c at line 14 plutovg-rle.c at line 29

Due to the random nature of the crashes, I guess it's some kind of overflow issue or other memory problem? Maybe even related to the commit that I had to roll back to get anything to render?

I'm on Ubuntu 22.04 and compiling with GCC if that's somehow helpful to know. Many thanks in advance!

leonstyhre commented 2 years ago

ASan provided some more useful information, for some reason it's not possible to attach files here directly, but here's an URL to the relevant output: https://es-de.org/temp/LunaSVG_crash.txt

It seems as if the crash occurs in the sw_ft_outline_cubic_to function. So this was with the latest commit as described in the previous comment, but with the problematic patch rolled back as that causes no textures to being rendered. Let me know if I should test something specific in addition to this!

sammycage commented 2 years ago

Let me know if I should test something specific in addition to this!

Please Run this

document->renderToBitmap(128, 128);
leonstyhre commented 2 years ago

I have identified the issue now but it's very bizarre. I build my application with the rlottie library (https://github.com/Samsung/rlottie) and it seems like this library shares or uses the same names for some functions as compared to your library and there is somehow a conflict introduced via this.

I have added the libraries like this in the CMakeLists.txt file:

if(NOT WIN32)
    # Lottie animation library rlottie.
    set(COMMON_LIBRARIES ${COMMON_LIBRARIES} ${PROJECT_SOURCE_DIR}/librlottie.a)
    # SVG rendering library LunaSVG.
    set(COMMON_LIBRARIES ${COMMON_LIBRARIES} ${PROJECT_SOURCE_DIR}/liblunasvg.a)
endif()

If I do this, then the issue occurs where no textures are rendered, but Lottie animations work correctly. If I however change the order to this:

if(NOT WIN32)
    # SVG rendering library LunaSVG.
    set(COMMON_LIBRARIES ${COMMON_LIBRARIES} ${PROJECT_SOURCE_DIR}/liblunasvg.a)
    # Lottie animation library rlottie.
    set(COMMON_LIBRARIES ${COMMON_LIBRARIES} ${PROJECT_SOURCE_DIR}/librlottie.a)
endif()

Then your library works, I can use the latest commit and the image I reported as crashing the application also renders correctly. However Lottie animations no longer work.

I have never encountered something like this before, there is some kind of namespace conflict or similar between LunaSVG and rlottie. Do you have any idea how this could be resolved?

sammycage commented 2 years ago

similar between LunaSVG

Freetype rasterizer

Do you have any idea how this could be resolved?

  1. Search and Replace SW_ with PVG_
  2. Search and Replace sw_ with pvg_

I will find a better way to fix this ;)

leonstyhre commented 2 years ago

Wow, that actually worked!

Find the patch here: http://es-de.org/temp/LunaSVG_namespace.patch

There were some strange linebreaks and such introduced so you may need to clean it up a bit, but it actually works, I can now use both Lottie animations and SVG images at the same time.

Could you apply this to your repository so I don't need to use a custom patch in my subtree?

leonstyhre commented 2 years ago

This issue is still not solved? I would prefer not to have to maintain a custom patch to get rlottie and LunaSVG to work in parallel, would you be able to apply the patch I supplied or add some other mechanism to fix this more permanently? Thanks :)

leonstyhre commented 2 years ago

Hi, any update on this issue? I've now integrated LunaSVG quite well with my application on a separate branch, but this collision with rlottie prevents me from merging it into the master branch as it's a blocker issue. Thanks in advance!

leonstyhre commented 1 year ago

I've just tested this on Linux, macOS and Windows and I can confirm that there is no longer any conflict and that rlottie and LunaSVG can now be used at the same time without any issues!