Subject: 29.0.50; Emacs crashes when turning treesit-explore-mode on with some C++ code

From: Daniel Martín <[email-masked]>
Date: Wed, 30 Nov 2022 00:06:03 +0100

Steps to reproduce the problem:

emacs -Q
C-x b sample.cpp RET

Paste the following code:

class OFFICE_EXPORT Sample : public Base, public WeakObservable<SampleObserver> {
public:    
   void getFoo();
private:
   mutable std::weak_ptr<Foo> m_Foo;
};

M-x c++-ts-mode RET
M-x treesit-explore-mode RET
M-<

Emacs crashes with the following backtrace:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x100100005530847)
  * frame #0: 0x0000000101365fc8 libtree-sitter.0.dylib`ts_node_field_name_for_child + 24
    frame #1: 0x00000001003b0a8c emacs`Ftreesit_node_field_name_for_child(node=0x00000001059f7575, n=(EMACS_INT) $329 = 0) at treesit.c:1868:7
    frame #2: 0x00000001002c2cd4 emacs`funcall_subr(subr=0x00000001005028c8, numargs=2, args=(struct Lisp_Symbol *) $332 = 0x0000000100b32ec0) at eval.c:3036:15
    frame #3: 0x000000010033bde4 emacs`exec_byte_code(fun=(struct Lisp_Vector *) $337 = 0x0000000103872e98, args_template=513, nargs=1, args=(struct Lisp_Symbol *) $340 = 0x0000000100b32ec0) at bytecode.c:809:14
    frame #4: 0x00000001002c8054 emacs`fetch_and_exec_byte_code(fun=(struct Lisp_Vector *) $345 = 0x0000000135009550, args_template=0, nargs=0, args=(struct Lisp_Symbol *) $348 = 0x0000000100b32ec0) at eval.c:3081:10
    frame #5: 0x00000001002c3300 emacs`funcall_lambda(fun=(struct Lisp_Vector *) $353 = 0x0000000135009550, nargs=0, arg_vector=(struct Lisp_Symbol *) $356 = 0x0000000100b32ec0) at eval.c:3153:9
    frame #6: 0x00000001002c2914 emacs`funcall_general(fun=(struct Lisp_Vector *) $361 = 0x0000000135009550, numargs=0, args=(struct Lisp_Symbol *) $364 = 0x0000000100b32ec0) at eval.c:2945:12
    frame #7: 0x00000001002bc8e0 emacs`Ffuncall(nargs=1, args=(struct Lisp_Symbol *) $367 = 0x0000000100b32ec0) at eval.c:2995:21
    frame #8: 0x00000001002c1224 emacs`Fapply(nargs=2, args=(struct Lisp_Symbol *) $370 = 0x0000000100b32ec0) at eval.c:2619:12
    frame #9: 0x00000001002c2e9c emacs`funcall_subr(subr=0x00000001004fca48, numargs=2, args=(struct Lisp_Symbol *) $373 = 0x0000000100b32ec0) at eval.c:3059:9
    frame #10: 0x000000010033bde4 emacs`exec_byte_code(fun=(struct Lisp_Vector *) $378 = 0x00000001044e6b58, args_template=257, nargs=1, args=(struct Lisp_Symbol *) $381 = 0x0000000100b32ec0) at bytecode.c:809:14
    frame #11: 0x00000001002c8054 emacs`fetch_and_exec_byte_code(fun=(struct Lisp_Vector *) $386 = 0x0000000104ae6e70, args_template=257, nargs=1, args=(struct Lisp_Symbol *) $389 = 0x0000000100b32ec0) at eval.c:3081:10
    frame #12: 0x00000001002c3300 emacs`funcall_lambda(fun=(struct Lisp_Vector *) $394 = 0x0000000104ae6e70, nargs=1, arg_vector=(struct Lisp_Symbol *) $397 = 0x0000000100b32ec0) at eval.c:3153:9
    frame #13: 0x00000001002c2914 emacs`funcall_general(fun=(struct Lisp_Vector *) $402 = 0x0000000104ae6e70, numargs=1, args=(struct Lisp_Symbol *) $405 = 0x0000000100b32ec0) at eval.c:2945:12
    frame #14: 0x00000001002bc8e0 emacs`Ffuncall(nargs=2, args=(struct Lisp_Symbol *) $408 = 0x0000000100b32ec0) at eval.c:2995:21
    frame #15: 0x00000001001a6da0 emacs`call1(fn=(struct Lisp_Symbol *) $411 = 0x0000000100b41f20, arg1=(struct Lisp_Vector *) $415 = 0x000000013500bc20) at lisp.h:3247:10
    frame #16: 0x00000001001b058c emacs`timer_check_2(timers=(struct Lisp_Cons *) $417 = 0x0000000105972560, idle_timers=(struct Lisp_Cons *) $419 = 0x0000000105972570) at keyboard.c:4681:8
    frame #17: 0x00000001001afc58 emacs`timer_check at keyboard.c:4747:18
    frame #18: 0x00000001001ac6b4 emacs`readable_events(flags=1) at keyboard.c:3524:5
    frame #19: 0x00000001001afae4 emacs`get_input_pending(flags=1) at keyboard.c:7367:42
    frame #20: 0x00000001001aac68 emacs`detect_input_pending_run_timers(do_display=true) at keyboard.c:10897:5
    frame #21: 0x0000000100350bec emacs`wait_reading_process_output(time_limit=30, nsecs=0, read_kbd=-1, do_display=true, wait_for_cell=(struct Lisp_Symbol *) $422 = 0x0000000100b32ec0, wait_proc=0x0000000000000000, just_wait_proc=0) at process.c:5779:8
    frame #22: 0x000000010000c54c emacs`sit_for(timeout=(EMACS_INT) $424 = 30, reading=true, display_option=1) at dispnew.c:6252:7
    frame #23: 0x00000001001a8a74 emacs`read_char(commandflag=1, map=(struct Lisp_Cons *) $426 = 0x000000010595beb0, prev_event=(struct Lisp_Symbol *) $429 = 0x0000000100b32ec0, used_mouse_menu=0x000000016fdfe837, end_time=0x0000000000000000) at keyboard.c:2872:11
    frame #24: 0x00000001001a3a6c emacs`read_key_sequence(keybuf=(struct Lisp_Symbol *) $432 = 0x0000000100b32ec0, prompt=(struct Lisp_Symbol *) $435 = 0x0000000100b32ec0, dont_downcase_last=false, can_return_switch_frame=true, fix_current_buffer=true, prevent_redisplay=false) at keyboard.c:10074:12
    frame #25: 0x00000001001a1d84 emacs`command_loop_1 at keyboard.c:1376:15
    frame #26: 0x00000001002bdfd0 emacs`internal_condition_case(bfun=(emacs`command_loop_1 at keyboard.c:1270), handlers=(struct Lisp_Symbol *) $438 = 0x0000000100b32f50, hfun=(emacs`cmd_error at keyboard.c:928)) at eval.c:1474:25
    frame #27: 0x00000001001a1850 emacs`command_loop_2(handlers=(struct Lisp_Symbol *) $441 = 0x0000000100b32f50) at keyboard.c:1125:11
    frame #28: 0x00000001002bd1e8 emacs`internal_catch(tag=(struct Lisp_Symbol *) $444 = 0x0000000100b421f0, func=(emacs`command_loop_2 at keyboard.c:1121), arg=(struct Lisp_Symbol *) $447 = 0x0000000100b32f50) at eval.c:1197:25
    frame #29: 0x00000001001a0844 emacs`command_loop at keyboard.c:1103:2
    frame #30: 0x00000001001a0694 emacs`recursive_edit_1 at keyboard.c:712:9
    frame #31: 0x00000001001a0c68 emacs`Frecursive_edit at keyboard.c:795:3
    frame #32: 0x000000010019d420 emacs`main(argc=2, argv=0x000000016fdff3a0) at emacs.c:2523:3
    frame #33: 0x0000000100d3108c dyld`start + 520

Lisp backtrace:

(unsigned char *) data = 0x00000001004d0dc3 "treesit-node-field-name-for-child"
(unsigned char *) data = 0x0000000103967dc8 "treesit-node-field-name"
(unsigned char *) data = 0x000000010396a700 "treesit--explorer-draw-node"
(unsigned char *) data = 0x000000010396a700 "treesit--explorer-draw-node"
(unsigned char *) data = 0x000000010396a700 "treesit--explorer-draw-node"
(unsigned char *) data = 0x000000010396a700 "treesit--explorer-draw-node"
(unsigned char *) data = 0x000000010396a6b8 "treesit--explorer-refresh"
(unsigned char *) data = 0x00000001004b51f2 "apply"
(unsigned char *) data = 0x00000001004c04f2 "timer-event-handler"