From 541b9a974351526d1e07aa3bf6bd5ee93fc90b1a Mon Sep 17 00:00:00 2001 From: lyon Date: Mon, 14 Oct 2024 11:11:54 +0800 Subject: [PATCH] fix parser for ";", support line buffer overflow protect --- port/linux/.vscode/launch.json | 37 +++------------------------------ port/linux/test/module-test.cpp | 8 +++---- src/PikaObj.c | 21 ++++++++++++++++++- src/PikaParser.c | 4 +++- src/PikaVersion.h | 2 +- 5 files changed, 31 insertions(+), 41 deletions(-) diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 9aa4aed9d..57e7b4c51 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,40 +11,9 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - // "--gtest_filter=vm.keyword_2" - // "--gtest_filter=compiler.find_break_point" - // "--gtest_filter=pikaMain.REPL_pdb_set_break" - // "--gtest_filter=vm.subsrc_import", - // "--gtest_filter=vm.run_file_subsrc" - // "--gtest_filter=vm.run_file" - // "--gtest_filter=stddata.encode_decode" - // "--gtest_filter=packtool.packfiles_txt" - // "--gtest_filter=cmodule.class_attr_obj" - // "--gtest_filter=except.try_import_except" - // "--gtest_filter=vm.test_cmodule_import_as" - // "--gtest_filter=vm.subsrc_import" - // "--gtest_filter=event.event_thread3" - // "--gtest_filter=parser.semicolon*" - // "--gtest_filter=time*" - // "--gtest_filter=flashdb.tsdb1" - // "--gtest_filter=flashdb.base" - // "--gtest_filter=jrpc.server" - // "--gtest_filter=jrpc.client" - // "--gtest_filter=jrpc.BlockingRequestBetweenTwoJRPC" - // "--gtest_filter=jrpc.cmd" - // "--gtest_filter=jrpc.exec_get_val" - // "--gtest_filter=jrpc.exec_get_val" - // "--gtest_filter=thread.issue1" - // "--gtest_filter=except.isinstance" - // "--gtest_filter=builtin.isinstance" - // "--gtest_filter=bytes.bytes_split" - // "--gtest_filter=except.dict" - // "--gtest_filter=except.*" - // "--gtest_filter=except.try1" - // "--gtest_filter=except.for_loop" - // "--gtest_filter=builtin.init_raise" - // "--gtest_filter=builtin.strformat" - // "--gtest_filter=except.typeerr" + // "--gtest_filter=module.REPL_big_script" + // "--gtest_filter=parser.input_issue1" + "--gtest_filter=except.raise_type" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/test/module-test.cpp b/port/linux/test/module-test.cpp index be5b95d9e..513d5ba1f 100644 --- a/port/linux/test/module-test.cpp +++ b/port/linux/test/module-test.cpp @@ -504,9 +504,9 @@ char f_getchar(void) { if (n > 0) { return c; } - pika_platform_printf("f_getchar error\r\n"); - pika_assert(0); - return -1; + // pika_platform_printf("f_getchar error\r\n"); + // pika_assert(0); + return EOF; } void pikaScriptShell_withGetchar(PikaObj* self, sh_getchar getchar_fn); } @@ -603,7 +603,7 @@ TEST(module, REPL_big_script) { fclose((FILE*)f_getchar_fp); /* collect */ /* assert */ - EXPECT_STREQ(log_buff[0], + EXPECT_STREQ(log_buff[3], "\r\nError: line buff overflow, please use bigger " "'PIKA_LINE_BUFF_SIZE'\r\n"); /* deinit */ diff --git a/src/PikaObj.c b/src/PikaObj.c index 2255ab6e3..cf8c2b55d 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -1668,6 +1668,14 @@ enum shellCTRL _inner_do_obj_runChar(PikaObj* self, ShellConfig* shell) { char* input_line = NULL; enum shellCTRL ctrl = SHELL_CTRL_CONTINUE; + static uint64_t tick_start_block_input = 0; + if (tick_start_block_input != 0) { + if (pika_platform_get_tick() - tick_start_block_input < 5000) { + return SHELL_CTRL_CONTINUE; + } else { + tick_start_block_input = 0; + } + } if (inputChar == 0x7F) { inputChar = '\b'; } @@ -1762,7 +1770,12 @@ enum shellCTRL _inner_do_obj_runChar(PikaObj* self, pika_platform_printf( "\r\nError: line buff overflow, please use bigger " "'PIKA_LINE_BUFF_SIZE'\r\n"); - ctrl = SHELL_CTRL_EXIT; + ctrl = SHELL_CTRL_CONTINUE; + pika_platform_printf( + "Input is blocked for 5 seconds to protect the " + "kernel...\r\n"); + tick_start_block_input = pika_platform_get_tick(); + pika_platform_printf(">>> "); __clearBuff(shell); goto __exit; } @@ -1981,6 +1994,12 @@ void _do_pikaScriptShell(PikaObj* self, ShellConfig* cfg) { while (1) { inputChar[1] = inputChar[0]; inputChar[0] = _await_getchar(cfg->fn_getchar); +#ifdef __linux + if (inputChar[0] == EOF) { + pika_platform_printf("\r\n"); + return; + } +#endif #if !PIKA_NANO_ENABLE /* run python script */ if (inputChar[0] == '!' && inputChar[1] == '#') { diff --git a/src/PikaParser.c b/src/PikaParser.c index 7f4c2c072..90c1e4dd4 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -2720,7 +2720,9 @@ static char* Suger_semicolon(Args* outbuffs, char* sLine) { sStmtItem = strsAppend(&buffs, sStmtItem, "\n"); sStmtAfter = strsAppend(&buffs, sStmtAfter, sStmtItem); } - sStmtAfter[strGetSize(sStmtAfter) - 1] = '\0'; + if (sStmtAfter[0] != '\0') { + sStmtAfter[strGetSize(sStmtAfter) - 1] = '\0'; + } sStmtAfter = strsCopy(outbuffs, sStmtAfter); strsDeinit(&buffs); return sStmtAfter; diff --git a/src/PikaVersion.h b/src/PikaVersion.h index 2e87bdd30..8d216eedc 100644 --- a/src/PikaVersion.h +++ b/src/PikaVersion.h @@ -2,4 +2,4 @@ #define PIKA_VERSION_MINOR 13 #define PIKA_VERSION_MICRO 4 -#define PIKA_EDIT_TIME "2024/10/13 23:56:45" +#define PIKA_EDIT_TIME "2024/10/14 11:09:44"