From 23261c4f6f35c8790d1eda46b0ea98a1fca110a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tu=E1=BA=A5n-Anh=20Nguy=E1=BB=85n?= Date: Thu, 27 Feb 2020 22:24:00 +0700 Subject: [PATCH] Make ts_language_symbol_name return NULL for out-of-bound ids --- lib/binding_rust/lib.rs | 11 +++++++---- lib/src/language.c | 4 +++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 9da94510..a3f77dff 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -176,10 +176,13 @@ impl Language { } /// Get the name of the node kind for the given numerical id. - pub fn node_kind_for_id(&self, id: u16) -> &'static str { - unsafe { CStr::from_ptr(ffi::ts_language_symbol_name(self.0, id)) } - .to_str() - .unwrap() + pub fn node_kind_for_id(&self, id: u16) -> Option<&'static str> { + let ptr = unsafe { ffi::ts_language_symbol_name(self.0, id) }; + if ptr.is_null() { + None + } else { + Some(unsafe { CStr::from_ptr(ptr) }.to_str().unwrap()) + } } /// Get the numeric id for the given node kind. diff --git a/lib/src/language.c b/lib/src/language.c index 05b189c6..a396b4b0 100644 --- a/lib/src/language.c +++ b/lib/src/language.c @@ -72,8 +72,10 @@ const char *ts_language_symbol_name( return "ERROR"; } else if (symbol == ts_builtin_sym_error_repeat) { return "_ERROR"; - } else { + } else if (symbol < ts_language_symbol_count(self)) { return self->symbol_names[symbol]; + } else { + return NULL; } }