-
Notifications
You must be signed in to change notification settings - Fork 129
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
Avoid parsing to non-finite floating point numbers #167
Comments
FWIW, in librsvg I ended up with this: pub trait CssParserExt {
fn expect_finite_number(&mut self) -> Result<f32, ValueErrorKind>;
}
impl<'i, 't> CssParserExt for Parser<'i, 't> {
fn expect_finite_number(&mut self) -> Result<f32, ValueErrorKind> {
finite_f32(self.expect_number()?)
}
}
pub fn finite_f32(n: f32) -> Result<f32, ValueErrorKind> {
if n.is_finite() {
Ok(n)
} else {
Err(ValueErrorKind::Value("expected finite number".to_string()))
}
} Of course this is tied to librsvg's error enums. The code uses |
I guess the question is, should encountering a non-finite number just yield an UnexpectedToken error? I don't know the details of the CSS spec well enough :) |
https://drafts.csswg.org/css-values/#numeric-ranges
Unfortunately I couldn’t easily find spec text that says what to do with inputs that exceed that chosen capacity. |
I think we should parse it, and clamp it to some appropriate range. We should probably just not serialize |
Probably when we started using the fn main() {
dbg!(dtoa_s(std::f32::INFINITY));
dbg!(dtoa_s(std::f32::NAN));
dbg!(dtoa_s(std::f32::MAX));
}
fn dtoa_s(x: f32) -> String {
let mut v = Vec::<u8>::new();
dtoa::write(&mut v, x).unwrap();
String::from_utf8(v).unwrap()
} Playground, output:
|
So, "too large" -> "parse error" sounds reasonable. But section 10.9 has an interesting note:
So clamping to $large_value sounds fine too! If that makes number values rountrippable, this sounds good. |
Parse errors seems like it might not be web-compatible at least for |
In #165, this input
3E833
is parsed as aToken::Number
whose value isstd::f32::INFINITY
, which serializes asinf
.We should definitely not serialize any number as
inf
(maybe an arbitrary large number instead), but probably also avoid non-finite numbers in tokens in the first place.CC servo/servo#15729
The text was updated successfully, but these errors were encountered: