Skip to content

Commit

Permalink
Fix unchecked string creation from ffi return value
Browse files Browse the repository at this point in the history
  • Loading branch information
9prady9 committed Jan 28, 2020
1 parent 438118e commit 353ba6c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate libc;

use self::libc::{c_char, c_int};
use self::libc::c_char;
use crate::defines::AfError;
use crate::util::{free_host, DimT, MutDimT};
use std::error::Error;
Expand All @@ -10,7 +10,7 @@ use std::sync::RwLock;

#[allow(dead_code)]
extern "C" {
fn af_get_last_error(str: *mut *mut c_char, len: *mut DimT) -> c_int;
fn af_get_last_error(str: *mut *mut c_char, len: *mut DimT);
}

/// Signature of error handling callback function
Expand Down Expand Up @@ -99,14 +99,15 @@ pub fn HANDLE_ERROR(error_code: AfError) {
}

pub fn get_last_error() -> String {
let result: String;
let mut result: String = String::from("No Last Error");
let mut tmp: *mut c_char = ::std::ptr::null_mut();
let mut len: DimT = 0;
unsafe {
let mut tmp: *mut c_char = ::std::ptr::null_mut();
let mut len: DimT = 0;
let err_val = af_get_last_error(&mut tmp, &mut len as MutDimT);
HANDLE_ERROR(AfError::from(err_val));
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
free_host(tmp);
af_get_last_error(&mut tmp, &mut len as MutDimT);
if len > 0 {
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
free_host(tmp);
}
}
result
}

0 comments on commit 353ba6c

Please sign in to comment.