Skip to content

Commit

Permalink
parser: deprecate legacy octal integer literals
Browse files Browse the repository at this point in the history
Also, deprecate legacy octal escape sequences in strings.

Refs: https://github.com/metarhia/jstp/issues/152
PR-URL: metarhia/jstp#247
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Dmytro Nechai <nechaido@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
  • Loading branch information
belochub committed Jul 21, 2018
1 parent f273459 commit c942e8e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 80 deletions.
95 changes: 29 additions & 66 deletions src/jsrs_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,8 @@ MaybeLocal<Value> ParseNumber(Isolate* isolate,
const char* end,
size_t* size) {
bool negate_result = false;
bool is_noctal = false;
const char* number_start = begin;

MaybeLocal<Value> result;

if (*begin == '+' || *begin == '-') {
negate_result = *begin == '-';
number_start++;
Expand All @@ -324,25 +321,25 @@ MaybeLocal<Value> ParseNumber(Isolate* isolate,
base = 16;
number_start++;
} else if (isdigit(*number_start)) {
is_noctal = true;
result = ParseNoctalNumber(isolate, number_start, end, size,
negate_result);
THROW_EXCEPTION(SyntaxError,
"Legacy octal and non-octal integer literals are not supported");
return MaybeLocal<Value>();
} else {
number_start--;
}
}

if (!is_noctal) {
if (base == 10) {
result = ParseDecimalNumber(isolate, number_start, end, size,
negate_result);
} else {
result = ParseIntegerNumber(isolate, number_start, end, size,
base, negate_result);
if (*size == 0) {
THROW_EXCEPTION(SyntaxError, "Empty number value");
return MaybeLocal<Value>();
}
MaybeLocal<Value> result;

if (base == 10) {
result = ParseDecimalNumber(isolate, number_start, end, size,
negate_result);
} else {
result = ParseIntegerNumber(isolate, number_start, end, size,
base, negate_result);
if (*size == 0) {
THROW_EXCEPTION(SyntaxError, "Empty number value");
return MaybeLocal<Value>();
}
}
*size += number_start - begin;
Expand Down Expand Up @@ -429,55 +426,6 @@ Local<Value> ParseBigIntegerNumber(Isolate* isolate,
return Number::New(isolate, negate_result ? -result : result);
}

MaybeLocal<Value> ParseNoctalNumber(Isolate* isolate,
const char* begin,
const char* end,
size_t* size,
bool negate_result) {
*size = end - begin;
bool is_octal = true;
int64_t result = 0;
bool int32_overflow = false;
double overflow_result = 0.0;
char current_digit;

for (size_t i = 0; i < *size; i++) {
if (!isdigit(begin[i])) {
*size = i;
break;
}

current_digit = begin[i] - '0';
if (current_digit > 7) {
is_octal = false;
}

if (!int32_overflow) {
result *= 10;
result += current_digit;
} else {
overflow_result *= 10.0;
overflow_result += current_digit;
}

if (!int32_overflow && (result < INT32_MIN || INT32_MAX < result)) {
int32_overflow = true;
overflow_result = result;
}
}

if (is_octal) {
THROW_EXCEPTION(SyntaxError, "Use new octal literal syntax");
return MaybeLocal<Value>();
}

if (!int32_overflow) {
return Integer::New(isolate, negate_result ? -result : result);
} else {
return Number::New(isolate, negate_result ? -overflow_result : overflow_result);
}
}

static bool GetControlChar(Isolate* isolate,
const char* str,
size_t* res_len,
Expand Down Expand Up @@ -669,7 +617,22 @@ static bool GetControlChar(Isolate* isolate,
break;
}

case '0': {
if (isdigit(str[1])) {
THROW_EXCEPTION(SyntaxError,
"Decimal digits after \\0 are not allowed in strings");
return false;
}
*write_to = 0;
break;
}

default: {
if ('0' <= str[0] && str[0] <= '7') {
THROW_EXCEPTION(SyntaxError,
"Octal escape sequences are not allowed in strings");
return false;
}
*write_to = str[0];
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/jsrs_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,6 @@ v8::Local<v8::Value> ParseBigIntegerNumber(v8::Isolate* isolate,
int base,
bool negate_result);

// Parses a noctal integer number.
v8::MaybeLocal<v8::Value> ParseNoctalNumber(v8::Isolate* isolate,
const char* begin,
const char* end,
std::size_t* size,
bool negate_result);


} // namespace internal

} // namespace parser
Expand Down
6 changes: 0 additions & 6 deletions test/todo/json5.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ const supportedByUs = {
arrays: [
'leading-comma-array',
'lone-trailing-comma-array'
],
numbers: [
'negative-noctal',
'noctal-with-leading-octal-digit',
'noctal',
'positive-noctal'
]
};

Expand Down

0 comments on commit c942e8e

Please sign in to comment.