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

Parsing z-index values outside of int range throws an OverflowException #158

Open
ECrownofFire opened this issue Jun 16, 2023 · 5 comments

Comments

@ECrownofFire
Copy link

For example, any z-index values greater than INT_MAX throws, such as this:

.foo {
    z-index: 99999999999999999;
}

Would also apply to values smaller than INT_MIN.

Stack trace
Unhandled exception. System.OverflowException: Value was either too large or too small for an Int32.
   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, ReadOnlySpan`1 value, TypeCode type)
   at System.Int32.Parse(String s, IFormatProvider provider)
   at ExCSS.NumberToken.get_IntegerValue()
   at ExCSS.ValueExtensions.ToInteger(IEnumerable`1 value)
   at ExCSS.StructValueConverter`1.Convert(IEnumerable`1 value)
   at ExCSS.OrValueConverter.Convert(IEnumerable`1 value)
   at ExCSS.OrValueConverter.Convert(IEnumerable`1 value)
   at ExCSS.OrValueConverter.Convert(IEnumerable`1 value)
   at ExCSS.Property.TrySetValue(TokenValue newTokenValue)
   at ExCSS.StylesheetComposer.CreateDeclarationWith(Func`2 createProperty, Token& token)
   at ExCSS.StylesheetComposer.FillDeclarations(StyleDeclaration style)
   at ExCSS.StylesheetComposer.CreateStyle(Token current)
   at ExCSS.StylesheetComposer.CreateRule(Token token)
   at ExCSS.StylesheetComposer.CreateRules(Stylesheet sheet)
   at ExCSS.StylesheetParser.Parse(TextSource source)
   at ExCSS.StylesheetParser.Parse(String content)
   at [redacted]

The standard doesn't actually limit z-index values to any particular range, though realistically speaking every browser clamps them to Int32.

@chrwei
Copy link

chrwei commented Jan 17, 2024

I'm also encountering this, it's in a rather large 3rd party css file that I don't have direct control over. I'm seeing z-index values of 999999999999999.

I can work-around by replacing those with smaller numbers before parsing, but it would be nice if the library tolerated this.

@TylerBrinks
Copy link
Owner

It's an interesting issue since CSS doesn't allow the value, but browsers handle the incorrect syntax. I'm looking at options on how to handle it since, technically, one could overflow an int64 value as well, but it's never fractional.

@chrwei
Copy link

chrwei commented Jan 17, 2024

I haven't peeked at your code yet, but something like "if length >= 10 and first character > 2". to speed it up maybe a TryParse and only check that on failure?

@TylerBrinks
Copy link
Owner

The most recent version fixes the parsing by first trying to parse the value as an integer (as it did before). If that fails, the string representing the numeric value is scanned to check that all characters are digits. If so, the value is set to int.MaxValue. While not ideal, it does allow the parser to continue without exception. Any non-digit characters will throw an explicit exception. This introduces an edge case where the value is negative and smaller than in.MinValue. That scenario isn't currently handled, but as an edge case should not affect standard parsing behavior.

@chrwei
Copy link

chrwei commented Jan 21, 2024

that sounds great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants