codec: fix codec description potential null ptr issue

Codecs may not have .long_name initialized.

Issue raised by Pilyushkin in #34.
This commit is contained in:
Zhiming Wang 2020-10-04 11:37:59 +08:00
parent ae06272af1
commit 6f0a14ee8b
No known key found for this signature in database
GPG Key ID: 5B58F95EC95965D8

View File

@ -41,7 +41,14 @@ impl Codec {
} }
pub fn description(&self) -> &str { pub fn description(&self) -> &str {
unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).long_name).to_bytes()) } unsafe {
let long_name = (*self.as_ptr()).long_name;
if long_name.is_null() {
""
} else {
from_utf8_unchecked(CStr::from_ptr(long_name).to_bytes())
}
}
} }
pub fn medium(&self) -> media::Type { pub fn medium(&self) -> media::Type {