Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(convert): return error when conversion exceeds f64 bounds #1108

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1107.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes the `to_float` function to return an error instead of `f64::INFINITY` when parsing [non-normal](https://doc.rust-lang.org/std/primitive.f64.html#method.is_normal) numbers.
5 changes: 5 additions & 0 deletions src/compiler/conversion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ impl Conversion {
let parsed = s
.parse::<f64>()
.with_context(|_| FloatParseSnafu { s: s.clone() })?;
if !parsed.is_normal() {
return Err(Error::NanFloat {
s: format!("Invalid float \"{s}\": not a normal f64 number"),
});
}
let f = NotNan::new(parsed).map_err(|_| Error::NanFloat { s: s.to_string() })?;
f.into()
}
Expand Down
34 changes: 33 additions & 1 deletion src/compiler/conversion/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use bytes::Bytes;
use chrono::{DateTime, Utc};
use ordered_float::NotNan;

use crate::compiler::conversion::parse_bool;
use crate::compiler::conversion::{parse_bool, Conversion, Error};
use crate::compiler::TimeZone;

#[cfg(unix)] // see https://github.com/vectordotdev/vector/issues/1201
mod unix;
Expand Down Expand Up @@ -91,3 +92,34 @@ fn parse_bool_errors() {
assert!(parse_bool("yes or no").is_err());
assert!(parse_bool("123.4").is_err());
}

fn convert_float(input: impl ToString) -> Result<StubValue, Error> {
let input = input.to_string();
let converter = Conversion::parse("float", TimeZone::Local).expect("float conversion");
converter.convert::<StubValue>(input.into())
}

#[test]
fn convert_float_ok() {
let max_float = format!("17976931348623157{}", "0".repeat(292));
let min_float = format!("-{max_float}");

assert_eq!(convert_float(max_float), Ok(StubValue::Float(f64::MAX)));
assert_eq!(convert_float("1"), Ok(StubValue::Float(1.0)));
assert_eq!(convert_float("1.23"), Ok(StubValue::Float(1.23)));
assert_eq!(convert_float("-1"), Ok(StubValue::Float(-1.0)));
assert_eq!(convert_float("-1.23"), Ok(StubValue::Float(-1.23)));
assert_eq!(convert_float(min_float), Ok(StubValue::Float(f64::MIN)));
}

#[test]
fn convert_float_errors() {
let exceeds_max_float = format!("17976931348623159{}", "0".repeat(292)); // last number inc by 2
let exceeds_min_float = format!("-{exceeds_max_float}");

assert!(convert_float("abc").is_err());
assert!(convert_float("1.23.4").is_err());
assert!(convert_float(exceeds_max_float).is_err());
assert!(convert_float(exceeds_min_float).is_err());
assert!(convert_float("0.0").is_err());
}
Loading