diff options
| author | 彭宣正 <[email protected]> | 2021-12-20 15:37:00 +0800 |
|---|---|---|
| committer | 彭宣正 <[email protected]> | 2021-12-20 15:37:00 +0800 |
| commit | 3488e3aba8008b0a3073a00e01332d872580779a (patch) | |
| tree | 2c816b82f35a95716dd0317c6a21307b6281f339 | |
| parent | 0dbf2e6957168a9f271881adf565918143f96586 (diff) | |
🧪 test(gtest. CI): 增加gtest用例,增加CPPCHECK,删除生成的可执行文件
| -rw-r--r-- | .gitignore | 64 | ||||
| -rw-r--r-- | .gitlab-ci.yml | 21 | ||||
| -rw-r--r-- | CMakeLists.txt | 19 | ||||
| -rwxr-xr-x | example/checktools/checktools.lua | 4 | ||||
| -rwxr-xr-x | example/multithread | bin | 25480 -> 0 bytes | |||
| -rwxr-xr-x | example/single_thread | bin | 25696 -> 0 bytes | |||
| -rw-r--r-- | gtest/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | gtest/gtest_get_weixinnum.cpp | 119 | ||||
| -rwxr-xr-x | gtest/gtest_tsg_lua | bin | 3869264 -> 0 bytes | |||
| -rw-r--r-- | gtest/gtest_tsg_lua_cache_script.cpp | 2 | ||||
| -rw-r--r-- | gtest/gtest_tsg_lua_exec.cpp | 2 | ||||
| -rw-r--r-- | gtest/gtest_tsg_lua_exec_bytecode.cpp | 2 | ||||
| -rw-r--r-- | gtest/script/handle_qqnum.lua | 55 | ||||
| -rw-r--r-- | gtest/script/handle_weixinnum.lua | 47 | ||||
| -rw-r--r-- | gtest/script/weixin_id_ios_20200423_004.pcap | bin | 0 -> 85894 bytes | |||
| -rw-r--r-- | src/CMakeLists.txt | 14 | ||||
| -rw-r--r-- | src/tsg_lua_func.cpp | 70 |
17 files changed, 335 insertions, 91 deletions
@@ -1,41 +1,23 @@ -CMakeCache.txt -CMakeCCompiler.cmake -CMakeCXXCompiler.cmake -CMakeDetermineCompilerABI_C.bin -CMakeDetermineCompilerABI_CXX.bin -CMakeSystem.cmake -CMakeCCompilerId.c -a.out -CMakeCXXCompilerId.cpp -CMakeDirectoryInformation.cmake -CMakeOutput.log -Makefile.cmake -Makefile2 -TargetDirectories.txt -CXX.includecache -DependInfo.cmake -build.make -cmake_clean.cmake -depend.internal -depend.make -flags.make -handle_weixinnum.cpp.o -link.txt -progress.make -cmake_clean_target.cmake -cmake.check_cache -feature_tests.bin -feature_tests.c -feature_tests.cxx -handle_fasta_count.cpp.o -handle_fasta_find.cpp.o -progress.marks -Makefile -cmake_install.cmake -libc_handle_weixinnum.a -libc_handle_weixinnum.so -libhandle_fasta_count.a -libhandle_fasta_count.so -libhandle_fasta_find.a -libhandle_fasta_find.so -tags +SI/ +*.log +*.o +*.so +*.si4project/ +*.a +*.d +build/ +.vscode +.idea +core.* +cmake-build-* +GPATH +GTAGS +GRTAGS +src/tags +version.txt +gtest/build/ +example/build/ +example/luatest/build/ +example/multithread +example/single_thread +example/luatest/lib/build/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6b0a3ff..e298ffe 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,6 +6,8 @@ variables: INSTALL_DEPENDENCY_LIBRARY: libMESA_handle_logger-devel libcjson-devel libMESA_field_stat2-devel sapp-devel framework_env libMESA_prof_load-devel http-devel dns-devel ftp-devel mail-devel ssl-devel librdkafka-devel libmaatframe-devel tsg_master-devel libdocumentanalyze-devel stages: +- analysis +- test - build .build_by_travis: @@ -21,6 +23,25 @@ stages: tags: - share +run_cppcheck: + stage: analysis + script: + - mkdir build || true + - cd build + - cmake3 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. + - cppcheck --project=compile_commands.json --enable=all --error-exitcode=1 --suppress=unusedFunction --suppress=missingInclude --suppress=uselessAssignmentPtrArg --suppress=unmatchedSuppression + tags: + - share + +run_test: + stage: test + extends: .build_by_travis + script: + - yum makecache + - ./ci/travis.sh + - cd build + - ctest --verbose + branch_build_debug: stage: build extends: .build_by_travis diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a2b043..7863164 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,9 +14,28 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") set(CMAKE_INSTALL_PREFIX /opt/MESA) set(LUAJIT_INSTALL_PREFIX /usr/local) +find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck) +if (CMAKE_CXX_CPPCHECK) + list( + APPEND CMAKE_CXX_CPPCHECK + "--enable=all" + "--error-exitcode=1" + "--suppress=unusedFunction" + "--suppress=missingInclude" + "--suppress=uselessAssignmentPtrArg" + "--suppress=unmatchedSuppression" + ) + set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK}) +else() + message(FATAL_ERROR "Could not find the program cppcheck.") +endif() + add_subdirectory(support) add_subdirectory(src) +enable_testing() +add_subdirectory(gtest) + install(FILES ./src/tsg_lua_interface.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER) install(PROGRAMS ./example/checktools/checktools.lua DESTINATION ${LUAJIT_INSTALL_PREFIX}/bin) diff --git a/example/checktools/checktools.lua b/example/checktools/checktools.lua index be36345..45223b4 100755 --- a/example/checktools/checktools.lua +++ b/example/checktools/checktools.lua @@ -15,7 +15,7 @@ if arg[1] then end end -function file_is_exit(path) +local function file_is_exit(path) local file = io.open(path) if not file then return false @@ -25,7 +25,7 @@ function file_is_exit(path) end if not file_is_exit(conf_file) then - print("error: conf file: ".. file .." not exits.") + print("error: conf file: ".. conf_file .." not exits.") return false end diff --git a/example/multithread b/example/multithread Binary files differdeleted file mode 100755 index d7d9dbd..0000000 --- a/example/multithread +++ /dev/null diff --git a/example/single_thread b/example/single_thread Binary files differdeleted file mode 100755 index c460291..0000000 --- a/example/single_thread +++ /dev/null diff --git a/gtest/CMakeLists.txt b/gtest/CMakeLists.txt index 7d04559..1d30758 100644 --- a/gtest/CMakeLists.txt +++ b/gtest/CMakeLists.txt @@ -7,6 +7,9 @@ link_directories("/opt/MESA/lib") link_libraries(tsglua gtest gtest_main pthread) add_definitions(-g -W -Wall) -add_executable(../gtest_tsg_lua ${SRCS}) -target_link_libraries(../gtest_tsg_lua tsglua gtest gtest_main pthread) +add_executable(gtest_tsg_lua ${SRCS}) +target_link_libraries(gtest_tsg_lua tsglua gtest gtest_main pthread) + +add_test(NAME SOFT_LINK COMMAND sh -c "ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/script" "${CMAKE_CURRENT_BINARY_DIR}/script") +add_test(NAME LUA_TEST COMMAND gtest_tsg_lua) diff --git a/gtest/gtest_get_weixinnum.cpp b/gtest/gtest_get_weixinnum.cpp new file mode 100644 index 0000000..e7a30a2 --- /dev/null +++ b/gtest/gtest_get_weixinnum.cpp @@ -0,0 +1,119 @@ +#include <gtest/gtest.h> +#include "tsg_lua_interface.h" + +static char *get_file_to_buffer(const char *file, int *len) +{ + FILE *fp = fopen(file, "r"); + if (fp) + { + fseek(fp, 0, 2); + *len = ftell(fp); + fseek(fp, 0, 0); + if (*len <= 0) + { + fclose(fp); + return NULL; + } + char *buff = (char *)malloc((*len) + 1); + int num = fread(buff, 1, *len, fp); + if (num != 0) + { + buff[num] = 0; + *len = num - 1; + fclose(fp); + return buff; + } + free(buff); + fclose(fp); + } + return NULL; +} + +TEST(tsg_lua_get_weixinnum, exec_cache_script) +{ + tsg_lua_handle L = NULL; + int script_len; + int data_len; + char out[255]; + size_t out_len = 0; + size_t out_type = 0; + memset(out, 0, 255); + + L = tsg_lua_vm_create(); + const char *script = get_file_to_buffer("./script/handle_weixinnum.lua", &script_len); + int script_id = tsg_lua_cache_script(L, script, script_len); + const char *data = get_file_to_buffer("./script/weixin_id_ios_20200423_004.pcap", &data_len); + int ret = tsg_lua_cache_exec(L, script_id, data, data_len, out, &out_len, &out_type); + free((void *)data); + free((void *)script); + + EXPECT_EQ(0, ret); + EXPECT_EQ(10, out_len); + EXPECT_EQ(STRING, out_type); + EXPECT_STREQ("1955740780", out); +} + +TEST(tsg_lua_get_weixinnum, exec_cache_file) +{ + tsg_lua_handle L = NULL; + int data_len; + char out[255]; + size_t out_len = 0; + size_t out_type = 0; + + memset(out, 0, 255); + L = tsg_lua_vm_create(); + int script_id = tsg_lua_cache_script_file(L, "./script/handle_weixinnum.lua"); + const char *data = get_file_to_buffer("./script/weixin_id_ios_20200423_004.pcap", &data_len); + int ret = tsg_lua_cache_exec(L, script_id, data, data_len, out, &out_len, &out_type); + free((void *)data); + + EXPECT_EQ(0, ret); + EXPECT_EQ(10, out_len); + EXPECT_EQ(STRING, out_type); + EXPECT_STREQ("1955740780", out); +} + +TEST(tsg_lua_get_weixinnum, exec_script) +{ + tsg_lua_handle L = NULL; + int script_len; + int data_len; + char out[255]; + size_t out_len = 0; + size_t out_type = 0; + + memset(out, 0, 255); + L = tsg_lua_vm_create(); + const char *script = get_file_to_buffer("./script/handle_weixinnum.lua", &script_len); + const char *data = get_file_to_buffer("./script/weixin_id_ios_20200423_004.pcap", &data_len); + int ret = tsg_lua_exec(L, script, script_len, data, data_len, out, &out_len, &out_type); + free((void *)data); + free((void *)script); + + EXPECT_EQ(0, ret); + EXPECT_EQ(10, out_len); + EXPECT_EQ(STRING, out_type); + EXPECT_STREQ("1955740780", out); +} + +TEST(tsg_lua_get_weixinnum, exec_file) +{ + tsg_lua_handle L = NULL; + int data_len; + char out[255]; + size_t out_len = 0; + size_t out_type = 0; + + memset(out, 0, 255); + L = tsg_lua_vm_create(); + const char *script = "./script/handle_weixinnum.lua"; + const char *data = get_file_to_buffer("./script/weixin_id_ios_20200423_004.pcap", &data_len); + int ret = tsg_lua_exec_file(L, script, data, data_len, out, &out_len, &out_type); + free((void *)data); + + EXPECT_EQ(0, ret); + EXPECT_EQ(10, out_len); + EXPECT_EQ(STRING, out_type); + EXPECT_STREQ("1955740780", out); +}
\ No newline at end of file diff --git a/gtest/gtest_tsg_lua b/gtest/gtest_tsg_lua Binary files differdeleted file mode 100755 index 2731bca..0000000 --- a/gtest/gtest_tsg_lua +++ /dev/null diff --git a/gtest/gtest_tsg_lua_cache_script.cpp b/gtest/gtest_tsg_lua_cache_script.cpp index ae20c29..1623a31 100644 --- a/gtest/gtest_tsg_lua_cache_script.cpp +++ b/gtest/gtest_tsg_lua_cache_script.cpp @@ -18,8 +18,10 @@ static char *get_script_buffer(const char *script) { buffer[num] = 0; script_len = num - 1; + fclose(fp); return buffer; } + fclose(fp); } return NULL; } diff --git a/gtest/gtest_tsg_lua_exec.cpp b/gtest/gtest_tsg_lua_exec.cpp index 44c4ad5..8f52ce8 100644 --- a/gtest/gtest_tsg_lua_exec.cpp +++ b/gtest/gtest_tsg_lua_exec.cpp @@ -17,8 +17,10 @@ static char *get_script_buffer(const char *script) if (num != 0) { buffer[num] = 0; + fclose(fp); return buffer; } + fclose(fp); } return NULL; } diff --git a/gtest/gtest_tsg_lua_exec_bytecode.cpp b/gtest/gtest_tsg_lua_exec_bytecode.cpp index 4365174..3fa8ac0 100644 --- a/gtest/gtest_tsg_lua_exec_bytecode.cpp +++ b/gtest/gtest_tsg_lua_exec_bytecode.cpp @@ -19,8 +19,10 @@ static char *get_script_buffer(const char *script) { buffer[num] = 0; script_len = num; + fclose(fp); return buffer; } + fclose(fp); } return NULL; } diff --git a/gtest/script/handle_qqnum.lua b/gtest/script/handle_qqnum.lua new file mode 100644 index 0000000..fc0d1a4 --- /dev/null +++ b/gtest/script/handle_qqnum.lua @@ -0,0 +1,55 @@ +---------------------------------------------------------------------------------------------- +-- 脚本功能:在数据中查找符合特征的qqnum -- +-- 输入: -- +-- TSG.data -- +-- 特征如下: -- +-- 特征一: -- +-- 00 00 0b db 00 00 00 0b 01 00 00 -- +-- 第1、2、5、6、7、10、11位必须为0x00,第9为必须为0x01 -- +-- 特征二: -- +-- 00 00 00 00 0d -- +-- 连续4个0x00后面紧跟一个字节的qq num 字符长度, -- +-- 该长度实际值为当前值减4,并且实际值不大于12 -- +---------------------------------------------------------------------------------------------- +local data = TSG.data --TSG.data 获取待处理数据 +local data_len = string.len(data) +local max_qqnum_len = 12 +local locate = 1 -- 字符串中当前正在处理的位置 + +-- 判断特征一 +if (string.byte(data, 1) ~= 0) or (string.byte(data, 2) ~= 0) or (string.byte(data, 5) ~= 0) or + (string.byte(data, 6) ~= 0) or (string.byte(data, 7) ~= 0) or (string.byte(data, 9) ~= 1) or + (string.byte(data, 10) ~= 0) or (string.byte(data, 11) ~= 0) then + return 1, false +end + +-- 判断特征二 +-- 命中特征"\0\0\0\0" +local _, _end = string.find(data, "\0\0\0\0") +if not _end then + return 1, false +end +-- 获取qqnum长度 +local qqnum_len = string.byte(data, _end + 1) +if not qqnum_len then + return 1,false +end +locate = _end + 1 +qqnum_len = qqnum_len - 4 +-- 判断qqnum长度的合法性 +if qqnum_len > 12 or qqnum_len > (data_len - locate) then + return 1, false +end +locate = locate + 1 +-- 获取qqnum +local qqnum = string.sub(data, locate, locate + qqnum_len - 1) +if not qqnum then + return 1, false +end +-- 验证qqnum由全数字组成 +if not tonumber(qqnum) then + return 1, false +end + +return qqnum_len, qqnum + diff --git a/gtest/script/handle_weixinnum.lua b/gtest/script/handle_weixinnum.lua new file mode 100644 index 0000000..1b8b448 --- /dev/null +++ b/gtest/script/handle_weixinnum.lua @@ -0,0 +1,47 @@ +--------------------------------------------------------------------------------------------- +-- 脚本功能:在数据中查找符合特征的weixinnum -- +-- 输入: -- +-- TSG.data -- +-- 特征如下: -- +-- 00 00 00 09 | 77 65 69 78 69 6e 6d 75 2d | 00 00 00 0a | 31 39 35 35 37 34 30 37 38 30 -- +-- weixinnum | w e i x i n n u m | weixinnum | 1 9 5 5 7 4 0 7 8 0 -- +-- 字符串长度 | 字符串 | 数值长度 | 字符串 -- +--------------------------------------------------------------------------------------------- + +local data = TSG.data --TSG.data 获取待处理数据 +local data_len = string.len(data) +local feature = "weixinnum" --待识别的特征 +local max_weixinnum_len = 12 +local offset = 4 -- 字符串长度所占位数 +local locate = 0 -- 字符串中当前正在处理的位置 + +-- 查找字符串weixinnum位置 +local _start, _end = string.find(data, feature) +if not _end then + return 1, false +end + +-- 获取weixinnum数值字符串长度 +-- weixinnum数值字符串长度不超过12,所以前三位必须为0 +if ((string.byte(data, _end + 1) ~= 0) or (string.byte(data, _end + 2) ~=0 ) or (string.byte(data, _end + 3) ~= 0)) then + return 1, false +end +local weixinnum_len = string.byte(data, _end + offset) +if not weixinnum_len then + return 1, false +end +locate = _end + offset + +-- 判断weixinnum数值字符串长度的合法性 +if weixinnum_len > data_len - _end - offset or weixinnum_len > max_weixinnum_len then + return 1, false +end + +-- 获取weixinnum数值字符串 +local weixinnum = string.sub(data, locate + 1, locate + weixinnum_len) +-- weixinnum数值字符串是否可以转换为数字 +if tonumber(weixinnum) then + return weixinnum_len, weixinnum +else + return 1, false +end diff --git a/gtest/script/weixin_id_ios_20200423_004.pcap b/gtest/script/weixin_id_ios_20200423_004.pcap Binary files differnew file mode 100644 index 0000000..bd42bbc --- /dev/null +++ b/gtest/script/weixin_id_ios_20200423_004.pcap diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3dd3177..c809e9c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,16 +7,16 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC") SET(CMAKE_CXX_DEBUG_FLAGS "${CMAKE_CXX_DEBUG_FLAGS} -DTSG_LUA_DEBUG") SET(CMAKE_BUILD_TYPE "Release") +option(TSG_LUA_DEBUG "If enabled, the SDK will be print debuginfo." OFF) + +if (TSG_LUA_DEBUG) + add_definitions(-DTSG_LUA_DEBUG) +endif() + + add_library(${lib_name}_shared SHARED ${DIR_LIB_SRCS}) target_link_libraries(${lib_name}_shared dl luajit2-static) set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name}) -#set_target_properties(${lib_name} PROPERTIES PREFIX "") -#set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name}) - -#add_library(${lib_name}_static ${DIR_LIB_SRCS}) -#add_dependencies(${lib_name}_static luajit2-static) -#set_target_properties(${lib_name}_static PROPERTIES PREFIX "") install(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARIES) -#install(TARGETS ${lib_name}_static LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARY) diff --git a/src/tsg_lua_func.cpp b/src/tsg_lua_func.cpp index ba04a4f..dd18a68 100644 --- a/src/tsg_lua_func.cpp +++ b/src/tsg_lua_func.cpp @@ -242,15 +242,11 @@ static int tsg_lua_clfactory_bytecode_buffer_prepare(tsg_lua_clfactory_buffer_t static int tsg_lua_clfactory_bytecode_file_prepare(tsg_lua_clfactory_file_t *lf) { - int little_endian, version, stripped; - size_t size; - long fsize; - *lf->begin_code.str = LUA_SIGNATURE[0]; if (lf->file_type == TSG_LUA_BC_LJ) { /* get bytecode header */ - size = fread(lf->begin_code.str + 1, 1, LJ_HEADERSIZE - 1, lf->f); + size_t size = fread(lf->begin_code.str + 1, 1, LJ_HEADERSIZE - 1, lf->f); if (size != LJ_HEADERSIZE - 1) { debuginfo("can not read header", __FILE__, __LINE__); @@ -258,7 +254,7 @@ static int tsg_lua_clfactory_bytecode_file_prepare(tsg_lua_clfactory_file_t *lf) } /* get bytecode version */ - version = *(lf->begin_code.str + 3); + int version = *(lf->begin_code.str + 3); /* compare bytecode header */ if (strncmp(lf->begin_code.str, LJ_SIGNATURE, sizeof(LJ_SIGNATURE) - 1)) @@ -270,7 +266,7 @@ static int tsg_lua_clfactory_bytecode_file_prepare(tsg_lua_clfactory_file_t *lf) lf->begin_code_len = LJ_HEADERSIZE; /* little endian or big little endian */ - little_endian = !((*(lf->begin_code.str + 4)) & LJ_BCDUMP_F_BE); + int little_endian = !((*(lf->begin_code.str + 4)) & LJ_BCDUMP_F_BE); if (little_endian == 0) { debuginfo("not support byte-code coding by big-endian", __FILE__, __LINE__); @@ -278,7 +274,7 @@ static int tsg_lua_clfactory_bytecode_file_prepare(tsg_lua_clfactory_file_t *lf) } /* stripped or debug */ - stripped = (*(lf->begin_code.str + 4)) & LJ_BCDUMP_F_STRIP; + int stripped = (*(lf->begin_code.str + 4)) & LJ_BCDUMP_F_STRIP; if (!stripped) { debuginfo("not support byte-code include debug-info", __FILE__, __LINE__); @@ -298,7 +294,7 @@ static int tsg_lua_clfactory_bytecode_file_prepare(tsg_lua_clfactory_file_t *lf) debuginfo("bytecode format version unsupported", __FILE__, __LINE__); return ERR_SCRIPT_BYTECODE_VERSION_UNSUPPORTED; } - fsize = tsg_lua_clfactory_file_size(lf->f); + long fsize = tsg_lua_clfactory_file_size(lf->f); if (fsize < 0) { return ERR_SCRIPT_IS_BAD; @@ -490,14 +486,13 @@ tsg_lua_handle tsg_lua_vm_create() lua_setfield(L, -2, "memmem"); lua_setglobal(L, "TSG"); #if 1 - const char *err; const char *code = "local ffi = require(\"ffi\")\n"\ "ffi.cdef[[char *memmem(const char *haystack, size_t haystacklen, const char *needle, size_t needlelen);]]\n"\ "TSG.ffi = ffi\n"\ "TSG.C = ffi.C"; if (luaL_dostring(L, code)) { - err = lua_tostring(L, -1); + const char *err = lua_tostring(L, -1); debuginfo(err, __FILE__, __LINE__); lua_close(L); return NULL; @@ -510,7 +505,6 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si { lua_State *L = (lua_State *)lua; const char *err = NULL; - char err_buf[255]; int ret = 0; int i = 0; int sharp = 0; @@ -567,7 +561,7 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si sharp = 1; } - if (c == LUA_SIGNATURE[0] && script) + if (c == LUA_SIGNATURE[0]) { lf.f = freopen(script, "rb", lf.f); if (lf.f == NULL) @@ -671,12 +665,14 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si int num = lua_gettop(L); if (num < 2) { + char err_buf[255]; sprintf((char *)err_buf,"num:%d", num); debuginfo(err_buf, __FILE__, __LINE__); return ERR_SCRIPT_RETURN_TOO_FEW; } if (num > 2) { + char err_buf[255]; sprintf((char *)err_buf,"num:%d", num); debuginfo(err_buf, __FILE__, __LINE__); return ERR_SCRIPT_RETURN_TOO_MUCH; @@ -704,7 +700,8 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si *out_type = INTEGER; break; default: - sprintf((char *)err_buf,"out_type:%d", (lua_type(L, -1))); + char err_buf[255]; + sprintf((char *)err_buf, "out_type:%d", (lua_type(L, -1))); debuginfo(err_buf, __FILE__, __LINE__); *out_len = 0; return ERR_SCRIPT_RETURN_TYPE_INVAILD; @@ -722,11 +719,9 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons { lua_State *L = (lua_State *)lua; const char *err = NULL; - char err_buf[255]; int ret = 0; int i = 0; int sharp = 0; - int status; tsg_lua_clfactory_buffer_t ls; jmp_buf *lua_exception; @@ -786,7 +781,7 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons { return ERR_SCRIPT_IS_BAD; } - status = tsg_lua_clfactory_bytecode_buffer_prepare(&ls); + int status = tsg_lua_clfactory_bytecode_buffer_prepare(&ls); if (status != 0) { return status; @@ -868,13 +863,15 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons int num = lua_gettop(L); if (num < 2) { - sprintf((char *)err_buf,"num:%d", num); + char err_buf[255]; + sprintf((char *)err_buf, "num:%d", num); debuginfo(err_buf, __FILE__, __LINE__); return ERR_SCRIPT_RETURN_TOO_FEW; } if (num > 2) { - sprintf((char *)err_buf,"num:%d", num); + char err_buf[255]; + sprintf((char *)err_buf, "num:%d", num); debuginfo(err_buf, __FILE__, __LINE__); return ERR_SCRIPT_RETURN_TOO_MUCH; } @@ -896,12 +893,13 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons *out_type = BOOLEAN; break; case LUA_TNUMBER: - *(long size_t *)out = lua_tointeger(L, -1); + *(size_t *)out = lua_tointeger(L, -1); //*out_len = 8; *out_type = INTEGER; break; default: - sprintf((char *)err_buf,"out_type:%d", (lua_type(L, -1))); + char err_buf[255]; + sprintf((char *)err_buf, "out_type:%d", (lua_type(L, -1))); debuginfo(err_buf, __FILE__, __LINE__); *out_len = 0; return ERR_SCRIPT_RETURN_TYPE_INVAILD; @@ -920,7 +918,7 @@ int tsg_lua_cache_script(tsg_lua_handle lua, const char *script, size_t script_l const char *err = NULL; int ret = 0; int i = 0; - int sharp = 0, status = 0; + int sharp = 0; lua_State *L = (lua_State *)lua; if (L == NULL) @@ -967,7 +965,7 @@ int tsg_lua_cache_script(tsg_lua_handle lua, const char *script, size_t script_l return ERR_SCRIPT_IS_BAD; } - status = tsg_lua_clfactory_bytecode_buffer_prepare(&ls); + int status = tsg_lua_clfactory_bytecode_buffer_prepare(&ls); if (status != 0) { return status; @@ -1037,7 +1035,6 @@ int tsg_lua_cache_script_file(tsg_lua_handle lua, const char *script) size_t script_id; const char *err = NULL; int ret = 0; - int i = 0; int sharp = 0; tsg_lua_clfactory_file_t lf; lua_State *L = (lua_State *)lua; @@ -1078,7 +1075,7 @@ int tsg_lua_cache_script_file(tsg_lua_handle lua, const char *script) sharp = 1; } - if (c == LUA_SIGNATURE[0] && script) + if (c == LUA_SIGNATURE[0]) { lf.f = freopen(script, "rb", lf.f); if (lf.f == NULL) @@ -1090,11 +1087,7 @@ int tsg_lua_cache_script_file(tsg_lua_handle lua, const char *script) { /* 出于对安全的考虑,禁用字节码文件首行不是lua代码的文件 * 这是因为,字节码文件首行不是lua代码会规避掉一些常规字节码检查 */ - if(script) - { - fclose(lf.f); - } - + fclose(lf.f); return ERR_SCRIPT_IS_BAD; } @@ -1126,7 +1119,7 @@ int tsg_lua_cache_script_file(tsg_lua_handle lua, const char *script) { err = lua_tostring(L, -1); debuginfo(err, __FILE__, __LINE__); - for (i = 0; i < SYNTAX_ERROR_INFO_NUM; i++) + for (int i = 0; i < SYNTAX_ERROR_INFO_NUM; i++) { if (strstr(err, syntax_error[i])) { @@ -1152,7 +1145,7 @@ int tsg_lua_cache_script_file(tsg_lua_handle lua, const char *script) { err = lua_tostring(L, -1); debuginfo(err, __FILE__, __LINE__); - for (i = 0; i < SYNTAX_ERROR_INFO_NUM; i++) + for (int i = 0; i < SYNTAX_ERROR_INFO_NUM; i++) { if (strstr(err, syntax_error[i])) { @@ -1191,10 +1184,6 @@ int tsg_lua_uncache_script(tsg_lua_handle lua, size_t script_id) int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, size_t in_len, char *out, size_t *out_len, size_t *out_type) { lua_State *L = (lua_State *)lua; - const char *err; - char err_buf[255]; - int i = 0; - int num = 0; jmp_buf *lua_exception; if (L == NULL) @@ -1231,24 +1220,26 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz { if (lua_pcall(L, 0, LUA_MULTRET, 0)) { - err = lua_tostring(L, -1); + const char *err = lua_tostring(L, -1); debuginfo(err, __FILE__, __LINE__); - for (i = 0; i < EXEC_ERROR_INFO_NUM; i++) + for (int i = 0; i < EXEC_ERROR_INFO_NUM; i++) { if ((strstr(err, exec_error[i].err_info1)) && (strstr(err, exec_error[i].err_info2))) return ERR_SCRIPT_EXEC_ERROR - (i + 1); } return ERR_SCRIPT_EXEC_ERROR; } - num = lua_gettop(L); + int num = lua_gettop(L); if (num < 2) { + char err_buf[255]; sprintf((char *)err_buf,"num:%d", num); debuginfo(err_buf, __FILE__, __LINE__); return ERR_SCRIPT_RETURN_TOO_FEW; } if (num > 2) { + char err_buf[255]; sprintf((char *)err_buf,"num:%d", num); debuginfo(err_buf, __FILE__, __LINE__); return ERR_SCRIPT_RETURN_TOO_MUCH; @@ -1277,6 +1268,7 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz *out_type = INTEGER; break; default: + char err_buf[255]; sprintf((char *)err_buf,"out_type:%d", lua_type(L, -1)); debuginfo(err_buf, __FILE__, __LINE__); *out_len = 0; |
