Skip to content

Commit 6b62098

Browse files
authored
Merge pull request #496 from bptato/fix-hex-access
Fix member access on non-decimal numeric literals
2 parents 8e09295 + 66afb72 commit 6b62098

2 files changed

Lines changed: 16 additions & 17 deletions

File tree

quickjs.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12442,7 +12442,7 @@ static JSValue js_atof(JSContext *ctx, const char *str, const char **pp,
1244212442
to_digit((uint8_t)p[1]) < radix)) {
1244312443
p++;
1244412444
}
12445-
if (!(flags & ATOD_INT_ONLY)) {
12445+
if (!(flags & ATOD_INT_ONLY) && radix == 10) {
1244612446
if (*p == '.' && (p > p_start || to_digit((uint8_t)p[1]) < radix)) {
1244712447
is_float = TRUE;
1244812448
p++;
@@ -12452,9 +12452,7 @@ static JSValue js_atof(JSContext *ctx, const char *str, const char **pp,
1245212452
(*p == sep && to_digit((uint8_t)p[1]) < radix))
1245312453
p++;
1245412454
}
12455-
if (p > p_start &&
12456-
(((*p == 'e' || *p == 'E') && radix == 10) ||
12457-
((*p == 'p' || *p == 'P') && (radix == 2 || radix == 8 || radix == 16)))) {
12455+
if (p > p_start && (*p == 'e' || *p == 'E')) {
1245812456
const char *p1 = p + 1;
1245912457
is_float = TRUE;
1246012458
if (*p1 == '+') {
@@ -12491,19 +12489,9 @@ static JSValue js_atof(JSContext *ctx, const char *str, const char **pp,
1249112489
}
1249212490
buf[j] = '\0';
1249312491

12494-
if (flags & ATOD_ACCEPT_SUFFIX) {
12495-
if (*p == 'n') {
12496-
p++;
12497-
atod_type = ATOD_TYPE_BIG_INT;
12498-
} else {
12499-
if (is_float && radix != 10)
12500-
goto fail;
12501-
}
12502-
} else {
12503-
if (atod_type == ATOD_TYPE_FLOAT64) {
12504-
if (is_float && radix != 10)
12505-
goto fail;
12506-
}
12492+
if ((flags & ATOD_ACCEPT_SUFFIX) && *p == 'n') {
12493+
p++;
12494+
atod_type = ATOD_TYPE_BIG_INT;
1250712495
}
1250812496

1250912497
switch(atod_type) {

tests/test_language.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,16 @@ function test_global_var_opt()
664664
assert(gvar1, 5);
665665
}
666666

667+
function test_number_literals()
668+
{
669+
assert(0.1.a, undefined);
670+
assert(0x1.a, undefined);
671+
assert(0b1.a, undefined);
672+
assert(01.a, undefined);
673+
assert(0o1.a, undefined);
674+
assert_throws(SyntaxError, () => eval('0.a'));
675+
}
676+
667677
test_op1();
668678
test_cvt();
669679
test_eq();
@@ -690,3 +700,4 @@ test_optional_chaining();
690700
test_parse_arrow_function();
691701
test_unicode_ident();
692702
test_global_var_opt();
703+
test_number_literals();

0 commit comments

Comments
 (0)