Skip to content

Commit

Permalink
Handle null pointer when attempting to build an error string
Browse files Browse the repository at this point in the history
It's possible that libproj will return a null pointer instead of an
actual error string. In this case we now return a specific error
indicating this.

Fixes georust#73
  • Loading branch information
urschrei committed Aug 16, 2021
1 parent ed01907 commit dbb86b4
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/proj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub enum ProjError {
HeaderConversion(#[from] reqwest::header::ToStrError),
#[error("A {0} error occurred for url {1} after {2} retries")]
DownloadError(String, String, u8),
#[error("Got a null pointer while attempting to build an error String")]
NullPointer
}

/// The bounding box of an area of use
Expand Down Expand Up @@ -125,7 +127,9 @@ impl Area {

/// Easily get a String from the external library
pub(crate) unsafe fn _string(raw_ptr: *const c_char) -> Result<String, ProjError> {
assert!(!raw_ptr.is_null());
if raw_ptr.is_null() {
return Err(ProjError::NullPointer);
}
let c_str = CStr::from_ptr(raw_ptr);
Ok(str::from_utf8(c_str.to_bytes())?.to_string())
}
Expand Down

0 comments on commit dbb86b4

Please sign in to comment.