Skip to content

Commit 6dc52bf

Browse files
Add missed checks for errors in ngx_(js|qjs)_exception().
1 parent 82a588c commit 6dc52bf

File tree

3 files changed

+35
-67
lines changed

3 files changed

+35
-67
lines changed

nginx/ngx_js.c

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,7 @@ ngx_njs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
681681
engine->u.njs.vm = vm;
682682

683683
if (njs_vm_start(vm, njs_value_arg(&retval)) == NJS_ERROR) {
684-
ngx_js_exception(vm, &exception);
685-
686-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0, "js exception: %V", &exception);
684+
ngx_js_exception(vm, ctx->log, "exception", &exception);
687685

688686
njs_vm_destroy(vm);
689687

@@ -719,10 +717,7 @@ ngx_engine_njs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
719717
ret = njs_vm_invoke(vm, func, njs_value_arg(args), nargs,
720718
njs_value_arg(&ctx->retval));
721719
if (ret == NJS_ERROR) {
722-
ngx_js_exception(vm, &exception);
723-
724-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
725-
"js exception: %V", &exception);
720+
ngx_js_exception(vm, ctx->log, "exception", &exception);
726721

727722
return NGX_ERROR;
728723
}
@@ -731,10 +726,8 @@ ngx_engine_njs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
731726
ret = njs_vm_execute_pending_job(vm);
732727
if (ret <= NJS_OK) {
733728
if (ret == NJS_ERROR) {
734-
ngx_js_exception(vm, &exception);
729+
ngx_js_exception(vm, ctx->log, "exception", &exception);
735730

736-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
737-
"js job exception: %V", &exception);
738731
return NGX_ERROR;
739732
}
740733

@@ -798,9 +791,8 @@ ngx_engine_njs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
798791
}
799792

800793
if (ngx_js_unhandled_rejection(ctx)) {
801-
ngx_js_exception(e->u.njs.vm, &exception);
802-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
803-
"js unhandled rejection: %V", &exception);
794+
ngx_js_exception(e->u.njs.vm, ctx->log, "unhandled rejection",
795+
&exception);
804796
}
805797
}
806798

@@ -860,8 +852,7 @@ ngx_engine_qjs_compile(ngx_js_loc_conf_t *conf, ngx_log_t *log, u_char *start,
860852
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
861853

862854
if (JS_IsException(code)) {
863-
ngx_qjs_exception(engine, &text);
864-
ngx_log_error(NGX_LOG_EMERG, log, 0, "js compile %V", &text);
855+
ngx_qjs_exception(engine, log, "compile", &text);
865856
return NGX_ERROR;
866857
}
867858

@@ -983,10 +974,8 @@ ngx_qjs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
983974
rv = JS_ReadObject(cx, pc[i].code, pc[i].code_size,
984975
JS_READ_OBJ_BYTECODE);
985976
if (JS_IsException(rv)) {
986-
ngx_qjs_exception(engine, &exception);
987-
988-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
989-
"js load module exception: %V", &exception);
977+
ngx_qjs_exception(engine, ctx->log, "load module exception",
978+
&exception);
990979
goto destroy;
991980
}
992981

@@ -1004,19 +993,13 @@ ngx_qjs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
1004993
rv = JS_EvalFunction(cx, rv);
1005994

1006995
if (JS_IsException(rv)) {
1007-
ngx_qjs_exception(engine, &exception);
1008-
1009-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0, "js eval exception: %V",
1010-
&exception);
996+
ngx_qjs_exception(engine, ctx->log, "eval exception", &exception);
1011997
goto destroy;
1012998
}
1013999

10141000
rv = js_std_await(cx, rv);
10151001
if (JS_IsException(rv)) {
1016-
ngx_qjs_exception(engine, &exception);
1017-
1018-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0, "js eval exception: %V",
1019-
&exception);
1002+
ngx_qjs_exception(engine, ctx->log, "eval exception", &exception);
10201003
goto destroy;
10211004
}
10221005

@@ -1058,10 +1041,7 @@ ngx_engine_qjs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
10581041
val = JS_Call(cx, fn, JS_UNDEFINED, nargs, &ngx_qjs_arg(args[0]));
10591042
JS_FreeValue(cx, fn);
10601043
if (JS_IsException(val)) {
1061-
ngx_qjs_exception(ctx->engine, &exception);
1062-
1063-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1064-
"js call exception: %V", &exception);
1044+
ngx_qjs_exception(ctx->engine, ctx->log, "call exception", &exception);
10651045

10661046
return NGX_ERROR;
10671047
}
@@ -1075,10 +1055,7 @@ ngx_engine_qjs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
10751055
rc = JS_ExecutePendingJob(rt, &cx1);
10761056
if (rc <= 0) {
10771057
if (rc == -1) {
1078-
ngx_qjs_exception(ctx->engine, &exception);
1079-
1080-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1081-
"js job exception: %V", &exception);
1058+
ngx_qjs_exception(ctx->engine, ctx->log, "job exception", &exception);
10821059

10831060
return NGX_ERROR;
10841061
}
@@ -1156,9 +1133,7 @@ ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
11561133
if (ctx != NULL) {
11571134
ret = qjs_call_exit_hook(cx);
11581135
if (JS_IsException(ret)) {
1159-
ngx_qjs_exception(e, &exception);
1160-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1161-
"js exit hook exception: %V", &exception);
1136+
ngx_qjs_exception(e, ctx->log, "exit hook exception", &exception);
11621137
}
11631138

11641139
node = njs_rbtree_min(&ctx->waiting_events);
@@ -1175,9 +1150,7 @@ ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
11751150
}
11761151

11771152
if (ngx_qjs_unhandled_rejection(ctx)) {
1178-
ngx_qjs_exception(e, &exception);
1179-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1180-
"js unhandled rejection: %V", &exception);
1153+
ngx_qjs_exception(e, ctx->log, "unhandled rejection", &exception);
11811154
}
11821155

11831156
JS_SetHostPromiseRejectionTracker(JS_GetRuntime(cx), NULL, NULL);
@@ -1412,10 +1385,7 @@ ngx_qjs_call(JSContext *cx, JSValue fn, JSValue *argv, int argc)
14121385

14131386
ret = JS_Call(cx, fn, JS_UNDEFINED, argc, argv);
14141387
if (JS_IsException(ret)) {
1415-
ngx_qjs_exception(ctx->engine, &exception);
1416-
1417-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1418-
"js call exception: %V", &exception);
1388+
ngx_qjs_exception(ctx->engine, ctx->log, "call exception", &exception);
14191389

14201390
return NGX_ERROR;
14211391
}
@@ -1428,10 +1398,8 @@ ngx_qjs_call(JSContext *cx, JSValue fn, JSValue *argv, int argc)
14281398
rc = JS_ExecutePendingJob(rt, &cx1);
14291399
if (rc <= 0) {
14301400
if (rc == -1) {
1431-
ngx_qjs_exception(ctx->engine, &exception);
1432-
1433-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1434-
"js job exception: %V", &exception);
1401+
ngx_qjs_exception(ctx->engine, ctx->log, "job exception",
1402+
&exception);
14351403

14361404
return NGX_ERROR;
14371405
}
@@ -1445,15 +1413,19 @@ ngx_qjs_call(JSContext *cx, JSValue fn, JSValue *argv, int argc)
14451413

14461414

14471415
ngx_int_t
1448-
ngx_qjs_exception(ngx_engine_t *e, ngx_str_t *s)
1416+
ngx_qjs_exception(ngx_engine_t *e, ngx_log_t *log, char *txt, ngx_str_t *s)
14491417
{
14501418
JSValue exception;
14511419

14521420
exception = JS_GetException(e->u.qjs.ctx);
14531421
if (ngx_qjs_dump_obj(e, exception, s) != NGX_OK) {
1422+
ngx_log_error(NGX_LOG_ERR, log, 0, "js can't get %s", txt);
1423+
14541424
return NGX_ERROR;
14551425
}
14561426

1427+
ngx_log_error(NGX_LOG_ERR, log, 0, "js %s: %V", txt, s);
1428+
14571429
JS_FreeValue(e->u.qjs.ctx, exception);
14581430

14591431
return NGX_OK;
@@ -2245,12 +2217,11 @@ ngx_js_call(njs_vm_t *vm, njs_function_t *func, njs_opaque_value_t *args,
22452217

22462218
ret = njs_vm_call(vm, func, njs_value_arg(args), nargs);
22472219
if (ret == NJS_ERROR) {
2248-
ngx_js_exception(vm, &exception);
22492220

22502221
c = ngx_external_connection(vm, njs_vm_external_ptr(vm));
22512222

2252-
ngx_log_error(NGX_LOG_ERR, c->log, 0,
2253-
"js exception: %V", &exception);
2223+
ngx_js_exception(vm, c->log, "exception", &exception);
2224+
22542225
return NGX_ERROR;
22552226
}
22562227

@@ -2260,10 +2231,8 @@ ngx_js_call(njs_vm_t *vm, njs_function_t *func, njs_opaque_value_t *args,
22602231
c = ngx_external_connection(vm, njs_vm_external_ptr(vm));
22612232

22622233
if (ret == NJS_ERROR) {
2263-
ngx_js_exception(vm, &exception);
2234+
ngx_js_exception(vm, c->log, "job exception", &exception);
22642235

2265-
ngx_log_error(NGX_LOG_ERR, c->log, 0,
2266-
"js job exception: %V", &exception);
22672236
return NGX_ERROR;
22682237
}
22692238

@@ -2276,19 +2245,22 @@ ngx_js_call(njs_vm_t *vm, njs_function_t *func, njs_opaque_value_t *args,
22762245

22772246

22782247
ngx_int_t
2279-
ngx_js_exception(njs_vm_t *vm, ngx_str_t *s)
2248+
ngx_js_exception(njs_vm_t *vm, ngx_log_t *log, char *txt, ngx_str_t *s)
22802249
{
22812250
njs_int_t ret;
22822251
njs_str_t str;
22832252

22842253
ret = njs_vm_exception_string(vm, &str);
22852254
if (ret != NJS_OK) {
2255+
ngx_log_error(NGX_LOG_ERR, log, 0, "js can't get %s", txt);
22862256
return NGX_ERROR;
22872257
}
22882258

22892259
s->data = str.start;
22902260
s->len = str.length;
22912261

2262+
ngx_log_error(NGX_LOG_ERR, log, 0, "js %s: %V", txt, s);
2263+
22922264
return NGX_OK;
22932265
}
22942266

nginx/ngx_js.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ void ngx_js_ctx_init(ngx_js_ctx_t *ctx, ngx_log_t *log);
327327
void ngx_js_ctx_destroy(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *conf);
328328
ngx_int_t ngx_js_call(njs_vm_t *vm, njs_function_t *func,
329329
njs_opaque_value_t *args, njs_uint_t nargs);
330-
ngx_int_t ngx_js_exception(njs_vm_t *vm, ngx_str_t *s);
330+
ngx_int_t ngx_js_exception(njs_vm_t *vm, ngx_log_t *log, char *txt,
331+
ngx_str_t *s);
331332
ngx_engine_t *ngx_njs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf,
332333
void *external);
333334

@@ -362,7 +363,8 @@ void ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
362363
ngx_js_loc_conf_t *conf);
363364
ngx_int_t ngx_qjs_call(JSContext *cx, JSValue function, JSValue *argv,
364365
int argc);
365-
ngx_int_t ngx_qjs_exception(ngx_engine_t *e, ngx_str_t *s);
366+
ngx_int_t ngx_qjs_exception(ngx_engine_t *e, ngx_log_t *log, char *txt,
367+
ngx_str_t *s);
366368
ngx_int_t ngx_qjs_integer(JSContext *cx, JSValueConst val, ngx_int_t *n);
367369
ngx_int_t ngx_qjs_string(JSContext *cx, ngx_pool_t *pool, JSValueConst val,
368370
ngx_str_t *dst);

nginx/ngx_stream_js_module.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,10 +1310,7 @@ ngx_stream_js_run_event(ngx_stream_session_t *s, ngx_stream_js_ctx_t *ctx,
13101310

13111311
if (ret == NJS_ERROR) {
13121312
error:
1313-
ngx_js_exception(vm, &exception);
1314-
1315-
ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %V",
1316-
&exception);
1313+
ngx_js_exception(vm, c->log, "exception", &exception);
13171314

13181315
return NGX_ERROR;
13191316
}
@@ -2751,10 +2748,7 @@ ngx_stream_qjs_run_event(ngx_stream_session_t *s, ngx_stream_js_ctx_t *ctx,
27512748

27522749
if (rc == NGX_ERROR) {
27532750
error:
2754-
ngx_qjs_exception(ctx->engine, &exception);
2755-
2756-
ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %V",
2757-
&exception);
2751+
ngx_qjs_exception(ctx->engine, c->log, "exception", &exception);
27582752

27592753
return NGX_ERROR;
27602754
}

0 commit comments

Comments
 (0)