1
Vote

XmlDiffPatch - NormalizeText on whitespace-only string

description

This was something I found and fixed in my personal copy of XmlDiffPatch a long time ago, but didn't realize that XmlDiffPatch was still "active" somewhere.
 
In XmlDiff.cs, NormalizeText method, the possibility exists for an IndexOutOfRangeException when trying to return a normalized string that consists only of whitespace.
 
In this method, if the string only consisted of whitespace (not blank), i would never be incremented. As a result, upon reaching the following block of code, i is decremented (to -1) and then used as the index into a string.
 
            if (j == 0)
                return string.Empty;
 
            if ( IsWhitespace( chars[j-1] ) )
                i--;
 
            return new string( chars, 0, i );
 
The simplest solution is to add i to the test in the first line listed, as follows:
 
            if ((j == 0) || (i == 0))
                return string.Empty;
 
            if ( IsWhitespace( chars[j-1] ) )
                i--;
 
            return new string( chars, 0, i );

comments