Skip to content

Commit

Permalink
[[FIX]] Disallow invalid numeric literals
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed Jul 29, 2020
1 parent 17d8f5a commit b02a025
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
37 changes: 24 additions & 13 deletions src/lex.js
Expand Up @@ -752,6 +752,7 @@ Lexer.prototype = {
var isAllowedDigit = isDecimalDigit;
var base = 10;
var isLegacy = false;
var isNonOctal = false;

function isDecimalDigit(str) {
return (/^[0-9]$/).test(str);
Expand All @@ -761,6 +762,10 @@ Lexer.prototype = {
return (/^[0-7]$/).test(str);
}

function isNonOctalDigit(str) {
return str === "8" || str === "9";
}

function isBinaryDigit(str) {
return (/^[01]$/).test(str);
}
Expand Down Expand Up @@ -843,25 +848,22 @@ Lexer.prototype = {
base = 8;
isLegacy = true;

index += 1;
value += char;
}

// Decimal numbers that start with '0' such as '09' are illegal
// but we still parse them and return as malformed.

if (!isOctalDigit(char) && isDecimalDigit(char)) {
index += 1;
value += char;
} else if (isDecimalDigit(char)) {
isNonOctal = true;
}
}

while (index < length) {
char = this.peek(index);

// Numbers like '019' (note the 9) are not valid octals
// but we still parse them and mark as malformed.
if (!(isLegacy && isDecimalDigit(char)) && !isAllowedDigit(char)) {
if (isLegacy && isNonOctalDigit(char)) {
base = 10;
isLegacy = false;
isNonOctal = true;
isAllowedDigit = isDecimalDigit;
}

if (!isAllowedDigit(char)) {
break;
}
value += char;
Expand Down Expand Up @@ -978,6 +980,7 @@ Lexer.prototype = {
type: Token.NumericLiteral,
value: value,
base: base,
isNonOctal: isNonOctal,
isMalformed: false
};
},
Expand Down Expand Up @@ -2174,6 +2177,14 @@ Lexer.prototype = {
return state.isStrict() && token.base === 8 && token.isLegacy;
});

this.triggerAsync("error", {
code: "E068",
line: this.line,
character: this.char
}, checks, function() {
return state.isStrict() && token.isNonOctal;
});

this.trigger("Number", {
line: this.line,
char: this.char,
Expand Down
3 changes: 2 additions & 1 deletion src/messages.js
Expand Up @@ -83,7 +83,8 @@ var errors = {
E065: "Functions defined outside of strict mode with non-simple parameter lists may not " +
"enable strict mode.",
E066: "Asynchronous iteration is only available with for-of loops.",
E067: "Malformed numeric literal: '{a}'."
E067: "Malformed numeric literal: '{a}'.",
E068: "Decimals with leading zeros are not allowed in strict mode."
};

var warnings = {
Expand Down
1 change: 0 additions & 1 deletion tests/test262/expectations.txt
Expand Up @@ -145,7 +145,6 @@ test/language/identifiers/vals-rus-alpha-upper-via-escape-hex.js(default)
test/language/identifiers/vals-rus-alpha-upper-via-escape-hex.js(strict mode)
test/language/arguments-object/10.6-13-c-1-s.js(strict mode)
test/language/arguments-object/10.6-2gs.js(strict mode)
test/language/literals/numeric/non-octal-decimal-integer-strict.js(strict mode)
test/language/block-scope/syntax/function-declarations/in-statement-position-if-expression-statement-else-statement.js(strict mode)
test/language/block-scope/syntax/function-declarations/in-statement-position-if-expression-statement.js(strict mode)
test/language/destructuring/binding/syntax/array-elements-with-initializer.js(default)
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/parser.js
Expand Up @@ -568,6 +568,30 @@ exports.numbers = function (test) {
"}());"
]);

TestRun(test)
.test([
"void 08;",
"void 0181;"
]);

TestRun(test)
.addError(3, 10, "Decimals with leading zeros are not allowed in strict mode.")
.test([
"(function () {",
"'use strict';",
"return 08;",
"}());"
]);

TestRun(test)
.addError(3, 12, "Decimals with leading zeros are not allowed in strict mode.")
.test([
"(function () {",
"'use strict';",
"return 0181;",
"}());"
]);

// GitHub #751 - an expression containing a number with a leading decimal point should be parsed in its entirety
TestRun(test)
.addError(1, 11, "A leading decimal point can be confused with a dot: '.3'.")
Expand Down

0 comments on commit b02a025

Please sign in to comment.