Skip to content

Commit

Permalink
Remove lossy UTF8 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed committed Aug 21, 2024
1 parent ae958a0 commit 6e93fc8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
9 changes: 8 additions & 1 deletion crates/solidity/src/process/worker_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::Input;
use super::Output;
use super::Process;

use anyhow::Context;
use serde::Deserialize;

#[derive(Deserialize)]
Expand Down Expand Up @@ -87,8 +88,14 @@ impl Process for WorkerProcess {
unsafe { resolc_compile(input_cstring.as_ptr(), input_cstring.as_bytes().len()) };

// Convert the output pointer back to a Rust string
let output_str = unsafe { CStr::from_ptr(output_ptr).to_string_lossy().into_owned() };
let output_str = unsafe {
CStr::from_ptr(output_ptr)
.to_str()
.with_context(|| "Failed to convert C string to Rust string")
.map(str::to_owned)
};
unsafe { libc::free(output_ptr as *mut c_void) };
let output_str = output_str?;
let response: Response = serde_json::from_str(&output_str)
.map_err(|error| anyhow::anyhow!("Worker output parsing error: {}", error,))?;
match response {
Expand Down
23 changes: 15 additions & 8 deletions crates/solidity/src/solc/soljson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::compiler::pipeline::Pipeline;
use crate::compiler::standard_json::input::Input as StandardJsonInput;
use crate::compiler::standard_json::output::Output as StandardJsonOutput;
use crate::compiler::version::Version;
use anyhow::Context;
use std::ffi::{c_char, c_void, CStr, CString};

use super::Compiler;
Expand All @@ -17,24 +18,30 @@ extern "C" {
fn soljson_compile(inputPtr: *const c_char, inputLen: usize) -> *const c_char;
}

fn get_soljson_version() -> String {
fn get_soljson_version() -> anyhow::Result<String> {
unsafe {
let version_ptr = soljson_version();
let version = CStr::from_ptr(version_ptr).to_string_lossy().into_owned();
let version = CStr::from_ptr(version_ptr)
.to_str()
.with_context(|| "Failed to convert C string to Rust string")
.map(str::to_owned);
libc::free(version_ptr as *mut c_void);
version
Ok(version?)
}
}

pub fn compile_standard_json(input: String) -> String {
pub fn compile_standard_json(input: String) -> anyhow::Result<String> {
let c_input = CString::new(input).unwrap();
let c_input_len = c_input.as_bytes().len();

unsafe {
let output_ptr = soljson_compile(c_input.as_ptr(), c_input_len);
let output_json = CStr::from_ptr(output_ptr).to_string_lossy().into_owned();
let output_json = CStr::from_ptr(output_ptr)
.to_str()
.with_context(|| "Failed to convert C string to Rust string")
.map(str::to_owned);
libc::free(output_ptr as *mut c_void);
output_json
Ok(output_json?)
}
}

Expand All @@ -58,7 +65,7 @@ impl Compiler for SoljsonCompiler {
let suppressed_warnings = input.suppressed_warnings.take().unwrap_or_default();

let input_json = serde_json::to_string(&input).expect("Always valid");
let out = compile_standard_json(input_json);
let out = compile_standard_json(input_json)?;
let mut output: StandardJsonOutput = revive_common::deserialize_from_slice(out.as_bytes())
.map_err(|error| {
anyhow::anyhow!(
Expand Down Expand Up @@ -88,7 +95,7 @@ impl Compiler for SoljsonCompiler {
}

fn version(&mut self) -> anyhow::Result<Version> {
let version = get_soljson_version();
let version = get_soljson_version()?;
let long = version.clone();
let default: semver::Version = version
.split('+')
Expand Down

0 comments on commit 6e93fc8

Please sign in to comment.