Skip to content

Commit 9e5eda0

Browse files
authored
Merge pull request #172 from ttencate/fix/geometry_from_wkt_leak
Fix memory leak in Geometry::from_wkt
2 parents c62a306 + 5724f7f commit 9e5eda0

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
`SpatialRef::axis_mapping_strategy` instead.
7777
* Add support for reading and setting rasterband colour interpretations
7878
* <https://github.com/georust/gdal/pull/144>
79+
* Fixed memory leak in `Geometry::from_wkt`
80+
* <https://github.com/georust/gdal/pull/172>
81+
7982
## 0.7.1
8083
* fix docs.rs build for gdal-sys
8184
* <https://github.com/georust/gdal/pull/128>

src/vector/geometry.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::spatial_ref::{CoordTransform, SpatialRef};
22
use crate::utils::{_last_null_pointer_err, _string};
33
use gdal_sys::{self, OGRErr, OGRGeometryH, OGRwkbGeometryType};
4-
use libc::{c_double, c_int, c_void};
4+
use libc::{c_char, c_double, c_int, c_void};
55
use std::cell::RefCell;
66
use std::ffi::CString;
77
use std::fmt::{self, Debug};
@@ -75,7 +75,9 @@ impl Geometry {
7575
/// [WKT](https://en.wikipedia.org/wiki/Well-known_text) string.
7676
pub fn from_wkt(wkt: &str) -> Result<Geometry> {
7777
let c_wkt = CString::new(wkt)?;
78-
let mut c_wkt_ptr = c_wkt.into_raw();
78+
// OGR_G_CreateFromWkt does not write to the pointed-to memory, but this is not reflected
79+
// in its signature (`char**` instead of `char const**`), so we need a scary looking cast.
80+
let mut c_wkt_ptr = c_wkt.as_ptr() as *mut c_char;
7981
let mut c_geom = null_mut();
8082
let rv = unsafe { gdal_sys::OGR_G_CreateFromWkt(&mut c_wkt_ptr, null_mut(), &mut c_geom) };
8183
if rv != OGRErr::OGRERR_NONE {

0 commit comments

Comments
 (0)