Skip to content
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

fix: FFI type mismatches between i8 and c_char #1128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions apps/api/sharedLibs/html-transformer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{collections::HashMap, ffi::{CStr, CString}};
use libc::c_char;

use kuchikiki::{parse_html, traits::TendrilSink};
use serde::Deserialize;
Expand All @@ -10,7 +11,7 @@ use url::Url;
/// # Safety
/// Input options must be a C HTML string. Output will be a JSON string array. Output string must be freed with free_string.
#[no_mangle]
pub unsafe extern "C" fn extract_links(html: *const libc::c_char) -> *mut i8 {
pub unsafe extern "C" fn extract_links(html: *const c_char) -> *mut c_char {
let html = unsafe { CStr::from_ptr(html) }.to_str().unwrap();

let document = parse_html().one(html);
Expand Down Expand Up @@ -54,7 +55,7 @@ macro_rules! insert_meta_property {
/// # Safety
/// Input options must be a C HTML string. Output will be a JSON object. Output string must be freed with free_string.
#[no_mangle]
pub unsafe extern "C" fn extract_metadata(html: *const libc::c_char) -> *mut i8 {
pub unsafe extern "C" fn extract_metadata(html: *const c_char) -> *mut c_char {
let html = unsafe { CStr::from_ptr(html) }.to_str().unwrap();

let document = parse_html().one(html);
Expand Down Expand Up @@ -334,7 +335,7 @@ fn _transform_html_inner(opts: TranformHTMLOptions) -> Result<String, ()> {
/// # Safety
/// Input options must be a C JSON string. Output will be an HTML string. Output string must be freed with free_string.
#[no_mangle]
pub unsafe extern "C" fn transform_html(opts: *const libc::c_char) -> *mut i8 {
pub unsafe extern "C" fn transform_html(opts: *const c_char) -> *mut c_char {
let opts: TranformHTMLOptions = match unsafe { CStr::from_ptr(opts) }.to_str().map_err(|_| ()).and_then(|x| serde_json::de::from_str(x).map_err(|_| ())) {
Ok(x) => x,
Err(_) => {
Expand All @@ -355,6 +356,6 @@ pub unsafe extern "C" fn transform_html(opts: *const libc::c_char) -> *mut i8 {
/// # Safety
/// ptr must be a non-freed string pointer returned by Rust code.
#[no_mangle]
pub unsafe extern "C" fn free_string(ptr: *mut i8) {
pub unsafe extern "C" fn free_string(ptr: *mut c_char) {
drop(unsafe { CString::from_raw(ptr) })
}