Skip to content

Commit d619882

Browse files
authored
Accept shell scripts in JS_DetectModule (#358)
- use `skip_shebang` in `JS_DetectModule` before scanning for `import` statements
1 parent 15c6a77 commit d619882

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

quickjs.c

+28-26
Original file line numberDiff line numberDiff line change
@@ -19924,6 +19924,31 @@ static int peek_token(JSParseState *s, BOOL no_line_terminator)
1992419924
return simple_next_token(&p, no_line_terminator);
1992519925
}
1992619926

19927+
static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end)
19928+
{
19929+
const uint8_t *p = *pp;
19930+
int c;
19931+
19932+
if (p[0] == '#' && p[1] == '!') {
19933+
p += 2;
19934+
while (p < buf_end) {
19935+
if (*p == '\n' || *p == '\r') {
19936+
break;
19937+
} else if (*p >= 0x80) {
19938+
c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
19939+
if (c == CP_LS || c == CP_PS) {
19940+
break;
19941+
} else if (c == -1) {
19942+
p++; /* skip invalid UTF-8 */
19943+
}
19944+
} else {
19945+
p++;
19946+
}
19947+
}
19948+
*pp = p;
19949+
}
19950+
}
19951+
1992719952
/* return true if 'input' contains the source of a module
1992819953
(heuristic). 'input' must be a zero terminated.
1992919954

@@ -19934,6 +19959,8 @@ BOOL JS_DetectModule(const char *input, size_t input_len)
1993419959
{
1993519960
const uint8_t *p = (const uint8_t *)input;
1993619961
int tok;
19962+
19963+
skip_shebang(&p, p + input_len);
1993719964
switch(simple_next_token(&p, FALSE)) {
1993819965
case TOK_IMPORT:
1993919966
tok = simple_next_token(&p, FALSE);
@@ -32247,31 +32274,6 @@ JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
3224732274
return JS_EvalFunctionInternal(ctx, fun_obj, ctx->global_obj, NULL, NULL);
3224832275
}
3224932276

32250-
static void skip_shebang(JSParseState *s)
32251-
{
32252-
const uint8_t *p = s->buf_ptr;
32253-
int c;
32254-
32255-
if (p[0] == '#' && p[1] == '!') {
32256-
p += 2;
32257-
while (p < s->buf_end) {
32258-
if (*p == '\n' || *p == '\r') {
32259-
break;
32260-
} else if (*p >= 0x80) {
32261-
c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
32262-
if (c == CP_LS || c == CP_PS) {
32263-
break;
32264-
} else if (c == -1) {
32265-
p++; /* skip invalid UTF-8 */
32266-
}
32267-
} else {
32268-
p++;
32269-
}
32270-
}
32271-
s->buf_ptr = p;
32272-
}
32273-
}
32274-
3227532277
/* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
3227632278
static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3227732279
const char *input, size_t input_len,
@@ -32287,7 +32289,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3228732289
JSModuleDef *m;
3228832290

3228932291
js_parse_init(ctx, s, input, input_len, filename);
32290-
skip_shebang(s);
32292+
skip_shebang(&s->buf_ptr, s->buf_end);
3229132293

3229232294
eval_type = flags & JS_EVAL_TYPE_MASK;
3229332295
m = NULL;

0 commit comments

Comments
 (0)