Skip to content

Commit

Permalink
Catch potential null pointers returned by Dataset::spatial_ref
Browse files Browse the repository at this point in the history
This otherwise hits an assertion in gdal debug builds for the OSRClone method
  • Loading branch information
weiznich authored and lnicola committed May 22, 2024
1 parent b06de82 commit 19c5104
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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

0 comments on commit 19c5104

Please sign in to comment.