Skip to content

Commit

Permalink
fix: add security
Browse files Browse the repository at this point in the history
  • Loading branch information
leogaudin committed Sep 1, 2024
1 parent 8a07d0e commit 77a3bd0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/ex09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ fn eval_tree(node: TreeNodeRef, sets: Vec<Vec<i32>>) -> Vec<i32> {

pub fn eval_set(formula: &str, sets: Vec<Vec<i32>>) -> Vec<i32> {
let tree: TreeNodeRef = create_tree(conjunctive_normal_form(formula).as_str());

let mut variables: Vec<char> = get_variables(formula);
if variables.len() > sets.len() {
panic!("Not enough sets");
}
variables.sort();
let mut result = eval_tree(tree, sets);

let mut result: Vec<i32> = eval_tree(tree, sets);
result.sort();

return result;
Expand Down
9 changes: 7 additions & 2 deletions src/tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, cmp::Ordering, rc::Rc};

#[derive(Debug, Clone)]
pub enum NodeValue {
Expand Down Expand Up @@ -124,7 +124,12 @@ pub fn create_tree(expression: &str) -> TreeNodeRef {
}
stack.push(node);
}
stack.pop().unwrap()

match stack.len().cmp(&1usize) {
Ordering::Greater => panic!("Too many operands, not enough operators."),
Ordering::Equal => return stack.pop().unwrap(),
Ordering::Less => panic!("Empty formula"),
}
}

pub fn eval_tree(node: TreeNodeRef) -> bool {
Expand Down

0 comments on commit 77a3bd0

Please sign in to comment.