-Fixed potential error when writing escaped unicode character
This commit is contained in:
@@ -60,6 +60,18 @@ namespace Newtonsoft.Json.Tests
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JsonConvertTest : TestFixtureBase
|
public class JsonConvertTest : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
[Test]
|
||||||
|
public void ToStringEnsureEscapedArrayLength()
|
||||||
|
{
|
||||||
|
const char nonAsciiChar = (char)257;
|
||||||
|
const char escapableNonQuoteAsciiChar = '\0';
|
||||||
|
|
||||||
|
string value = nonAsciiChar + @"\" + escapableNonQuoteAsciiChar;
|
||||||
|
|
||||||
|
string convertedValue = JsonConvert.ToString((object)value);
|
||||||
|
Assert.AreEqual(@"""" + nonAsciiChar + @"\\\u0000""", convertedValue);
|
||||||
|
}
|
||||||
|
|
||||||
public class PopulateTestObject
|
public class PopulateTestObject
|
||||||
{
|
{
|
||||||
public decimal Prop { get; set; }
|
public decimal Prop { get; set; }
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
internal static readonly bool[] DoubleQuoteCharEscapeFlags = new bool[128];
|
internal static readonly bool[] DoubleQuoteCharEscapeFlags = new bool[128];
|
||||||
internal static readonly bool[] HtmlCharEscapeFlags = new bool[128];
|
internal static readonly bool[] HtmlCharEscapeFlags = new bool[128];
|
||||||
|
|
||||||
|
private const int UnicodeTextLength = 6;
|
||||||
|
|
||||||
static JavaScriptUtils()
|
static JavaScriptUtils()
|
||||||
{
|
{
|
||||||
IList<char> escapeChars = new List<char>
|
IList<char> escapeChars = new List<char>
|
||||||
@@ -159,8 +161,8 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (writeBuffer == null)
|
if (writeBuffer == null || writeBuffer.Length < UnicodeTextLength)
|
||||||
writeBuffer = new char[6];
|
writeBuffer = new char[UnicodeTextLength];
|
||||||
|
|
||||||
StringUtils.ToCharAsUnicode(c, writeBuffer);
|
StringUtils.ToCharAsUnicode(c, writeBuffer);
|
||||||
|
|
||||||
@@ -182,8 +184,8 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
if (i > lastWritePosition)
|
if (i > lastWritePosition)
|
||||||
{
|
{
|
||||||
int length = i - lastWritePosition + ((isEscapedUnicodeText) ? 6 : 0);
|
int length = i - lastWritePosition + ((isEscapedUnicodeText) ? UnicodeTextLength : 0);
|
||||||
int start = (isEscapedUnicodeText) ? 6 : 0;
|
int start = (isEscapedUnicodeText) ? UnicodeTextLength : 0;
|
||||||
|
|
||||||
if (writeBuffer == null || writeBuffer.Length < length)
|
if (writeBuffer == null || writeBuffer.Length < length)
|
||||||
{
|
{
|
||||||
@@ -192,7 +194,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
// the unicode text is already in the buffer
|
// the unicode text is already in the buffer
|
||||||
// copy it over when creating new buffer
|
// copy it over when creating new buffer
|
||||||
if (isEscapedUnicodeText)
|
if (isEscapedUnicodeText)
|
||||||
Array.Copy(writeBuffer, newBuffer, 6);
|
Array.Copy(writeBuffer, newBuffer, UnicodeTextLength);
|
||||||
|
|
||||||
writeBuffer = newBuffer;
|
writeBuffer = newBuffer;
|
||||||
}
|
}
|
||||||
@@ -207,7 +209,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
if (!isEscapedUnicodeText)
|
if (!isEscapedUnicodeText)
|
||||||
writer.Write(escapedValue);
|
writer.Write(escapedValue);
|
||||||
else
|
else
|
||||||
writer.Write(writeBuffer, 0, 6);
|
writer.Write(writeBuffer, 0, UnicodeTextLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastWritePosition == 0)
|
if (lastWritePosition == 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user