添加认证清理函数,优化内存管理;修复问题查询和处理中的内存释放,增强代码稳定性

This commit is contained in:
keqingmoe 2024-12-30 16:45:59 +08:00
parent 68628a0ee2
commit 2e0f22a939
4 changed files with 16 additions and 2 deletions

View File

@ -12,7 +12,8 @@ int user_permission_handler(mg_connection* conn, void* cbdata);
int admin_handler(mg_connection* conn, void* cbdata); int admin_handler(mg_connection* conn, void* cbdata);
extern char* secret; extern char* secret;
extern char* admin_session; extern char* admin_session;
void auth_cleanup();
#endif // SERVER_AUTH_H #endif // SERVER_AUTH_H

View File

@ -176,7 +176,7 @@ extern "C"
{ {
auto problems = std::vector<int>{}; auto problems = std::vector<int>{};
leveldb::Iterator* it = problems_db->NewIterator(leveldb::ReadOptions()); auto it = problems_db->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) { for (it->SeekToFirst(); it->Valid(); it->Next()) {
if (!it->key().compare("@")) continue; if (!it->key().compare("@")) continue;
problems.emplace_back(std::stoi(it->key().ToString())); problems.emplace_back(std::stoi(it->key().ToString()));
@ -184,8 +184,10 @@ extern "C"
if (!it->status().ok()) { if (!it->status().ok()) {
std::println(stderr, "Failed to get all problems: {}", it->status().ToString()); std::println(stderr, "Failed to get all problems: {}", it->status().ToString());
delete it;
return 0; return 0;
} }
delete it;
auto json = nlohmann::json(problems); auto json = nlohmann::json(problems);
*result = strdup(json.dump().c_str()); *result = strdup(json.dump().c_str());

View File

@ -1,7 +1,14 @@
#include "server/auth.h" #include "server/auth.h"
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
char* secret = NULL; char* secret = NULL;
char* admin_session = NULL; char* admin_session = NULL;
void auth_cleanup()
{
if (secret) free(secret);
if (admin_session) free(admin_session);
}

View File

@ -141,9 +141,12 @@ static void impl_query(mg_connection* conn, problem_form_t* form, int permission
} }
if (pr2) { if (pr2) {
res_query_problem2(conn, result1, result2, result3); res_query_problem2(conn, result1, result2, result3);
free(result2);
} else { } else {
res_query_problem(conn, result1, result3); res_query_problem(conn, result1, result3);
} }
free(result1);
free(result3);
} }
static void impl_modify(mg_connection* conn, problem_form_t* form) static void impl_modify(mg_connection* conn, problem_form_t* form)
@ -210,6 +213,7 @@ static void impl_all(mg_connection* conn, problem_form_t* form)
return; return;
} }
res_all_problems(conn, result); res_all_problems(conn, result);
free(result);
} }
int problems_handler(mg_connection* conn, void* cbdata) int problems_handler(mg_connection* conn, void* cbdata)