Fixed:memory leaks bug about open source code.

This commit is contained in:
Fancy code 2024-02-28 05:54:49 -08:00
parent 64ad852f91
commit 359eec1914
14 changed files with 277 additions and 16 deletions

View File

@ -82,6 +82,11 @@ void _hs_accept_and_begin_request_cycle(http_server_t *server,
} else {
hs_request_begin_read(request);
}
// ================ added by xiao ================ //
if (request) {
hs_request_terminate_connection(request);
}
// ================ added by xiao end ================ //
}
}

View File

@ -102,11 +102,11 @@ void _hs_add_server_sock_events(http_server_t *serv) {
ev.data.ptr = serv;
epoll_ctl(serv->loop, EPOLL_CTL_ADD, serv->socket, &ev);
}
int gHttpServerRuning = 1;
int hs_server_run_event_loop(http_server_t *serv, const char *ipaddr) {
hs_server_listen_on_addr(serv, ipaddr);
struct epoll_event ev_list[1];
while (1) {
while (gHttpServerRuning) {
int nev = epoll_wait(serv->loop, ev_list, 1, -1);
for (int i = 0; i < nev; i++) {
ev_cb_t *ev_cb = (ev_cb_t *)ev_list[i].data.ptr;

View File

@ -8,7 +8,7 @@ include_directories(
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/LedControl/include
${UTILS_SOURCE_PATH}/FxHttpServer/include
${HAL_SOURCE_PATH}/include
)
#do not rely on any other library

View File

@ -13,7 +13,47 @@
* limitations under the License.
*/
#include "AppManager.h"
#include "FxHttpServer.h"
#include "ILog.h"
#include <vector>
const StatusCode AppManager::Init(void) { return CreateStatusCode(STATUS_CODE_OK); }
const StatusCode AppManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); }
AppManager::AppManager()
{
//
// mHttpServerRuning = false;
}
const StatusCode AppManager::Init(void)
{
HttpServerStart();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode AppManager::UnInit(void)
{
HttpServerStop();
return CreateStatusCode(STATUS_CODE_OK);
}
void AppManager::HttpServerStart(void)
{
auto httpServerThread = [](std::shared_ptr<AppManager> app) {
LogInfo("AppManager httpServer started.\n");
app->HttpServerThread();
};
mHttpSever = std::thread(httpServerThread, shared_from_this());
}
void AppManager::HttpServerStop(void)
{
FxHttpServerExit();
if (mHttpSever.joinable()) {
mHttpSever.join();
}
}
void AppManager::HttpServerThread(void)
{
// typedef void (*HttpHandleCallback)(const char *, const unsigned int, ResponseHandle, void *);
std::shared_ptr<AppManager> app = shared_from_this();
auto httpHandle =
[](const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context) -> void {
//
LogInfo("=============================== url = %s\n", url);
};
FxHttpServerInit(httpHandle);
FxHttpServerUnInit();
}

View File

@ -15,17 +15,23 @@
#ifndef APP_MANAGER_H
#define APP_MANAGER_H
#include "IAppManager.h"
class AppManager : public IAppManager
#include <thread>
class AppManager : public IAppManager, public std::enable_shared_from_this<AppManager>
{
public:
AppManager() = default;
AppManager();
virtual ~AppManager() = default;
const StatusCode Init(void) override;
const StatusCode UnInit(void) override;
private:
// std::vector<std::shared_ptr<LedManager>> mLedManagers;
void HttpServerStart(void);
void HttpServerStop(void);
void HttpServerThread(void);
private:
// bool mHttpServerRuning;
std::thread mHttpSever;
};
#endif

View File

@ -0,0 +1,87 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./src
./include
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
# ${UTILS_SOURCE_PATH}/UartDevice/include
# ${UTILS_SOURCE_PATH}/McuProtocol/include
${UTILS_SOURCE_PATH}/KeyControl/include
${UTILS_SOURCE_PATH}/LedControl/include
${HAL_SOURCE_PATH}/include
# ${HAL_SOURCE_PATH}/src
${MIDDLEWARE_SOURCE_PATH}/AppManager/include
${MIDDLEWARE_SOURCE_PATH}/AppManager/src
# ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include
# ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/hal/tool/include
# ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include
# ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
# ${TEST_SOURCE_PATH}/middleware/McuAskBase/tool/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME AppManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} AppManager gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
AppManagerTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make AppManagerTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
AppManagerTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make AppManagerTest_code_check
COMMAND make AppManagerTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
# add_subdirectory(tool)

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "IAppManager.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace AppManagerTest
{
class AppManagerTest : public testing::Test
{
public:
AppManagerTest() {}
virtual ~AppManagerTest() {}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase() { ILogUnInit(); }
virtual void SetUp()
{
// CreateAllKeysMcok();
// HalTestTool::Init();
// AppManagerTestTool::Init();
// CreateHalCppModule();
CreateAppManagerModule();
}
virtual void TearDown()
{
// HalTestTool::UnInit();
// AppManagerTestTool::UnInit();
DestroyAppManagerModule();
// DestroyAllKeysMock();
}
};
// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_Demo
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo)
{
IAppManager::GetInstance()->Init();
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
IAppManager::GetInstance()->UnInit();
}
} // namespace AppManagerTest

View File

@ -3,4 +3,5 @@
add_subdirectory(IpcConfig)
add_subdirectory(McuManager)
add_subdirectory(McuAskBase)
add_subdirectory(DeviceManager)
add_subdirectory(DeviceManager)
add_subdirectory(AppManager)

View File

@ -31,8 +31,6 @@ link_directories(
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})

View File

@ -36,8 +36,8 @@ void HttpHandle(const char *url, const unsigned int urlLength, ResponseHandle re
}
}
}
// ../output_files/test/bin/FxHttpServerTest --gtest_filter=FxHttpServerTest.Demo
TEST(FxHttpServerTest, Demo)
// ../output_files/test/bin/FxHttpServerTest --gtest_filter=FxHttpServerTest.INTEGRATION_AppManager_EXAMPLE_Demo
TEST(FxHttpServerTest, INTEGRATION_AppManager_EXAMPLE_Demo)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);

View File

@ -21,4 +21,42 @@ add_custom_command(
```
PUBLIC $<$<CONFIG:DEBUG>:-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all>
```
4.
4. 由于开源代码不支持安全退出,所以修改了开源代码;
```
int gHttpServerRuning = 1; // 增加一个运行标识
int hs_server_run_event_loop(http_server_t *serv, const char *ipaddr) {
hs_server_listen_on_addr(serv, ipaddr);
struct epoll_event ev_list[1];
while (gHttpServerRuning) { // 运行标识赋值为0时httpserver退出
int nev = epoll_wait(serv->loop, ev_list, 1, -1);
for (int i = 0; i < nev; i++) {
ev_cb_t *ev_cb = (ev_cb_t *)ev_list[i].data.ptr;
ev_cb->handler(&ev_list[i]);
}
}
return 0;
}
```
5. 修复一个内存安全漏洞;
```
void _hs_accept_and_begin_request_cycle(http_server_t *server,
hs_io_cb_t on_client_connection_cb,
hs_io_cb_t on_timer_event_cb) {
http_request_t *request = NULL;
while ((request = hs_server_accept_connection(server, on_client_connection_cb,
on_timer_event_cb))) {
if (server->memused > HTTP_MAX_TOTAL_EST_MEM_USAGE) {
hs_request_respond_error(request, 503, "Service Unavailable",
hs_request_begin_write);
} else {
hs_request_begin_read(request);
}
// ================ added by xiao ================ //
if (request) {
hs_request_terminate_connection(request); // 此处应该释放内存
}
// ================ added by xiao end ================ //
}
}
```
6.

View File

@ -21,6 +21,7 @@ extern "C" {
typedef void (*ResponseHandle)(const char *, void *);
typedef void (*HttpHandleCallback)(const char *, const unsigned int, ResponseHandle, void *);
StatusCode FxHttpServerInit(HttpHandleCallback httpHandle);
StatusCode FxHttpServerExit(void);
StatusCode FxHttpServerUnInit(void);
#ifdef __cplusplus
}

View File

@ -16,6 +16,7 @@
#include "ILog.h"
#include "httpserver.h"
#include <string.h>
extern int gHttpServerRuning;
static struct http_server_s *server = NULL;
static struct http_server_s *poll_server = NULL;
static HttpHandleCallback gHttpHandle = NULL;
@ -49,6 +50,11 @@ StatusCode FxHttpServerInit(HttpHandleCallback httpHandle)
http_server_listen(server);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode FxHttpServerExit(void)
{
gHttpServerRuning = 0;
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode FxHttpServerUnInit(void)
{
free(server);