diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index a222dbf7..9da94510 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -212,10 +212,13 @@ impl Language { } /// Get the field names for the given numerical id. - pub fn field_name_for_id(&self, field_id: u16) -> &'static str { - unsafe { CStr::from_ptr(ffi::ts_language_field_name_for_id(self.0, field_id)) } - .to_str() - .unwrap() + pub fn field_name_for_id(&self, field_id: u16) -> Option<&'static str> { + let ptr = unsafe { ffi::ts_language_field_name_for_id(self.0, field_id) }; + if ptr.is_null() { + None + } else { + Some(unsafe { CStr::from_ptr(ptr) }.to_str().unwrap()) + } } /// Get the numerical id for the given field name. diff --git a/lib/src/language.c b/lib/src/language.c index e240ef2a..05b189c6 100644 --- a/lib/src/language.c +++ b/lib/src/language.c @@ -119,7 +119,7 @@ const char *ts_language_field_name_for_id( TSFieldId id ) { uint32_t count = ts_language_field_count(self); - if (count) { + if (count && id <= count) { return self->field_names[id]; } else { return NULL;