fixed memoryleak in function:_do_pikaMaker_linkCompiledModulesFullPath;fixed bugs for pikafs_fopen_pack, but still have memryleak problem

This commit is contained in:
shanjiayang 2023-04-24 13:28:00 +08:00
parent 38ef36c054
commit ef39e6d48a
4 changed files with 46 additions and 22 deletions

View File

@ -11,8 +11,10 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
//"--gtest_filter=parser.NEW"
// "--gtest_filter=vm.dir_print_arg"
"--gtest_filter=packtool.unpack"
// "--gtest_filter=packtool.unpack"
"--gtest_filter=packtool.packread"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -388,7 +388,7 @@ static int32_t __foreach_handler_libWriteIndex(Arg* argEach, Args* context) {
uint32_t buff_len = strGetSize(module_name);
char* name_buff = (char* )__platform_malloc(5 + buff_len); /* 4 + 1 + buff_len*/
__platform_memset(name_buff, 0x00, buff_len + 1);
module_name = strsReplace(&buffs, module_name, "|", ".");
// module_name = strsReplace(&buffs, module_name, "|", ".");
// pika_platform_printf(" %s:%d\r\n", module_name, bytecode_size);
pika_platform_memcpy(name_buff, &buff_len, 4);
pika_platform_memcpy(name_buff + 4, module_name, buff_len + 1); /* add '\0' after name */
@ -398,6 +398,8 @@ static int32_t __foreach_handler_libWriteIndex(Arg* argEach, Args* context) {
// name_buff, 1, LIB_INFO_BLOCK_SIZE - sizeof(bytecode_size), out_file);
// pika_platform_fwrite(&bytecode_size, 1, sizeof(bytecode_size),
// out_file);
__platform_free(name_buff);
}
strsDeinit(&buffs);
return 0;
@ -567,6 +569,7 @@ static PIKA_RES _loadModuleDataWithIndex(uint8_t* library_bytes,
return PIKA_RES_OK;
}
PIKA_RES _loadModuleDataWithName(uint8_t* library_bytes,
char* module_name,
uint8_t** addr_p,
@ -575,18 +578,25 @@ PIKA_RES _loadModuleDataWithName(uint8_t* library_bytes,
if (module_num < 0) {
return (PIKA_RES)module_num;
}
Args buffs = {0};
for (int i = 0; i < module_num; i++) {
char* name = NULL;
uint8_t* addr = NULL;
size_t size = 0;
_loadModuleDataWithIndex(library_bytes, module_num, i, &name, &addr,
&size);
name = strsGetLastToken(&buffs, name, '/'); /*找到最后一个 / 出现的位置的下一个地址*/
if (strEqu(module_name, name)) {
*addr_p = addr;
*size_p = size;
strsDeinit(&buffs);
return PIKA_RES_OK;
}
}
strsDeinit(&buffs);
return PIKA_RES_ERR_ARG_NO_FOUND;
}
@ -617,6 +627,7 @@ PIKA_RES _getPack_libraryBytes(pikafs_FILE** fp, Arg** f_arg, char* pack_name) {
if (NULL == *f_arg) {
pika_platform_printf("Error: Could not load file \'%s\'\r\n", pack_name);
pikaFree(*fp, sizeof(pikafs_FILE));
arg_deinit(*f_arg);
// fp == NULL;
return PIKA_RES_ERR_IO_ERROR;
}
@ -1164,7 +1175,9 @@ pikafs_FILE* pikafs_fopen(char* file_name, char* mode) {
return f;
}
pikafs_FILE* pikafs_fopen_pack(char* pack_name, char* file_name) {
pikafs_FILE* f = NULL;
Arg* file_arg = NULL;
PIKA_RES stat = PIKA_RES_OK;
@ -1178,7 +1191,7 @@ pikafs_FILE* pikafs_fopen_pack(char* pack_name, char* file_name) {
if (PIKA_RES_OK !=
_loadModuleDataWithName(library_bytes, file_name, &f->addr, &f->size)) {
return NULL;
f = NULL;
}
arg_deinit(file_arg);

View File

@ -614,6 +614,7 @@ Arg* arg_loadFile(Arg* self, char* filename) {
FILE* input_file = pika_platform_fopen(filename, "rb");
if (NULL == input_file) {
pika_platform_printf("Error: Couldn't open file '%s'\n", filename);
arg_deinit(res);
res = NULL;
goto exit;
}
@ -622,7 +623,9 @@ Arg* arg_loadFile(Arg* self, char* filename) {
if (file_size >= PIKA_READ_FILE_BUFF_SIZE) {
pika_platform_printf("Error: Not enough buff for input file.\r\n");
return NULL;
arg_deinit(res);
res = NULL;
goto exit;
}
/* add '\0' to the end of the string */
res = arg_setBytes(res, "", (uint8_t*)file_buff, file_size + 1);

View File

@ -4,7 +4,7 @@ TEST_START
#include "PikaCompiler.h"
// TEST(packtool, unpack) {
// PIKA_RES res = pikafs_unpack_files("test/out/packout/0417.pack", "test/out/unpackout/");
// PIKA_RES res = pikafs_unpack_files("test/out/packout/0424.pack", "test/out/unpackout/");
// EXPECT_EQ(res, PIKA_RES_OK);
// }
@ -17,30 +17,36 @@ TEST(packtool, packfiles) {
pikaMaker_linkRaw_New(maker, "test/out/file3.txt", "/txt-file");
pikaMaker_linkRaw_New(maker, "test/out/G.bmp", "/bmp-file");
ret = pikaMaker_linkCompiledModulesFullPath(maker, "./test/out/packout/0418.pack");
ret = pikaMaker_linkCompiledModulesFullPath(maker, "./test/out/packout/0424.pack");
pikaMaker_deinit(maker);
EXPECT_EQ(ret, PIKA_RES_OK);
}
// TEST(packtool, packread) {
// size_t n = 0;
// //Arg* fileArg = NULL;
// pikafs_FILE* pack_file = pikafs_fopen_pack("test/packout/0417.pack", "main.py");
// // pikafs_FILE* pack_file = pikafs_fopen_pack_new(&fileArg, "test/assets/a.pack", "main.py");
// FILE* file = pika_platform_fopen("test/out/unpackout/main2.py", "wb+");
// if (NULL == file) {
// pika_platform_printf("open file: %s error\r\n", "test/out/unpackout/main2.py");
// }
TEST(packtool, packread) {
size_t n = 0;
//Arg* fileArg = NULL;
pikafs_FILE* pack_file = pikafs_fopen_pack("test/out/packout/0424.pack", "file3.txt");
// pikafs_FILE* pack_file = pikafs_fopen_pack_new(&fileArg, "test/assets/a.pack", "main.py");
FILE* file = pika_platform_fopen("test/out/unpackout/file3_test.txt", "wb+");
if (NULL == file) {
pika_platform_printf("open file: %s error\r\n", "test/out/unpackout/file3_test.txt");
}
// n = pika_platform_fwrite(pack_file->addr, pack_file->size, 1, file);
// EXPECT_NE(n, 0);
if (NULL == pack_file) {
pika_platform_printf("open file: %s error\r\n", "test/out/packout/0424.pack");
}
// //arg_deinit(fileArg);
// pikaFree(pack_file, sizeof(pikafs_FILE));
// pika_platform_fclose(file);
// pack_file = NULL;
n = pika_platform_fwrite(pack_file->addr, pack_file->size, 1, file);
EXPECT_NE(n, 0);
// }
//arg_deinit(fileArg);
pikaFree(pack_file, sizeof(pikafs_FILE));
pika_platform_fclose(file);
pack_file = NULL;
}
TEST_END