diff --git a/package/PikaStdLib/PikaStdData_FILEIO.c b/package/PikaStdLib/PikaStdData_FILEIO.c index f6378a577..a0f8cd853 100644 --- a/package/PikaStdLib/PikaStdData_FILEIO.c +++ b/package/PikaStdLib/PikaStdData_FILEIO.c @@ -8,8 +8,11 @@ int PikaStdData_FILEIO_init(PikaObj* self, char* path, char* mode) { /* already initialized */ return 0; } - if (strIsStartWith(path, "pikafs/")) { - pikafs_FILE* f = pikafs_fopen(path + 7, "rb"); + if (strIsStartWith(path, "/pikafs/")) { + pikafs_FILE* f = pikafs_fopen(path + 8, "rb"); + if (f == NULL) { + return 1; + } obj_setInt(self, "pikafs", PIKA_TRUE); obj_setPtr(self, "_f", f); obj_setStr(self, "_mode", mode); diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 0714c6468..8c310a1e1 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -33,7 +33,8 @@ // "--gtest_filter=json.speed" // "--gtest_filter=json.json_issue1" // "--gtest_filter=json.err" - "--gtest_filter=builtin.eval" + // "--gtest_filter=builtin.eval" + "--gtest_filter=stddata.pikafs_open_err" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/.vscode/settings.json b/port/linux/.vscode/settings.json index 0547cb6ab..a69f7adcd 100644 --- a/port/linux/.vscode/settings.json +++ b/port/linux/.vscode/settings.json @@ -133,5 +133,8 @@ }, "[c]": { "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd" + }, + "[jsonc]": { + "editor.defaultFormatter": "vscode.json-language-features" } } \ No newline at end of file diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_FILEIO.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_FILEIO.c index f6378a577..a0f8cd853 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_FILEIO.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_FILEIO.c @@ -8,8 +8,11 @@ int PikaStdData_FILEIO_init(PikaObj* self, char* path, char* mode) { /* already initialized */ return 0; } - if (strIsStartWith(path, "pikafs/")) { - pikafs_FILE* f = pikafs_fopen(path + 7, "rb"); + if (strIsStartWith(path, "/pikafs/")) { + pikafs_FILE* f = pikafs_fopen(path + 8, "rb"); + if (f == NULL) { + return 1; + } obj_setInt(self, "pikafs", PIKA_TRUE); obj_setPtr(self, "_f", f); obj_setStr(self, "_mode", mode); diff --git a/src/PikaCompiler.c b/src/PikaCompiler.c index d2c4920f8..fd13be3ae 100644 --- a/src/PikaCompiler.c +++ b/src/PikaCompiler.c @@ -1080,17 +1080,24 @@ PIKA_RES pikaMaker_linkRaw(PikaMaker* self, char* file_path) { */ pikafs_FILE* pikafs_fopen(char* file_name, char* mode) { pikafs_FILE* f = (pikafs_FILE*)pikaMalloc(sizeof(pikafs_FILE)); + if (NULL == f) { + return NULL; // 避免空指针 + } memset(f, 0, sizeof(pikafs_FILE)); extern volatile PikaObj* __pikaMain; uint8_t* library_bytes = obj_getPtr((PikaObj*)__pikaMain, "@libraw"); if (NULL == library_bytes) { - return NULL; + goto __error; // 如果library_bytes为NULL,则跳转到__error } if (PIKA_RES_OK != _loadModuleDataWithName(library_bytes, file_name, &f->addr, &f->size)) { - return NULL; + goto __error; // 如果_loadModuleDataWithName的结果不是PIKA_RES_OK,则跳转到__error } return f; + +__error: + pikaFree(f, sizeof(pikafs_FILE)); // 释放内存 + return NULL; } pikafs_FILE* pikafs_fopen_pack(char* pack_name, char* file_name) { @@ -1156,7 +1163,7 @@ int pikafs_fwrite(void* buf, size_t size, size_t count, pikafs_FILE* file) { * @return 0 if success */ int pikafs_fclose(pikafs_FILE* file) { - if (file->need_free){ + if (file->need_free) { pikaFree(file->addr, file->size); } pikaFree(file, sizeof(pikafs_FILE)); diff --git a/test/stddata-test.cpp b/test/stddata-test.cpp index 4d8f61f56..7ed8ed7b0 100644 --- a/test/stddata-test.cpp +++ b/test/stddata-test.cpp @@ -540,7 +540,7 @@ TEST(stddata, pikafs_open) { /* run */ __platform_printf("BEGIN\r\n"); obj_run(pikaMain, - "f = open('pikafs/widget_config.ini','r')\n" + "f = open('/pikafs/widget_config.ini','r')\n" "f.read(8)\n" "f.close()\n"); /* collect */ @@ -551,6 +551,26 @@ TEST(stddata, pikafs_open) { EXPECT_EQ(pikaMemNow(), 0); } +TEST(stddata, pikafs_open_err) { + /* init */ + g_PikaMemInfo.heapUsedMax = 0; + PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); + extern unsigned char pikaModules_py_a[]; + obj_linkLibrary(pikaMain, pikaModules_py_a); + /* run */ + __platform_printf("BEGIN\r\n"); + obj_run(pikaMain, + "f = open('/pikafs/_no_file_widget_config.ini','r')\n" + "f.read(8)\n" + "f.close()\n"); + /* collect */ + /* assert */ + // EXPECT_STREQ(log_buff[0], "'[端口]'\r\n"); + /* deinit */ + obj_deinit(pikaMain); + EXPECT_EQ(pikaMemNow(), 0); +} + TEST(stddata, list_slice_issue) { /* init */ g_PikaMemInfo.heapUsedMax = 0;