-5

See the link:

https://i.stack.imgur.com/NH6bX.png

Notice the Autos window at the bottom shows that toParse = "". However toParse != "" evaluates as true anyway, causing the application to crash.

Here's the full method:

public void parseString(string toParse)
    {
        while (toParse != "")
        {
            string nextLine = readLine(ref toParse);

            if (nextLine.IndexOf("//") == 0)
            {
                comments.Add(nextLine);
                continue;
            }

            if (nextLine.IndexOf(".") == 0)
            {
                string[] declarationParts = nextLine.Split(' ');
                string declarationString = declarationParts[0].Substring(1, declarationParts[0].Length - 1);
                declarationString = char.ToUpper(declarationString[0]) + declarationString.Substring(1);
                DirectiveEnum type = (DirectiveEnum)Enum.Parse(typeof(DirectiveEnum), declarationString);
                string[] attributes = declarationParts.Skip(1).ToArray();
                MSILNode newNode = new MSILNode(type);
                newNode.addAttributes(attributes);
                toParse = toParse.Trim();
                if (toParse != "")
                {
                    while (toParse[0] != '{' && toParse[0] != '.' && toParse.IndexOf("//") != 0)
                    {
                        nextLine = readLine(ref toParse);
                        attributes = nextLine.Split(' ');
                        newNode.addAttributes(attributes);
                    }

                    if (toParse[0] == '{')
                    {
                        readLine(ref toParse);
                        string inside = separate(ref toParse, "}");

                        newNode.parseString(inside);
                    }
                }
                subNodes.Add(newNode);
                continue;
            }

            Console.WriteLine(nextLine);
        }
    }

2 Answers2

2

It's difficult to see everything that's happening during the debugging session when only given this one snapshot. However, since toParse is passed by reference to the function readline() (line 57), it's value can be changed in the body of that function.

From the PNG image provided in the original question:

53 if (toParse != "")
54 {
55     while (toParse[0] != '{' && toParse[0] != '.' && toParse.IndexOf("//") != 0)
56     {
57         nextLine = readLine(ref toParse);
58         ...

At line 53, toParse is not empty. Then, during one of the iterations of the while loop, it is updated to an empty value. This would cause any accesses to array indices (i.e. toParse[0] in the while condition) to throw an exception.

For more information on the ref keyword in C#, see this StackOverflow issue or this official Microsoft documentation.

I hope this helps!

Community
  • 1
  • 1
0

In the image, this line newNode.parseString(inside) is highlighted meaning that it's in the Callstack and was called before crashing. This line is probably mutating toParse to be "".

Kafo
  • 3,568
  • 1
  • 12
  • 19
  • I've modified my OP to show the entire method. The first thing I check when calling parseString() is whether or not the passed string is == "". – Fred Heinecke Sep 08 '16 at 03:42