Bugfix: Prevent overflow in math::power (#3162)

This commit is contained in:
Mees Delzenne 2023-12-15 22:50:34 +01:00 committed by GitHub
parent 63214a82c9
commit a7f186424e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -13,7 +13,7 @@ use crate::fnc::util::math::top::Top;
use crate::fnc::util::math::trimean::Trimean; use crate::fnc::util::math::trimean::Trimean;
use crate::fnc::util::math::variance::Variance; use crate::fnc::util::math::variance::Variance;
use crate::sql::number::{Number, Sort}; use crate::sql::number::{Number, Sort};
use crate::sql::value::Value; use crate::sql::value::{TryPow, Value};
pub fn abs((arg,): (Number,)) -> Result<Value, Error> { pub fn abs((arg,): (Number,)) -> Result<Value, Error> {
Ok(arg.abs().into()) Ok(arg.abs().into())
@ -95,7 +95,7 @@ pub fn percentile((mut array, n): (Vec<Number>, Number)) -> Result<Value, Error>
} }
pub fn pow((arg, pow): (Number, Number)) -> Result<Value, Error> { pub fn pow((arg, pow): (Number, Number)) -> Result<Value, Error> {
Ok(arg.pow(pow).into()) Ok(arg.try_pow(pow)?.into())
} }
pub fn product((array,): (Vec<Number>,)) -> Result<Value, Error> { pub fn product((array,): (Vec<Number>,)) -> Result<Value, Error> {

View file

@ -2428,6 +2428,15 @@ async fn function_math_pow() -> Result<(), Error> {
let tmp = res.remove(0).result?; let tmp = res.remove(0).result?;
let val = Value::from(1045678.375); let val = Value::from(1045678.375);
assert_eq!(tmp, val); assert_eq!(tmp, val);
let sql = r#"
RETURN math::pow(101, 50);
"#;
let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 1);
let res = res.remove(0).result;
assert!(matches!(res, Err(Error::TryPow(_, _))));
// //
Ok(()) Ok(())
} }