From 279d87cff6f1d666141cfe631e73ea9410cb531c Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Tue, 10 Dec 2024 16:26:25 -0500 Subject: [PATCH 1/5] fix(sub): panic when diffing inf floats --- .../arithmetic/subtraction/nan_result.vrl | 5 +++++ src/compiler/value/arithmetic.rs | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 lib/tests/tests/expressions/arithmetic/subtraction/nan_result.vrl diff --git a/lib/tests/tests/expressions/arithmetic/subtraction/nan_result.vrl b/lib/tests/tests/expressions/arithmetic/subtraction/nan_result.vrl new file mode 100644 index 0000000000..2dbb0e34ba --- /dev/null +++ b/lib/tests/tests/expressions/arithmetic/subtraction/nan_result.vrl @@ -0,0 +1,5 @@ +# result: can't subtract type float from float + +large = to_float!("1.7976931348623157e+308") +inf_float = large + large +inf_float - inf_float diff --git a/src/compiler/value/arithmetic.rs b/src/compiler/value/arithmetic.rs index d51c9e57ef..378bbf316b 100644 --- a/src/compiler/value/arithmetic.rs +++ b/src/compiler/value/arithmetic.rs @@ -1,9 +1,8 @@ #![deny(clippy::arithmetic_side_effects)] -use std::ops::{Add, Mul, Rem, Sub}; +use std::ops::{Add, Mul, Rem}; use bytes::{BufMut, Bytes, BytesMut}; - use crate::compiler::{ value::{Kind, VrlValueConvert}, ExpressionError, @@ -60,6 +59,15 @@ pub trait VrlValueArithmetic: Sized { fn eq_lossy(&self, rhs: &Self) -> bool; } +fn safe_sub(lhv: f64, rhv: f64) -> Option { + let result = lhv - rhv; + if result.is_nan() { + None + } else { + Some(Value::from_f64_or_zero(result)) + } +} + impl VrlValueArithmetic for Value { /// Similar to [`std::ops::Mul`], but fallible (e.g. `TryMul`). fn try_mul(self, rhs: Self) -> Result { @@ -155,9 +163,9 @@ impl VrlValueArithmetic for Value { let rhv = rhs.try_into_i64().map_err(|_| err())?; i64::wrapping_sub(lhv, rhv).into() } - Value::Float(lhv) => { - let rhv = rhs.try_into_f64().map_err(|_| err())?; - lhv.sub(rhv).into() + Value::Float(lhs) => { + let rhs = rhs.try_into_f64().map_err(|_| err())?; + safe_sub(*lhs, rhs).ok_or_else(|| err())? } _ => return Err(err()), }; From 1f74279f377d5666cfa8f8b5299a44014617b8ce Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Tue, 10 Dec 2024 16:30:31 -0500 Subject: [PATCH 2/5] cargo fmt --- src/compiler/value/arithmetic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/value/arithmetic.rs b/src/compiler/value/arithmetic.rs index 378bbf316b..3bca2cfa47 100644 --- a/src/compiler/value/arithmetic.rs +++ b/src/compiler/value/arithmetic.rs @@ -2,12 +2,12 @@ use std::ops::{Add, Mul, Rem}; -use bytes::{BufMut, Bytes, BytesMut}; use crate::compiler::{ value::{Kind, VrlValueConvert}, ExpressionError, }; use crate::value::{ObjectMap, Value}; +use bytes::{BufMut, Bytes, BytesMut}; use super::ValueError; From 84c1b3bdc014dede2f0cd8bff44298ca7407215b Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Tue, 10 Dec 2024 16:37:55 -0500 Subject: [PATCH 3/5] clippy fix --- src/compiler/value/arithmetic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/value/arithmetic.rs b/src/compiler/value/arithmetic.rs index 3bca2cfa47..0a59f2c4e6 100644 --- a/src/compiler/value/arithmetic.rs +++ b/src/compiler/value/arithmetic.rs @@ -165,7 +165,7 @@ impl VrlValueArithmetic for Value { } Value::Float(lhs) => { let rhs = rhs.try_into_f64().map_err(|_| err())?; - safe_sub(*lhs, rhs).ok_or_else(|| err())? + safe_sub(*lhs, rhs).ok_or_else(&err)? } _ => return Err(err()), }; From 1a8ee232ef73d993c4944ad0f59fc3da39a0a4c5 Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Tue, 10 Dec 2024 16:59:44 -0500 Subject: [PATCH 4/5] clippy fix --- src/compiler/value/arithmetic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/value/arithmetic.rs b/src/compiler/value/arithmetic.rs index 0a59f2c4e6..80b63b30bc 100644 --- a/src/compiler/value/arithmetic.rs +++ b/src/compiler/value/arithmetic.rs @@ -165,7 +165,7 @@ impl VrlValueArithmetic for Value { } Value::Float(lhs) => { let rhs = rhs.try_into_f64().map_err(|_| err())?; - safe_sub(*lhs, rhs).ok_or_else(&err)? + safe_sub(*lhs, rhs).ok_or_else(err)? } _ => return Err(err()), }; From 8426ab6d884aa35a57e351fa160c718b3f999f5d Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Tue, 10 Dec 2024 17:00:49 -0500 Subject: [PATCH 5/5] changelog --- changelog.d/2893.fix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/2893.fix.md diff --git a/changelog.d/2893.fix.md b/changelog.d/2893.fix.md new file mode 100644 index 0000000000..3eb6b7ae07 --- /dev/null +++ b/changelog.d/2893.fix.md @@ -0,0 +1 @@ +Fix a panic in float subtraction that produces NaN values.