From 3770f6cc7ff7e993e506aecb86e1c5ed90b0353b Mon Sep 17 00:00:00 2001 From: Arseniy Volynets <vol0ncar@yandex.ru> Date: Thu, 7 Mar 2024 20:38:13 +0300 Subject: [PATCH] fix: parsing password captures extra quotes --- sbroad-core/src/frontend/sql.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/sbroad-core/src/frontend/sql.rs b/sbroad-core/src/frontend/sql.rs index 453e945ec5..689fb9a4df 100644 --- a/sbroad-core/src/frontend/sql.rs +++ b/sbroad-core/src/frontend/sql.rs @@ -3,6 +3,7 @@ //! Parses an SQL statement to the abstract syntax tree (AST) //! and builds the intermediate representation (IR). +use core::panic; use pest::iterators::{Pair, Pairs}; use pest::pratt_parser::PrattParser; use pest::Parser; @@ -111,6 +112,21 @@ fn get_timeout(ast: &AbstractSyntaxTree, node_id: usize) -> Result<Decimal, Sbro )) } +fn parse_string_literal(ast: &AbstractSyntaxTree, node_id: usize) -> Result<String, SbroadError> { + let node = ast.nodes.get_node(node_id)?; + let str_ref = node + .value + .as_ref() + .expect("Rule node must contain string value."); + assert!( + node.rule == Rule::SingleQuotedString, + "Expected SingleQuotedString, got: {:?}", + node.rule + ); + + Ok(str_ref[1..str_ref.len() - 1].to_string()) +} + /// Parse node from which we want to get String value. fn parse_string_value_node(ast: &AbstractSyntaxTree, node_id: usize) -> Result<&str, SbroadError> { let string_value_node = ast.nodes.get_node(node_id)?; @@ -2527,8 +2543,7 @@ impl AbstractSyntaxTree { .children .first() .expect("Password expected as a first child"); - let password = - String::from(parse_string_value_node(self, *pwd_node_id)?); + let password = parse_string_literal(self, *pwd_node_id)?; let mut auth_method = get_default_auth_method(); if let Some(auth_method_node_id) = alter_option_node.children.get(1) { @@ -2585,7 +2600,7 @@ impl AbstractSyntaxTree { Some(String::from("Password expected as a second child")), ) })?; - let password = String::from(parse_string_value_node(self, *pwd_node_id)?); + let password = parse_string_literal(self, *pwd_node_id)?; let mut timeout = get_default_timeout(); let mut auth_method = get_default_auth_method(); -- GitLab