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

IndexOutOfRange exception in StringExtensions.TrimFirstLine #73

Closed
MaceWindu opened this issue Oct 23, 2015 · 5 comments
Closed

IndexOutOfRange exception in StringExtensions.TrimFirstLine #73

MaceWindu opened this issue Oct 23, 2015 · 5 comments

Comments

@MaceWindu
Copy link

System.IndexOutOfRangeException occurred
  StackTrace:
       at System.Text.StringBuilder.get_Chars(Int32 index)
       at ExCSS.Model.Extensions.StringExtensions.TrimFirstLine(StringBuilder builder)
       at ExCSS.StyleSheet.ToString(Boolean friendlyFormat, Int32 indentation)
       at ExCSS.StyleSheet.ToString()

public static StringBuilder TrimFirstLine(this StringBuilder builder)
        {
            if (builder.Length > 0) // this check must be moved to "while"
            {
                while (builder[0] == '\r' || builder[0] == '\n' || builder[0] == '\t')
                {
                    builder.Remove(0, 1);
                }
            }

            return builder;
        }
@TylerBrinks
Copy link
Owner

Can you provide any detail on what was being parsed? There's not enough detail here to understand what happened.

@MaceWindu
Copy link
Author

Well, I was testing different inputs and it was failing like that on latest released version on empty string (string.Empty).
On current code base it doesn't fail due to some other changes since last release, so I have no inputs to reproduce it now using parser. But you still can reproduce it using isolated test for this extension method (and TrimLastLine):

new StringBuilder("\r\n").TrimLastLine();
new StringBuilder("\r\n").TrimFirstLine();

@MaceWindu
Copy link
Author

Looks like #64 tried to fix the same issue, but introduced this error.

@1nTheZ0ne
Copy link

1nTheZ0ne commented May 4, 2016

I can confirm that this exception is being thrown when the input string is empty, resulting in every collection in ExCSS.StyleSheet having a count of zero.

var parser = new ExCSS.Parser();
var stylesheet = parser.Parse(string.Empty);
var css = stylesheet.ToString(true); // throws IndexOutOfRangeException

@TylerBrinks
Copy link
Owner

Looking into it.

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