]> git.eshelyaron.com Git - emacs.git/commitdiff
Handle ts_node_type return NULL (bug#78938)
authorYuan Fu <casouri@gmail.com>
Fri, 4 Jul 2025 20:13:36 +0000 (13:13 -0700)
committerEshel Yaron <me@eshelyaron.com>
Wed, 23 Jul 2025 20:19:00 +0000 (22:19 +0200)
* src/treesit.c (treesit_traverse_match_predicate): Handle the
case when ts_node_type returns NULL.
(Ftreesit_node_type): Add some comment.

(cherry picked from commit 060f964906b64fa8a63a120af9e2380d11d30604)

src/treesit.c

index 8b066b09ef74a2f98ad287e9bfcdee7d2cba0c14..d24821f385bee7d7d20034e5e9fd2e379cfcfc1a 100644 (file)
@@ -2854,7 +2854,9 @@ If NODE is nil, return nil.  */)
   treesit_initialize ();
 
   TSNode treesit_node = XTS_NODE (node)->node;
-  /* ts_node_type could return NULL, see source code.  */
+  /* ts_node_type could return NULL, see source code (tree-sitter can't
+     find the string name of a node type by its id in its node name
+     obarray).  */
   const char *type = ts_node_type (treesit_node);
   return type == NULL ? Vtreesit_str_empty : build_string (type);
 }
@@ -4446,6 +4448,10 @@ treesit_traverse_match_predicate (TSTreeCursor *cursor, Lisp_Object pred,
   if (STRINGP (pred))
     {
       const char *type = ts_node_type (node);
+      /* ts_node_type returning NULL means something unexpected happend
+         in tree-sitter, in this case the only reasonable thing is to
+         not match anything.  */
+      if (type == NULL) return false;
       return fast_c_string_match (pred, type, strlen (type)) >= 0;
     }
   else if (FUNCTIONP (pred)
@@ -4498,6 +4504,10 @@ treesit_traverse_match_predicate (TSTreeCursor *cursor, Lisp_Object pred,
        {
          /* A bit of code duplication here, but should be fine.  */
          const char *type = ts_node_type (node);
+         /* ts_node_type returning NULL means something unexpected
+             happend in tree-sitter, in this case the only reasonable
+             thing is to not match anything  */
+         if (type == NULL) return false;
          if (!(fast_c_string_match (car, type, strlen (type)) >= 0))
            return false;