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

Fixes #804 Json parsing errors mention JSON.parse in errors #1001

Merged
merged 2 commits into from
Jun 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/Parser/rterrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,14 @@ RT_ERROR_MSG(JSERR_ResolveExportFailed, 5647, "Resolve export %s failed due to c

RT_ERROR_MSG(JSERR_ObjectCoercible, 5648, "", "Cannot convert null or undefined to object", kjstTypeError, 0)
RT_ERROR_MSG(JSERR_SIMDConversion, 5649, "%s: cannot be converted to a number", "Cannot be converted to a number", kjstTypeError, 0)


// JSON.parse errors. When this happens we want to make it explicitly clear the issue is in JSON.parse and not in the code
RT_ERROR_MSG(JSERR_JsonSyntax, 5650, "JSON.parse Error: Unexpected input at position:%s", "JSON.parse syntax error", kjstTypeError, 0)
RT_ERROR_MSG(JSERR_JsonNoColon, 5651,"JSON.parse Error: Expected ':' at position:%s", "JSON.parse syntax error", kjstTypeError, 0)
RT_ERROR_MSG(JSERR_JsonNoRbrack, 5652, "JSON.parse Error: Expected ']' at position:%s", "JSON.parse syntax error", kjstTypeError, 0)
RT_ERROR_MSG(JSERR_JsonNoRcurly, 5653, "JSON.parse Error: Expected '}' at position:%s", "JSON.parse syntax error", kjstTypeError, 0)
RT_ERROR_MSG(JSERR_JsonBadNumber, 5654, "JSON.parse Error: Invalid number at position:%s", "JSON.parse syntax error", kjstTypeError, 0)
RT_ERROR_MSG(JSERR_JsonIllegalChar, 5655, "JSON.parse Error: Invalid character at position:%s", "JSON.parse syntax error", kjstTypeError, 0)
RT_ERROR_MSG(JSERR_JsonBadHexDigit, 5656, "JSON.parse Error: Expected hexadecimal digit at position:%s", "JSON.parse syntax error", kjstTypeError, 0)
RT_ERROR_MSG(JSERR_JsonNoStrEnd, 5657, "JSON.parse Error: Unterminated string constant at position:%s", "JSON.parse syntax error", kjstTypeError, 0)
18 changes: 9 additions & 9 deletions lib/Runtime/Library/JSONParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace JSON
Js::Var ret = ParseObject();
if (m_token.tk != tkEOF)
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRsyntax);
m_scanner.ThrowSyntaxError(JSERR_JsonSyntax);
}
return ret;
}
Expand Down Expand Up @@ -232,7 +232,7 @@ namespace JSON
}
else
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRbadNumber);
m_scanner.ThrowSyntaxError(JSERR_JsonBadNumber);
}

case tkLBrack:
Expand Down Expand Up @@ -260,11 +260,11 @@ namespace JSON
Scan();
if(tkRBrack == m_token.tk)
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRillegalChar);
m_scanner.ThrowSyntaxError(JSERR_JsonIllegalChar);
}
}
//check and consume the ending ']'
CheckCurrentToken(tkRBrack, ERRnoRbrack);
CheckCurrentToken(tkRBrack, JSERR_JsonNoRbrack);
return arrayObj;

}
Expand Down Expand Up @@ -311,7 +311,7 @@ namespace JSON
//pick "name"
if(tkStrCon != m_token.tk)
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRsyntax);
m_scanner.ThrowSyntaxError(JSERR_JsonIllegalChar);
}

// currentStrLength = length w/o null-termination
Expand All @@ -332,7 +332,7 @@ namespace JSON
//check and consume ":"
if(Scan() != tkColon )
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRnoColon);
m_scanner.ThrowSyntaxError(JSERR_JsonNoColon);
}
Scan();

Expand Down Expand Up @@ -365,7 +365,7 @@ namespace JSON
//check and consume ":"
if(Scan() != tkColon )
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRnoColon);
m_scanner.ThrowSyntaxError(JSERR_JsonNoColon);
}
Scan();
Js::Var value = ParseObject();
Expand Down Expand Up @@ -404,12 +404,12 @@ namespace JSON
}

// check and consume the ending '}"
CheckCurrentToken(tkRCurly, ERRnoRcurly);
CheckCurrentToken(tkRCurly, JSERR_JsonNoRcurly);
return object;
}

default:
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRsyntax);
m_scanner.ThrowSyntaxError(JSERR_JsonSyntax);
}
}
} // namespace JSON
2 changes: 1 addition & 1 deletion lib/Runtime/Library/JSONParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace JSON
void CheckCurrentToken(int tk, int wErr)
{
if (m_token.tk != tk)
Js::JavascriptError::ThrowSyntaxError(scriptContext, wErr);
m_scanner.ThrowSyntaxError(wErr);
Scan();
}

Expand Down
34 changes: 17 additions & 17 deletions lib/Runtime/Library/JSONScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ namespace JSON
const char16* saveCurrentChar = currentChar;
if(!IsJSONNumber())
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRbadNumber);
ThrowSyntaxError(JSERR_JsonBadNumber);
}
currentChar = saveCurrentChar;
double val;
const char16* end;
val = Js::NumberUtilities::StrToDbl(currentChar, &end, scriptContext);
if(currentChar == end)
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRbadNumber);
ThrowSyntaxError(JSERR_JsonBadNumber);
}
AssertMsg(!Js::JavascriptNumber::IsNan(val), "Bad result from string to double conversion");
pToken->tk = tkFltCon;
Expand Down Expand Up @@ -121,7 +121,7 @@ namespace JSON
currentChar += 3;
return (pToken->tk = tkNULL);
}
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRillegalChar);
ThrowSyntaxError(JSERR_JsonIllegalChar);

case 't':
//check for 'true'
Expand All @@ -130,7 +130,7 @@ namespace JSON
currentChar += 3;
return (pToken->tk = tkTRUE);
}
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRillegalChar);
ThrowSyntaxError(JSERR_JsonIllegalChar);

case 'f':
//check for 'false'
Expand All @@ -139,7 +139,7 @@ namespace JSON
currentChar += 4;
return (pToken->tk = tkFALSE);
}
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRillegalChar);
ThrowSyntaxError(JSERR_JsonIllegalChar);

case '{':
return (pToken->tk = tkLCurly);
Expand All @@ -148,7 +148,7 @@ namespace JSON
return (pToken->tk = tkRCurly);

default:
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRillegalChar);
ThrowSyntaxError(JSERR_JsonIllegalChar);
}

}
Expand Down Expand Up @@ -245,28 +245,28 @@ namespace JSON
else if (ch <= 0x1F)
{
//JSON doesn't accept \u0000 - \u001f range, LS(\u2028) and PS(\u2029) are ok
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRillegalChar);
ThrowSyntaxError(JSERR_JsonIllegalChar);
}
else if ( 0 == ch )
{
currentChar--;
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRnoStrEnd);
ThrowSyntaxError(JSERR_JsonNoStrEnd);
}
else if ('\\' == ch)
{
//JSON escape sequence in a string \", \/, \\, \b, \f, \n, \r, \t, unicode seq
// unlikely V5.8 regular chars are not escaped, i.e '\g'' in a string is illegal not 'g'
if (currentChar >= inputText + inputLen )
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRnoStrEnd);
ThrowSyntaxError(JSERR_JsonNoStrEnd);
}

ch = ReadNextChar();
switch (ch)
{
case 0:
currentChar--;
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRnoStrEnd);
ThrowSyntaxError(JSERR_JsonNoStrEnd);

case '"':
case '/':
Expand Down Expand Up @@ -301,30 +301,30 @@ namespace JSON
if (currentChar + 3 >= inputText + inputLen)
{
//no room left for 4 hex chars
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRnoStrEnd);
ThrowSyntaxError(JSERR_JsonNoStrEnd);

}
if (!Js::NumberUtilities::FHexDigit((WCHAR)ReadNextChar(), &tempHex))
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRbadHexDigit);
ThrowSyntaxError(JSERR_JsonBadHexDigit);
}
chcode = tempHex * 0x1000;

if (!Js::NumberUtilities::FHexDigit((WCHAR)ReadNextChar(), &tempHex))
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRbadHexDigit);
ThrowSyntaxError(JSERR_JsonBadHexDigit);
}
chcode += tempHex * 0x0100;

if (!Js::NumberUtilities::FHexDigit((WCHAR)ReadNextChar(), &tempHex))
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRbadHexDigit);
ThrowSyntaxError(JSERR_JsonBadHexDigit);
}
chcode += tempHex * 0x0010;

if (!Js::NumberUtilities::FHexDigit((WCHAR)ReadNextChar(), &tempHex))
{
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRbadHexDigit);
ThrowSyntaxError(JSERR_JsonBadHexDigit);
}
chcode += tempHex;
AssertMsg(chcode == (chcode & 0xFFFF), "Bad unicode code");
Expand All @@ -334,7 +334,7 @@ namespace JSON

default:
// Any other '\o' is an error in JSON
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRillegalChar);
ThrowSyntaxError(JSERR_JsonIllegalChar);
}

// flush
Expand Down Expand Up @@ -367,7 +367,7 @@ namespace JSON
if (!endFound)
{
// no ending '"' found
Js::JavascriptError::ThrowSyntaxError(scriptContext, ERRnoStrEnd);
ThrowSyntaxError(JSERR_JsonNoStrEnd);
}

if (isStringDirectInputTextMapped == false)
Expand Down
11 changes: 9 additions & 2 deletions lib/Runtime/Library/JSONScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ namespace JSON
::Js::ScriptContext* sc, const char16* current, ArenaAllocator* allocator);

void Finalizer();
char16* GetCurrentString(){return currentString;}
uint GetCurrentStringLen(){return currentIndex;}
char16* GetCurrentString() { return currentString; }
uint GetCurrentStringLen() { return currentIndex; }
uint GetScanPosition() { return uint(currentChar - inputText); }

void __declspec(noreturn) ThrowSyntaxError(int wErr)
{
char16 scanPos[16];
::_itow_s(GetScanPosition(), scanPos, _countof(scanPos) / sizeof(char16), 10);
Js::JavascriptError::ThrowSyntaxError(scriptContext, wErr, scanPos);
}

private:

Expand Down
Loading