Bugfix: Fix a panic when invalid builtin function names are passed to the executor (#3454)

This commit is contained in:
Mees Delzenne 2024-02-07 16:13:40 +01:00 committed by GitHub
parent 8bce4d7789
commit 9ff0fade50
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 3 deletions

View file

@ -71,7 +71,12 @@ macro_rules! dispatch {
#[allow(clippy::redundant_closure_call)] #[allow(clippy::redundant_closure_call)]
$($wrapper)*(|| $($function_path)::+($($ctx_arg,)* args))()$(.$await)* $($wrapper)*(|| $($function_path)::+($($ctx_arg,)* args))()$(.$await)*
},)+ },)+
_ => unreachable!() _ => {
return Err($crate::err::Error::InvalidFunction{
name: String::from($name),
message: "no such builtin function".to_string()
})
}
} }
} }
}; };

View file

@ -4,7 +4,7 @@ mod helpers;
use helpers::new_ds; use helpers::new_ds;
use surrealdb::dbs::Session; use surrealdb::dbs::Session;
use surrealdb::err::Error; use surrealdb::err::Error;
use surrealdb::sql::{Number, Value}; use surrealdb_core::sql::{self, Number, Value};
async fn test_queries(sql: &str, desired_responses: &[&str]) -> Result<(), Error> { async fn test_queries(sql: &str, desired_responses: &[&str]) -> Result<(), Error> {
let db = new_ds().await?; let db = new_ds().await?;
@ -13,7 +13,7 @@ async fn test_queries(sql: &str, desired_responses: &[&str]) -> Result<(), Error
for (i, r) in response.into_iter().map(|r| r.result).enumerate() { for (i, r) in response.into_iter().map(|r| r.result).enumerate() {
let v = r?; let v = r?;
if let Some(desired_response) = desired_responses.get(i) { if let Some(desired_response) = desired_responses.get(i) {
let desired_value = Value::parse(desired_response); let desired_value = sql::Value::parse(desired_response);
// If both values are NaN, they are equal from a test PoV // If both values are NaN, they are equal from a test PoV
if !desired_value.is_nan() || !v.is_nan() { if !desired_value.is_nan() || !v.is_nan() {
assert_eq!( assert_eq!(
@ -59,6 +59,24 @@ async fn check_test_is_error(sql: &str, expected_errors: &[&str]) -> Result<(),
Ok(()) Ok(())
} }
#[tokio::test]
async fn error_on_invalid_function() -> Result<(), Error> {
let dbs = new_ds().await?;
let query = sql::Query(sql::Statements(vec![sql::Statement::Value(sql::Value::Function(
Box::new(sql::Function::Normal("this is an invalid function name".to_string(), Vec::new())),
))]));
let session = Session::owner().with_ns("test").with_db("test");
let mut resp = dbs.process(query, &session, None).await.unwrap();
assert_eq!(resp.len(), 1);
match resp.pop().unwrap().result {
Err(Error::InvalidFunction {
..
}) => {}
x => panic!("returned wrong result {:#?}", x),
}
Ok(())
}
// -------------------------------------------------- // --------------------------------------------------
// array // array
// -------------------------------------------------- // --------------------------------------------------