Skip to content

Commit 38a004b

Browse files
committed
Fix Rust String to CString conversion before sending to ffi
Fixes #115 Image module is effected by this change. Rest of the modules which pass strings to C library are already handling this appropriately.
1 parent a1da45b commit 38a004b

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/image/mod.rs

+26-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use array::Array;
44
use defines::{AfError, BorderType, ColorSpace, Connectivity, InterpType, YCCStd, MomentType};
55
use error::HANDLE_ERROR;
66
use util::{AfArray, DimT, HasAfEnum, MutAfArray};
7-
use self::libc::{uint8_t, c_uint, c_int, c_float, c_double};
7+
use self::libc::{uint8_t, c_uint, c_int, c_float, c_double, c_char};
8+
use std::ffi::CString;
89

910
// unused functions from image.h header
1011
// af_load_image_memory
@@ -14,10 +15,10 @@ use self::libc::{uint8_t, c_uint, c_int, c_float, c_double};
1415
#[allow(dead_code)]
1516
extern {
1617
fn af_gradient(dx: MutAfArray, dy: MutAfArray, arr: AfArray) -> c_int;
17-
fn af_load_image(out: MutAfArray, filename: *const u8, iscolor: c_int) -> c_int;
18-
fn af_save_image(filename: *const u8, input: AfArray) -> c_int;
19-
fn af_load_image_native(out: MutAfArray, filename: *const u8) -> c_int;
20-
fn af_save_image_native(filename: *const u8, input: AfArray) -> c_int;
18+
fn af_load_image(out: MutAfArray, filename: *const c_char, iscolor: c_int) -> c_int;
19+
fn af_save_image(filename: *const c_char, input: AfArray) -> c_int;
20+
fn af_load_image_native(out: MutAfArray, filename: *const c_char) -> c_int;
21+
fn af_save_image_native(filename: *const c_char, input: AfArray) -> c_int;
2122

2223
fn af_resize(out: MutAfArray, input: AfArray,
2324
odim0: DimT, odim1: DimT, method: uint8_t) -> c_int;
@@ -135,11 +136,13 @@ pub fn gradient(input: &Array) -> (Array, Array) {
135136
/// An Array with pixel values loaded from the image
136137
#[allow(unused_mut)]
137138
pub fn load_image(filename: String, is_color: bool) -> Array {
139+
let cstr_param = match CString::new(filename) {
140+
Ok(cstr) => cstr,
141+
Err(_) => panic!("CString creation from input filename failed"),
142+
};
138143
unsafe {
139144
let mut temp: i64 = 0;
140-
let err_val = af_load_image(&mut temp as MutAfArray,
141-
filename.clone().as_bytes().as_ptr() as *const u8,
142-
is_color as c_int);
145+
let err_val = af_load_image(&mut temp as MutAfArray, cstr_param.as_ptr(), is_color as c_int);
143146
HANDLE_ERROR(AfError::from(err_val));
144147
Array::from(temp)
145148
}
@@ -165,10 +168,13 @@ pub fn load_image(filename: String, is_color: bool) -> Array {
165168
/// An Array with pixel values loaded from the image
166169
#[allow(unused_mut)]
167170
pub fn load_image_native(filename: String) -> Array {
171+
let cstr_param = match CString::new(filename) {
172+
Ok(cstr) => cstr,
173+
Err(_) => panic!("CString creation from input filename failed"),
174+
};
168175
unsafe {
169176
let mut temp: i64 = 0;
170-
let err_val = af_load_image_native(&mut temp as MutAfArray,
171-
filename.clone().as_bytes().as_ptr() as *const u8);
177+
let err_val = af_load_image_native(&mut temp as MutAfArray, cstr_param.as_ptr());
172178
HANDLE_ERROR(AfError::from(err_val));
173179
Array::from(temp)
174180
}
@@ -182,9 +188,12 @@ pub fn load_image_native(filename: String) -> Array {
182188
/// - `input` is the Array to be stored into the image file
183189
#[allow(unused_mut)]
184190
pub fn save_image(filename: String, input: &Array) {
191+
let cstr_param = match CString::new(filename) {
192+
Ok(cstr) => cstr,
193+
Err(_) => panic!("CString creation from input filename failed"),
194+
};
185195
unsafe {
186-
let err_val = af_save_image(filename.clone().as_bytes().as_ptr() as *const u8,
187-
input.get() as AfArray);
196+
let err_val = af_save_image(cstr_param.as_ptr(), input.get() as AfArray);
188197
HANDLE_ERROR(AfError::from(err_val));
189198
}
190199
}
@@ -207,9 +216,12 @@ pub fn save_image(filename: String, input: &Array) {
207216
/// - `input` is the Array to be saved. Should be U8 for saving 8-bit image, U16 for 16-bit image, and F32 for 32-bit image.
208217
#[allow(unused_mut)]
209218
pub fn save_image_native(filename: String, input: &Array) {
219+
let cstr_param = match CString::new(filename) {
220+
Ok(cstr) => cstr,
221+
Err(_) => panic!("CString creation from input filename failed"),
222+
};
210223
unsafe {
211-
let err_val = af_save_image_native(filename.clone().as_bytes().as_ptr() as *const u8,
212-
input.get() as AfArray);
224+
let err_val = af_save_image_native(cstr_param.as_ptr(), input.get() as AfArray);
213225
HANDLE_ERROR(AfError::from(err_val));
214226
}
215227
}

0 commit comments

Comments
 (0)