Skip to content

Commit 4b79b39

Browse files
committed
feat: added saveguards for the std::number::log function
1 parent 2500290 commit 4b79b39

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

  • crates/taurus-core/src/runtime/functions

crates/taurus-core/src/runtime/functions/number.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,36 @@ fn log(
521521
Ok(v) => v,
522522
Err(e) => return e,
523523
};
524-
Signal::Success(value_from_f64(value.log(base)))
524+
if !value.is_finite() || !base.is_finite() {
525+
return Signal::Failure(RuntimeError::new(
526+
"T-STD-00001",
527+
"InvalidArgumentRuntimeError",
528+
"Log input and base must be finite numbers",
529+
));
530+
}
531+
if value <= 0.0 {
532+
return Signal::Failure(RuntimeError::new(
533+
"T-STD-00001",
534+
"InvalidArgumentRuntimeError",
535+
"Log input must be greater than zero",
536+
));
537+
}
538+
if base <= 0.0 || base == 1.0 {
539+
return Signal::Failure(RuntimeError::new(
540+
"T-STD-00001",
541+
"InvalidArgumentRuntimeError",
542+
"Log base must be greater than zero and not equal to one",
543+
));
544+
}
545+
let result = value.log(base);
546+
if !result.is_finite() {
547+
return Signal::Failure(RuntimeError::new(
548+
"T-STD-00001",
549+
"InvalidArgumentRuntimeError",
550+
"Log result was not finite",
551+
));
552+
}
553+
Signal::Success(value_from_f64(result))
525554
}
526555

527556
fn ln(
@@ -1086,6 +1115,24 @@ mod tests {
10861115
let mut run = dummy_run;
10871116
let ln1 = expect_num(ln(&[a_num(f64::consts::E)], &mut ctx, &mut run));
10881117
assert!((ln1 - 1.0).abs() < f64::EPSILON);
1118+
1119+
let mut run = dummy_run;
1120+
assert!(matches!(
1121+
log(&[a_num(-100.0), a_num(10.0)], &mut ctx, &mut run),
1122+
Signal::Failure(_)
1123+
));
1124+
1125+
let mut run = dummy_run;
1126+
assert!(matches!(
1127+
log(&[a_num(100.0), a_num(1.0)], &mut ctx, &mut run),
1128+
Signal::Failure(_)
1129+
));
1130+
1131+
let mut run = dummy_run;
1132+
assert!(matches!(
1133+
log(&[a_num(100.0), a_num(0.0)], &mut ctx, &mut run),
1134+
Signal::Failure(_)
1135+
));
10891136
}
10901137

10911138
#[test]

0 commit comments

Comments
 (0)