diff options
| author | Yuan Fu | 2025-07-04 13:13:36 -0700 |
|---|---|---|
| committer | Yuan Fu | 2025-07-04 13:22:14 -0700 |
| commit | 060f964906b64fa8a63a120af9e2380d11d30604 (patch) | |
| tree | 791813da07477983c96ab7a668e16c0c1e115d4c /src | |
| parent | 5bdacbe460a3dbc1777b8d06767c9c323c4fee23 (diff) | |
| download | emacs-060f964906b64fa8a63a120af9e2380d11d30604.tar.gz emacs-060f964906b64fa8a63a120af9e2380d11d30604.zip | |
Handle ts_node_type return NULL (bug#78938)
* src/treesit.c (treesit_traverse_match_predicate): Handle the
case when ts_node_type returns NULL.
(Ftreesit_node_type): Add some comment.
Diffstat (limited to 'src')
| -rw-r--r-- | src/treesit.c | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/treesit.c b/src/treesit.c index 7393404f7ea..e2986c186b8 100644 --- a/src/treesit.c +++ b/src/treesit.c | |||
| @@ -2043,7 +2043,9 @@ If NODE is nil, return nil. */) | |||
| 2043 | treesit_initialize (); | 2043 | treesit_initialize (); |
| 2044 | 2044 | ||
| 2045 | TSNode treesit_node = XTS_NODE (node)->node; | 2045 | TSNode treesit_node = XTS_NODE (node)->node; |
| 2046 | /* ts_node_type could return NULL, see source code. */ | 2046 | /* ts_node_type could return NULL, see source code (tree-sitter can't |
| 2047 | find the string name of a node type by its id in its node name | ||
| 2048 | obarray). */ | ||
| 2047 | const char *type = ts_node_type (treesit_node); | 2049 | const char *type = ts_node_type (treesit_node); |
| 2048 | return type == NULL ? Vtreesit_str_empty : build_string (type); | 2050 | return type == NULL ? Vtreesit_str_empty : build_string (type); |
| 2049 | } | 2051 | } |
| @@ -3595,6 +3597,10 @@ treesit_traverse_match_predicate (TSTreeCursor *cursor, Lisp_Object pred, | |||
| 3595 | if (STRINGP (pred)) | 3597 | if (STRINGP (pred)) |
| 3596 | { | 3598 | { |
| 3597 | const char *type = ts_node_type (node); | 3599 | const char *type = ts_node_type (node); |
| 3600 | /* ts_node_type returning NULL means something unexpected happend | ||
| 3601 | in tree-sitter, in this case the only reasonable thing is to | ||
| 3602 | not match anything. */ | ||
| 3603 | if (type == NULL) return false; | ||
| 3598 | return fast_c_string_match (pred, type, strlen (type)) >= 0; | 3604 | return fast_c_string_match (pred, type, strlen (type)) >= 0; |
| 3599 | } | 3605 | } |
| 3600 | else if (FUNCTIONP (pred)) | 3606 | else if (FUNCTIONP (pred)) |
| @@ -3632,6 +3638,10 @@ treesit_traverse_match_predicate (TSTreeCursor *cursor, Lisp_Object pred, | |||
| 3632 | { | 3638 | { |
| 3633 | /* A bit of code duplication here, but should be fine. */ | 3639 | /* A bit of code duplication here, but should be fine. */ |
| 3634 | const char *type = ts_node_type (node); | 3640 | const char *type = ts_node_type (node); |
| 3641 | /* ts_node_type returning NULL means something unexpected | ||
| 3642 | happend in tree-sitter, in this case the only reasonable | ||
| 3643 | thing is to not match anything */ | ||
| 3644 | if (type == NULL) return false; | ||
| 3635 | if (!(fast_c_string_match (car, type, strlen (type)) >= 0)) | 3645 | if (!(fast_c_string_match (car, type, strlen (type)) >= 0)) |
| 3636 | return false; | 3646 | return false; |
| 3637 | 3647 | ||