1
Vote

Octal Escape codes not processed properly.

description

I came across an issue where a function I found on the internet was escaping with \0. Per the standards the octal code range can go from 0 to 377.

This line will cause and exception if you use \0 or \00.

string octalCode = text.Substring(slashIndex + 1, 3);

Here is the fixed code.
                        string octalCode = "";
                        for (int i = 1; i < 4; i++)
                        {
                            if (slashIndex + i > text.Length) break;
                            var c = text[slashIndex + i];
                            if (Char.IsDigit(c)) octalCode += c;
                            else break;
                        }

comments

jtrahan wrote Jul 2, 2012 at 5:46 PM

Here is a revised version. I didn't take into account of the escape char at the end.
                        string octalCode = "";
                        for (int i = 0; i < 3; i++)
                        {
                            if (slashIndex++ > text.Length) break;
                            var c = text[slashIndex];
                            if (Char.IsDigit(c)) octalCode += c;
                            else break;
                        }
                        char octalChar = Latin1.GetChars(new byte[] { System.Convert.ToByte(octalCode, 8) } )[0]; 
                        // insert decoded char
                        sb.Append(octalChar);
                        // skip encoded char
                        // already skipped from above loop ; slashIndex += 4;

jtrahan wrote Jul 2, 2012 at 6:05 PM

actually 1 more revision the index was in the wrong spot.
                        string octalCode = "";
                        slashIndex++;
                        for (int i = 0; i < 3; i++)
                        {
                            if (slashIndex >= text.Length) break;
                            var c = text[slashIndex];
                            if (Char.IsDigit(c)) { octalCode += c; slashIndex++; }
                            else break;
                        }
                        char octalChar = Latin1.GetChars(new byte[] { System.Convert.ToByte(octalCode, 8) } )[0]; 
                        // insert decoded char
                        sb.Append(octalChar);
                        // skip encoded char
                        // already skipped from above loop ; slashIndex += 4;