From c719e24a45e159526dc9a81e264692942fbf36d4 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 21:09:20 +0700 Subject: [PATCH] Make ts_language_field_name_for_id return NULL for out-of-bound id --- lib/binding_rust/lib.rs | 11 +++++++---- lib/src/language.c | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) 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;