Subject: bug#73870: 30.0.91; Segmentation fault when enabling php-ts-mode in an empty buffer

From: Eli Zaretskii <[email-masked]>
Date: Sat, 19 Oct 2024 09:31:04 +0300

> Cc: [email-masked]
> From: Morgan Willcock <[email-masked]>
> Date: Fri, 18 Oct 2024 21:55:50 +0100
> 
> 
> 
> I have a reproducible segfault when enabling php-ts-mode (grammars have
> been installed with php-ts-mode-install-parsers):
> 
>   emacs -Q \
>         --eval "(find-file \"/tmp/newfile\")" \
>         --eval "(php-ts-mode)"
> 
> The error doesn't occur when the buffer is not empty, so this doesn't
> crash:
> 
>   emacs -Q \
>         --eval "(find-file \"/tmp/newfile\")" \
>         --eval "(insert \"a\")" \
>         --eval "(php-ts-mode)"
> 
> Here is a backtrace:
> 
>   (gdb) run
>   Starting program: /home/mwillcock/build/emacs-30.0.91/src/emacs -Q --eval \(find-file\ \"/tmp/newfile\"\) --eval \(php-ts-mode\)
>   [Thread debugging using libthread_db enabled]
>   Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
>   [New Thread 0x7ffff1a476c0 (LWP 70615)]
>   [New Thread 0x7ffff12466c0 (LWP 70616)]
>   [New Thread 0x7ffff0a456c0 (LWP 70617)]
> 
>   Thread 1 "emacs" received signal SIGSEGV, Segmentation fault.
>   0x00005555558ee950 in treesit_sync_visible_region (parser=...) at treesit.c:1087
>   1087	      new_ranges[new_len - 1].end_byte = new_end;
>   (gdb) bt full
>   #0  0x00005555558ee950 in treesit_sync_visible_region (parser=...) at treesit.c:1087
>           new_ranges = 0x555556b02e10
>           tree = 0x555556b03ab0
>           buffer = 0x555556109100
>           visible_beg = 1
>           visible_end = 1
>           len = 1
>           ranges = 0x5555568d73a0
>           new_len = 0
>           new_end = 0
>   #1  0x00005555558eed56 in treesit_ensure_parsed (parser=...) at treesit.c:1156
>           buffer = 0x555556109100
>           treesit_parser = 0x0
>           tree = 0x5555558edc7b <treesit_check_parser+36>
>           input = {payload = 0x7fffffffa420, read = 0x5555558ec9b7 <XTS_PARSER+35>, encoding = TSInputEncodingUTF8}
>           new_tree = 0x7fffffffa440

The implementation of that function was significantly modified on the
emacs-30 release branch, so the code which segfaults for you is no
longer there.  Can you try the current emacs-30 branch and see if the
problem still happens there?  If it does, please show a backtrace from
that version.

Thanks.

P.S. Adding Yuan, in case he has some comments.