Skip to content

Commit

Permalink
limit the number of instructions that can be evaluated (#2459)
Browse files Browse the repository at this point in the history
* limit the number of instructions that can be evaluated

* code cleanup
  • Loading branch information
tyrelr authored May 4, 2023
1 parent dc9e298 commit 39acaf1
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sqlx-sqlite/src/connection/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const OP_HALT: &str = "Halt";
const OP_HALT_IF_NULL: &str = "HaltIfNull";

const MAX_LOOP_COUNT: u8 = 2;
const MAX_TOTAL_INSTRUCTION_COUNT: u32 = 100_000;

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
enum ColumnType {
Expand Down Expand Up @@ -428,13 +429,21 @@ pub(super) fn explain(

let mut visited_branch_state: HashSet<BranchStateHash> = HashSet::new();

let mut gas = MAX_TOTAL_INSTRUCTION_COUNT;
let mut result_states = Vec::new();

while let Some(mut state) = states.pop() {
while state.program_i < program_size {
let (_, ref opcode, p1, p2, p3, ref p4) = program[state.program_i];
state.history.push(state.program_i);

//limit the number of 'instructions' that can be evaluated
if gas > 0 {
gas -= 1;
} else {
break;
}

if state.visited[state.program_i] > MAX_LOOP_COUNT {
if logger.log_enabled() {
let program_history: Vec<&(i64, String, i64, i64, i64, Vec<u8>)> =
Expand Down

0 comments on commit 39acaf1

Please sign in to comment.