Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch potential null pointers returned by Dataset::spatial_ref #540

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,17 @@ impl Dataset {
#[cfg(major_ge_3)]
/// Get the spatial reference system for this dataset.
pub fn spatial_ref(&self) -> Result<SpatialRef> {
unsafe { SpatialRef::from_c_obj(gdal_sys::GDALGetSpatialRef(self.c_dataset)) }
unsafe {
let spatial_ref = gdal_sys::GDALGetSpatialRef(self.c_dataset);
if spatial_ref.is_null() {
Err(GdalError::NullPointer {
method_name: "GDALGetSpatialRef",
msg: "Unable to get a spatial reference".to_string(),
})
} else {
SpatialRef::from_c_obj(spatial_ref)
}
}
}

#[cfg(major_ge_3)]
Expand Down
1 change: 1 addition & 0 deletions src/spatial_ref/srs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl SpatialRef {
/// # Safety
/// The handle passed to this function must be valid.
pub unsafe fn from_c_obj(c_obj: gdal_sys::OGRSpatialReferenceH) -> Result<SpatialRef> {
assert!(!c_obj.is_null(), "Expected a pointer that is not null");
let mut_c_obj = gdal_sys::OSRClone(c_obj);
if mut_c_obj.is_null() {
Err(_last_null_pointer_err("OSRClone"))
Expand Down
Loading