Skip to content

Commit

Permalink
Merge branch 'main' into clippy2
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre authored Oct 9, 2023
2 parents 02ab93c + 0695437 commit b802c8c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 83 deletions.
50 changes: 0 additions & 50 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,56 +82,6 @@ jobs:
grep --ignore-case "all deps seem to have been used" udeps.log || { printf "%s\n" "::${fault_type} ::${fault_prefix}: \`cargo udeps\`: style violation (unused dependency found)" ; fault=true ; }
if [ -n "${{ steps.vars.outputs.FAIL_ON_FAULT }}" ] && [ -n "$fault" ]; then exit 1 ; fi
fuzz:
name: Run the fuzzers
runs-on: ubuntu-latest
env:
RUN_FOR: 60
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- name: Install `cargo-fuzz`
run: cargo install cargo-fuzz
- uses: Swatinem/rust-cache@v2
- name: Run fuzz_date for XX seconds
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_date -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_test for XX seconds
continue-on-error: true
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_test -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_expr for XX seconds
continue-on-error: true
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_expr -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_parse_glob for XX seconds
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_parse_glob -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_parse_size for XX seconds
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_parse_size -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_parse_time for XX seconds
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_parse_time -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
doc_warnings:
name: Documentation/warnings
runs-on: ${{ matrix.job.os }}
Expand Down
64 changes: 64 additions & 0 deletions .github/workflows/fuzzing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Fuzzing

# spell-checker:ignore fuzzer

on: [push, pull_request]

permissions:
contents: read # to fetch code (actions/checkout)

# End the current execution if there is a new changeset in the PR.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
fuzz:
name: Run the fuzzers
runs-on: ubuntu-latest
env:
RUN_FOR: 60
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- name: Install `cargo-fuzz`
run: cargo install cargo-fuzz
- uses: Swatinem/rust-cache@v2
- name: Run fuzz_date for XX seconds
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_date -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_test for XX seconds
continue-on-error: true
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_test -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_expr for XX seconds
continue-on-error: true
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_expr -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_parse_glob for XX seconds
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_parse_glob -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_parse_size for XX seconds
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_parse_size -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
- name: Run fuzz_parse_time for XX seconds
shell: bash
run: |
## Run it
cd fuzz
cargo +nightly fuzz run fuzz_parse_time -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
15 changes: 2 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ pub enum AstNode {
operands: OperandsList,
},
}

impl AstNode {
fn debug_dump(&self) {
self.debug_dump_impl(1);
}

fn debug_dump_impl(&self, depth: usize) {
for _ in 0..depth {
print!("\t",);
Expand All @@ -52,7 +54,7 @@ impl AstNode {
operands,
} => {
println!(
"Node( {} ) at #{} (evaluate -> {:?})",
"Node( {} ) at #{} ( evaluate -> {:?} )",
op_type,
token_idx,
self.evaluate()
Expand All @@ -71,12 +73,14 @@ impl AstNode {
operands,
})
}

fn new_leaf(token_idx: usize, value: &str) -> Box<Self> {
Box::new(Self::Leaf {
token_idx,
value: value.into(),
})
}

pub fn evaluate(&self) -> Result<String, String> {
match self {
Self::Leaf { value, .. } => Ok(value.clone()),
Expand Down Expand Up @@ -154,9 +158,27 @@ impl AstNode {
},
}
}

pub fn operand_values(&self) -> Result<Vec<String>, String> {
if let Self::Node { operands, .. } = self {
if let Self::Node {
operands, op_type, ..
} = self
{
let mut out = Vec::with_capacity(operands.len());
let mut operands = operands.iter();
// check the first value before `|`, stop evaluate and return directly if it is true.
// push dummy to pass the check of `len() == 2`
if op_type == "|" {
if let Some(value) = operands.next() {
let value = value.evaluate()?;
out.push(value.clone());
if value_as_bool(&value) {
out.push(String::from("dummy"));
return Ok(out);
}
}
}

for operand in operands {
let value = operand.evaluate()?;
out.push(value);
Expand Down Expand Up @@ -240,6 +262,7 @@ fn ast_from_rpn(rpn: &mut TokenStack) -> Result<Box<AstNode>, String> {
}
}
}

fn maybe_ast_node(
token_idx: usize,
op_type: &str,
Expand Down Expand Up @@ -503,13 +526,15 @@ fn prefix_operator_substr(values: &[String]) -> String {
fn bool_as_int(b: bool) -> u8 {
u8::from(b)
}

fn bool_as_string(b: bool) -> String {
if b {
"1".to_string()
} else {
"0".to_string()
}
}

fn value_as_bool(s: &str) -> bool {
if s.is_empty() {
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/uu/expr/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum Token {
value: String,
},
}

impl Token {
fn new_infix_op(v: &str, left_assoc: bool, precedence: u8) -> Self {
Self::InfixOp {
Expand All @@ -46,6 +47,7 @@ impl Token {
value: v.into(),
}
}

fn new_value(v: &str) -> Self {
Self::Value { value: v.into() }
}
Expand All @@ -56,12 +58,14 @@ impl Token {
_ => false,
}
}

fn is_a_number(&self) -> bool {
match self {
Self::Value { value, .. } => value.parse::<BigInt>().is_ok(),
_ => false,
}
}

fn is_a_close_paren(&self) -> bool {
matches!(*self, Self::ParClose)
}
Expand Down
17 changes: 0 additions & 17 deletions src/uucore/src/lib/features/tokenize/num_format/format_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ pub enum FieldType {
Charf,
}

// #[allow(non_camel_case_types)]
// pub enum FChar {
// d,
// e,
// E,
// i,
// f,
// F,
// g,
// G,
// u,
// x,
// X,
// o
// }
//

// a Sub Tokens' fields are stored
// as a single object so they can be more simply
// passed by ref to num_format in a Sub method
Expand Down
1 change: 0 additions & 1 deletion src/uucore/src/lib/features/tokenize/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use std::iter::Peekable;
use std::process::exit;
use std::slice::Iter;
use std::str::Chars;
// use std::collections::HashSet;

use super::num_format::format_field::{FieldType, FormatField};
use super::num_format::num_format;
Expand Down
15 changes: 15 additions & 0 deletions tests/by-util/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ fn test_or() {
.args(&["-14", "|", "1"])
.succeeds()
.stdout_only("-14\n");

new_ucmd!()
.args(&["1", "|", "a", "/", "5"])
.succeeds()
.stdout_only("1\n");

new_ucmd!()
.args(&["foo", "|", "a", "/", "5"])
.succeeds()
.stdout_only("foo\n");

new_ucmd!()
.args(&["0", "|", "10", "/", "5"])
.succeeds()
.stdout_only("2\n");
}

#[test]
Expand Down

0 comments on commit b802c8c

Please sign in to comment.