jlaurens/synctex

Stack overflow in _synctex_free_handle

Closed this issue · 1 comments

Hello synctex developers,

Pretty much any version of TexStudio (including the latest HEAD) crashes on Windows in some edge cases when trying to synchronize from tex source to compiled PDF. After some investigation it seems that the crash happens due to stack overflow when synctex tries to free some large tree (or maybe list) structure. I am attaching a debug backtrace from the crashed Windows version of the latest HEAD version of TeXstudio.

crash_backtrace.txt

I am also attaching the .tex file which causes the crash and all the other output files from TeXstudio including the .synctex.gz file.
(I had to zip them into a single file, since github does not allow me to upload them otherwise)
texstudio_crash_files.zip

You can cause the crash by compiling barcode_test_crash.tex two or three times, then editing line 25 which says
(-2cm,0) rectangle (\textwidth,4cm); and changing 4cm to 5cm and compiling it 2-3 times, then chaning it to 8cm and again compiling 2-3 times. At some point on Windows it will crash when it tries to free a node.

On Windows the executable stack size is set to 0x200000 (2MB) while on Linux the default ulimit is 8MB, so TeXstudio is able to get away with the deep recursion and it just shows a noticeable delay when trying to sync from source to PDF.

We are discussing this crash in the following TeXstudio issue here :

texstudio-org/texstudio#1127

Can anyone from the synctex team please comment on the issue and let us know if:

  1. If this deep recursion is normal for synctex or if there is some bug in the synctex code (or maybe in the TeXstudio code) that causes synctex to go into such a deep recursion?

  2. TeXstudio uses a relatively old version of the synctex codebase (I think from 2017). Will upgrading to a newer version fix or maybe alleviate the problem?

  3. Is there a way to improve the synctex code that frees the tree structure, so that it does not recurse so deeply and does not use so much stack? I am not well familiar with the synctex codebase so I cannot really offer code, but maybe synctex could use an iterative algorithm when freeing the tree and use as a stack an array allocated on the heap?

Many thanks for the fix. The new code solves the stack overflow issue.