fix error in json parser which did not allow 1.2E10 as number

This commit is contained in:
Misko Hevery 2010-11-05 16:41:36 -07:00
parent dc66687149
commit d5e9f38f3d
2 changed files with 12 additions and 5 deletions

View file

@ -101,26 +101,26 @@ function lex(text, parseStringsForObjects){
'_' == ch || ch == '$'; '_' == ch || ch == '$';
} }
function isExpOperator(ch) { function isExpOperator(ch) {
return ch == '-' || ch == '+'; return ch == '-' || ch == '+' || isNumber(ch);
} }
function readNumber() { function readNumber() {
var number = ""; var number = "";
var start = index; var start = index;
while (index < text.length) { while (index < text.length) {
var ch = text.charAt(index); var ch = lowercase(text.charAt(index));
if (ch == '.' || isNumber(ch)) { if (ch == '.' || isNumber(ch)) {
number += ch; number += ch;
} else { } else {
var peekCh = peek(); var peekCh = peek();
if (ch == 'E' && isExpOperator(peekCh)) { if (ch == 'e' && isExpOperator(peekCh)) {
number += ch; number += ch;
} else if (isExpOperator(ch) && } else if (isExpOperator(ch) &&
peekCh && isNumber(peekCh) && peekCh && isNumber(peekCh) &&
number.charAt(number.length - 1) == 'E') { number.charAt(number.length - 1) == 'e') {
number += ch; number += ch;
} else if (isExpOperator(ch) && } else if (isExpOperator(ch) &&
(!peekCh || !isNumber(peekCh)) && (!peekCh || !isNumber(peekCh)) &&
number.charAt(number.length - 1) == 'E') { number.charAt(number.length - 1) == 'e') {
throw 'Lexer found invalid exponential value "' + text + '"'; throw 'Lexer found invalid exponential value "' + text + '"';
} else { } else {
break; break;

View file

@ -109,6 +109,13 @@ describe('json', function(){
expect(fromJson("{neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}")).toEqual({neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}); expect(fromJson("{neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}")).toEqual({neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]});
}); });
it('should parse exponents', function() {
expect(fromJson("{exp:1.2E10}")).toEqual({exp:1.2E10});
expect(fromJson("{exp:1.2E-10}")).toEqual({exp:1.2E-10});
expect(fromJson("{exp:1.2e+10}")).toEqual({exp:1.2E10});
expect(fromJson("{exp:1.2e-10}")).toEqual({exp:1.2E-10});
});
describe('security', function(){ describe('security', function(){
it('should not allow naked expressions', function(){ it('should not allow naked expressions', function(){
expect(function(){fromJson('1+2');}).toThrow("Did not understand '+2' while evaluating '1+2'."); expect(function(){fromJson('1+2');}).toThrow("Did not understand '+2' while evaluating '1+2'.");