Skip to content

Commit 7eccf99

Browse files
authored
fix: do not pass whitespace-only strings to Calculator expr evaluation lib (#532)
1 parent 5044a98 commit 7eccf99

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src-tauri/src/local/calculator.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,17 @@ impl SearchSource for CalculatorSource {
108108
}
109109

110110
async fn search(&self, query: SearchQuery) -> Result<QueryResponse, SearchError> {
111-
let query_string = query
112-
.query_strings
113-
.get("query")
114-
.unwrap_or(&"".to_string())
115-
.to_string();
111+
let Some(query_string) = query.query_strings.get("query") else {
112+
return Ok(QueryResponse {
113+
source: self.get_type(),
114+
hits: Vec::new(),
115+
total_hits: 0,
116+
});
117+
};
118+
119+
// Trim the leading and tailing whitespace so that our later if condition
120+
// will only be evaluated against non-whitespace characters.
121+
let query_string = query_string.trim();
116122

117123
if query_string.is_empty() || query_string.len() == 1 {
118124
return Ok(QueryResponse {
@@ -122,11 +128,11 @@ impl SearchSource for CalculatorSource {
122128
});
123129
}
124130

125-
match meval::eval_str(&query_string) {
131+
match meval::eval_str(query_string) {
126132
Ok(num) => {
127133
let mut payload: HashMap<String, Value> = HashMap::new();
128134

129-
let payload_query = parse_query(query_string);
135+
let payload_query = parse_query(query_string.into());
130136
let payload_result = parse_result(num);
131137

132138
payload.insert("query".to_string(), payload_query);

0 commit comments

Comments
 (0)