Merge pull request #561 from ubolonton/fix-out-of-bounds-access

Fix out-of-bounds array access in `Language` APIs
This commit is contained in:
Max Brunsfeld 2020-02-27 09:27:18 -08:00 committed by GitHub
commit 048b8c87a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View file

@ -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.
@ -212,10 +215,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.