Skip to content

Fix unchecked string creation from ffi return value #212

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

Merged
merged 2 commits into from
Jan 28, 2020
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
10 changes: 8 additions & 2 deletions .github/workflows/pull_request.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- master

name: Pull Request
name: ci

jobs:
build_test:
Expand Down
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
}