Make ts_language_field_name_for_id return NULL for out-of-bound id

This commit is contained in:
Tuấn-Anh Nguyễn 2020-02-27 21:09:20 +07:00
parent a578ba54c4
commit c719e24a45
2 changed files with 8 additions and 5 deletions

View file

@ -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.

View file

@ -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;