From 39acaf145936d748b3f03cf236ee72a6af98c507 Mon Sep 17 00:00:00 2001 From: tyrelr <44035897+tyrelr@users.noreply.github.com> Date: Thu, 4 May 2023 12:46:11 -0600 Subject: [PATCH] limit the number of instructions that can be evaluated (#2459) * limit the number of instructions that can be evaluated * code cleanup --- sqlx-sqlite/src/connection/explain.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sqlx-sqlite/src/connection/explain.rs b/sqlx-sqlite/src/connection/explain.rs index d5c8c5e8..59a63909 100644 --- a/sqlx-sqlite/src/connection/explain.rs +++ b/sqlx-sqlite/src/connection/explain.rs @@ -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 { @@ -428,6 +429,7 @@ pub(super) fn explain( let mut visited_branch_state: HashSet = HashSet::new(); + let mut gas = MAX_TOTAL_INSTRUCTION_COUNT; let mut result_states = Vec::new(); while let Some(mut state) = states.pop() { @@ -435,6 +437,13 @@ pub(super) fn explain( 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)> =