Skip to content

Commit 4090544

Browse files
committed
规避代码不以\0结尾,JS_Eval随机错误的bug
1 parent 198c95f commit 4090544

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

source/PapiQuickjsImpl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,12 @@ pesapi_value pesapi_eval(pesapi_env env, const uint8_t* code, size_t code_size,
778778
{
779779
auto ctx = qjsContextFromPesapiEnv(env);
780780
auto rt = JS_GetRuntime(ctx);
781+
eastl::vector<char, eastl::allocator_malloc> buff;
782+
buff.reserve(code_size + 1);
783+
memcpy(buff.data(), code, code_size);
784+
buff.data()[code_size] = '\0'; // 尽管JS_Eval传了长度,但如果代码没有以\0结尾,JS_Eval会出现随机错误
781785
JS_UpdateStackTop(rt);
782-
JSValue retOrEx = JS_Eval(ctx, (const char *)code, code_size, path, JS_EVAL_TYPE_GLOBAL);
786+
JSValue retOrEx = JS_Eval(ctx, (const char *)buff.data(), code_size, path, JS_EVAL_TYPE_GLOBAL);
783787
if (JS_IsException(retOrEx)) {
784788
auto scope = getCurrentScope(ctx);
785789
scope->setCaughtException(JS_GetException(ctx));

0 commit comments

Comments
 (0)