Skip to content
17 changes: 13 additions & 4 deletions fuzz/fuzz_targets/fuzz_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
#[macro_use]
extern crate libfuzzer_sys;

use diffutilslib::side_diff;
use diffutilslib::side_diff::{self, Params};

use std::fs::File;
use std::io::Write;
use diffutilslib::params::Params;

fuzz_target!(|x: (Vec<u8>, Vec<u8>, /* usize, usize */ bool)| {
let (original, new, /* width, tabsize, */ expand) = x;
Expand All @@ -22,7 +21,16 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>, /* usize, usize */ bool)| {
..Default::default()
};
let mut output_buf = vec![];
side_diff::diff(&original, &new, &mut output_buf, &params);
side_diff::diff(
&original,
&new,
&mut output_buf,
&Params {
width: params.width,
tabsize: params.tabsize,
expand_tabs: params.expand_tabs,
},
);
File::create("target/fuzz.file.original")
.unwrap()
.write_all(&original)
Expand All @@ -39,4 +47,5 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>, /* usize, usize */ bool)| {
.unwrap()
.write_all(&output_buf)
.unwrap();
});
});

51 changes: 20 additions & 31 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
// files that was distributed with this source code.

use crate::params::{parse_params, Format};
use crate::utils::report_failure_to_read_input_file;
use crate::utils;
use crate::{context_diff, ed_diff, normal_diff, side_diff, unified_diff};
use std::env::ArgsOs;
use std::ffi::OsString;
use std::fs;
use std::io::{self, stdout, Read, Write};
use std::io::{self, stdout, Write};
use std::iter::Peekable;
use std::process::{exit, ExitCode};

Expand All @@ -23,6 +21,7 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
eprintln!("{error}");
exit(2);
});

// if from and to are the same file, no need to perform any comparison
let maybe_report_identical_files = || {
if params.report_identical_files {
Expand All @@ -40,35 +39,16 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
return ExitCode::SUCCESS;
}

// read files
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
if filepath == "-" {
let mut content = Vec::new();
io::stdin().read_to_end(&mut content).and(Ok(content))
} else {
fs::read(filepath)
}
}
let mut io_error = false;
let from_content = match read_file_contents(&params.from) {
Ok(from_content) => from_content,
Err(e) => {
report_failure_to_read_input_file(&params.executable, &params.from, &e);
io_error = true;
vec![]
}
};
let to_content = match read_file_contents(&params.to) {
Ok(to_content) => to_content,
let (from_content, to_content) = match utils::read_both_files(&params.from, &params.to) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on main, diff already printed both errors (and the test had count(2)). what does this fix for diff?

Ok(contents) => contents,
Err(e) => {
report_failure_to_read_input_file(&params.executable, &params.to, &e);
io_error = true;
vec![]
eprintln!(
"{}",
utils::format_failure_to_read_input_files(&params.executable, &e)
);
return ExitCode::from(2);
}
};
if io_error {
return ExitCode::from(2);
}

// run diff
let result: Vec<u8> = match params.format {
Expand All @@ -81,7 +61,16 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
}),
Format::SideBySide => {
let mut output = stdout().lock();
side_diff::diff(&from_content, &to_content, &mut output, &params)
side_diff::diff(
&from_content,
&to_content,
&mut output,
&side_diff::Params {
tabsize: params.tabsize,
width: params.width,
expand_tabs: params.expand_tabs,
},
)
}
};
if params.brief && !result.is_empty() {
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod ed_diff;
mod macros;
mod normal_diff;
mod params;
mod sdiff;
mod side_diff;
mod unified_diff;
mod utils;
Expand Down Expand Up @@ -72,6 +73,7 @@ fn main() -> ExitCode {
match util_name.to_str() {
Some("diff") => diff::main(args),
Some("cmp") => cmp::main(args),
Some("sdiff") => sdiff::main(args),
Some(name) => {
eprintln!("{name}: utility not supported");
ExitCode::from(2)
Expand Down
Loading
Loading