Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
fix(solidity/core/slither): fixed and cleaned foundry config fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSwapFeeder committed Feb 22, 2024
1 parent 745790b commit 05abb2e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 69 deletions.
30 changes: 3 additions & 27 deletions toolchains/solidity/core/crates/slither-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@ impl Backend {
}

async fn analyze_file(&self, file: Url) {
if self.is_in_libs(file.path()).await
|| self.is_in_tests(file.path()).await
|| !self.is_in_src(file.path()).await
{
let normalized_path = normalize_slither_path(file.path());
if !self.is_in_src(&normalized_path).await {
self.client
.log_message(
MessageType::INFO,
Expand All @@ -203,33 +201,11 @@ impl Backend {
self.launch_slither(file).await
}

async fn is_in_libs(&self, path: &str) -> bool {
let state = self.data.lock().await;
for lib in state.libs_paths.iter() {
let fsrc = format!("/{}/", lib.replace('\"', ""));
if path.contains(&fsrc) {
return true;
}
}
false
}

async fn is_in_src(&self, path: &str) -> bool {
let state = self.data.lock().await;
for src in state.src_paths.iter() {
let fsrc = format!("/{}/", src.replace('\"', ""));
if path.contains(&fsrc) {
return true;
}
}
false
}

async fn is_in_tests(&self, path: &str) -> bool {
let state = self.data.lock().await;
for test in state.tests_paths.iter() {
let fsrc = format!("/{}/", test.replace('\"', ""));
if path.contains(&fsrc) {
if path.strip_prefix(&state.workspace).unwrap().contains(&fsrc) {
return true;
}
}
Expand Down
25 changes: 21 additions & 4 deletions toolchains/solidity/core/crates/slither-server/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@ pub struct SlitherData {
pub slither_processes: Vec<CancellationToken>,
pub receiver: Option<Receiver<SlitherDiag>>,
pub sender: Sender<SlitherDiag>,
pub libs_paths: Vec<String>,
pub src_paths: Vec<String>,
pub tests_paths: Vec<String>,
pub workspace: String,
}

impl SlitherData {
pub fn new() -> Self {
let (sender, receiver) = tokio::sync::mpsc::channel::<SlitherDiag>(100);
Self {
libs_paths: vec![],
src_paths: vec![],
tests_paths: vec![],
slither_processes: vec![],
receiver: Some(receiver),
sender,
Expand All @@ -43,6 +39,27 @@ impl SlitherData {
}
}

#[derive(Debug, Deserialize, Clone)]
pub enum FoundryArrOrStr {
Arr(Vec<String>),
Str(String),
}

#[derive(Debug, Deserialize, Clone)]
pub struct FoundryProfile {
pub src: Option<FoundryArrOrStr>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct FoundryProfiles {
pub default: Option<FoundryProfile>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct FoundryToml {
pub profiles: Option<FoundryProfiles>,
}

/////////////////////////
// SLITHER JSON OUTPUT //
/////////////////////////
Expand Down
50 changes: 12 additions & 38 deletions toolchains/solidity/core/crates/slither-server/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::SlitherError;
use crate::SlitherData;
use crate::{FoundryArrOrStr, FoundryToml, SlitherData};
use glob::glob;
use std::error::Error;
use std::process::Command as StdCommand;
Expand All @@ -26,54 +26,28 @@ pub fn normalize_slither_path(path: &str) -> String {
path.to_string()
}

fn extract_foundry_src(foundry: FoundryToml) -> Option<FoundryArrOrStr> {
foundry.profiles?.default?.src
}

pub fn parse_foundry_toml(foundry: String, state: &mut SlitherData) {
let foundry: toml::Value = match foundry.parse() {
let foundry: FoundryToml = match toml::from_str(&foundry) {
Ok(foundry) => foundry,
Err(e) => {
eprintln!("Error parsing foundry.toml: {}", e);
return;
}
};

let libs = foundry["profile"]["default"]["libs"].as_array();
match libs {
Some(libs) => {
for lib in libs {
state.libs_paths.push(lib.to_string());
}
}
None => {
state
.libs_paths
.push(foundry["profile"]["default"]["libs"].to_string());
}
}
let src = foundry["profile"]["default"]["src"].as_array();
match src {
Some(src) => {
for src in src {
match extract_foundry_src(foundry.clone()).unwrap_or(FoundryArrOrStr::Str("src".to_string())) {
FoundryArrOrStr::Arr(srcs) => {
for src in srcs {
state.src_paths.push(src.to_string());
}
}
None => {
state
.src_paths
.push(foundry["profile"]["default"]["src"].to_string());
FoundryArrOrStr::Str(src) => {
state.src_paths.push(src);
}
}
let tests = foundry["profile"]["default"]["test"].as_array();
match tests {
Some(tests) => {
for test in tests {
state.tests_paths.push(test.to_string());
}
}
None => {
state
.tests_paths
.push(foundry["profile"]["default"]["test"].to_string());
}
}
};
}

/**
Expand Down

0 comments on commit 05abb2e

Please sign in to comment.