mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-19 22:16:22 +00:00
Rage-cleanup.
While trying to get the unit tests working again I stumbled over all those things that I thought could be better^TM. Now we also know "TODO: why does this make the test fail?" - because the unit tests are brittle AF ... and we have to init the subsystems we use in the test, otherwise the cleanup will fail... BTW. you can now also only run a single test ... or a pattern or so ... you'd have to read how `cmocka_set_test_filter()` works exactly. Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
@@ -1648,6 +1648,7 @@ cmd_ac_uninit(void)
|
||||
autocomplete_free(plugins_reload_ac);
|
||||
autocomplete_free(script_show_ac);
|
||||
g_hash_table_destroy(ac_funcs);
|
||||
ac_funcs = NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
|
||||
@@ -2845,7 +2845,9 @@ _cmd_uninit(void)
|
||||
{
|
||||
cmd_ac_uninit();
|
||||
g_hash_table_destroy(commands);
|
||||
commands = NULL;
|
||||
g_hash_table_destroy(search_index);
|
||||
search_index = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
68
src/log.c
68
src/log.c
@@ -54,6 +54,8 @@
|
||||
|
||||
#define PROF "prof"
|
||||
|
||||
static void _log_msg(log_level_t level, const char* const area, const char* const msg);
|
||||
|
||||
static FILE* logp;
|
||||
static gchar* mainlogfile = NULL;
|
||||
static gboolean user_provided_log = FALSE;
|
||||
@@ -121,14 +123,22 @@ _log_abbreviation_string_from_level(log_level_t level)
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_should_log(log_level_t level)
|
||||
{
|
||||
return level >= level_filter && logp;
|
||||
}
|
||||
|
||||
void
|
||||
log_debug(const char* const msg, ...)
|
||||
{
|
||||
if (!_should_log(PROF_LEVEL_DEBUG))
|
||||
return;
|
||||
va_list arg;
|
||||
va_start(arg, msg);
|
||||
GString* fmt_msg = g_string_new(NULL);
|
||||
g_string_vprintf(fmt_msg, msg, arg);
|
||||
log_msg(PROF_LEVEL_DEBUG, PROF, fmt_msg->str);
|
||||
_log_msg(PROF_LEVEL_DEBUG, PROF, fmt_msg->str);
|
||||
g_string_free(fmt_msg, TRUE);
|
||||
va_end(arg);
|
||||
}
|
||||
@@ -136,11 +146,13 @@ log_debug(const char* const msg, ...)
|
||||
void
|
||||
log_info(const char* const msg, ...)
|
||||
{
|
||||
if (!_should_log(PROF_LEVEL_INFO))
|
||||
return;
|
||||
va_list arg;
|
||||
va_start(arg, msg);
|
||||
GString* fmt_msg = g_string_new(NULL);
|
||||
g_string_vprintf(fmt_msg, msg, arg);
|
||||
log_msg(PROF_LEVEL_INFO, PROF, fmt_msg->str);
|
||||
_log_msg(PROF_LEVEL_INFO, PROF, fmt_msg->str);
|
||||
g_string_free(fmt_msg, TRUE);
|
||||
va_end(arg);
|
||||
}
|
||||
@@ -148,11 +160,13 @@ log_info(const char* const msg, ...)
|
||||
void
|
||||
log_warning(const char* const msg, ...)
|
||||
{
|
||||
if (!_should_log(PROF_LEVEL_WARN))
|
||||
return;
|
||||
va_list arg;
|
||||
va_start(arg, msg);
|
||||
GString* fmt_msg = g_string_new(NULL);
|
||||
g_string_vprintf(fmt_msg, msg, arg);
|
||||
log_msg(PROF_LEVEL_WARN, PROF, fmt_msg->str);
|
||||
_log_msg(PROF_LEVEL_WARN, PROF, fmt_msg->str);
|
||||
g_string_free(fmt_msg, TRUE);
|
||||
va_end(arg);
|
||||
}
|
||||
@@ -160,11 +174,13 @@ log_warning(const char* const msg, ...)
|
||||
void
|
||||
log_error(const char* const msg, ...)
|
||||
{
|
||||
if (!_should_log(PROF_LEVEL_ERROR))
|
||||
return;
|
||||
va_list arg;
|
||||
va_start(arg, msg);
|
||||
GString* fmt_msg = g_string_new(NULL);
|
||||
g_string_vprintf(fmt_msg, msg, arg);
|
||||
log_msg(PROF_LEVEL_ERROR, PROF, fmt_msg->str);
|
||||
_log_msg(PROF_LEVEL_ERROR, PROF, fmt_msg->str);
|
||||
g_string_free(fmt_msg, TRUE);
|
||||
va_end(arg);
|
||||
}
|
||||
@@ -206,28 +222,34 @@ log_close(void)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_log_msg(log_level_t level, const char* const area, const char* const msg)
|
||||
{
|
||||
GDateTime* dt = g_date_time_new_now_local();
|
||||
|
||||
char* level_str = _log_abbreviation_string_from_level(level);
|
||||
|
||||
auto_gchar gchar* date_fmt = g_date_time_format_iso8601(dt);
|
||||
|
||||
fprintf(logp, "%s: %s: %s: %s\n", date_fmt, area, level_str, msg);
|
||||
g_date_time_unref(dt);
|
||||
|
||||
fflush(logp);
|
||||
|
||||
if (prefs_get_boolean(PREF_LOG_ROTATE) && !user_provided_log) {
|
||||
long result = ftell(logp);
|
||||
if (result != -1 && result >= prefs_get_max_log_size()) {
|
||||
_rotate_log_file();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
log_msg(log_level_t level, const char* const area, const char* const msg)
|
||||
{
|
||||
if (level >= level_filter && logp) {
|
||||
GDateTime* dt = g_date_time_new_now_local();
|
||||
|
||||
char* level_str = _log_abbreviation_string_from_level(level);
|
||||
|
||||
auto_gchar gchar* date_fmt = g_date_time_format_iso8601(dt);
|
||||
|
||||
fprintf(logp, "%s: %s: %s: %s\n", date_fmt, area, level_str, msg);
|
||||
g_date_time_unref(dt);
|
||||
|
||||
fflush(logp);
|
||||
|
||||
if (prefs_get_boolean(PREF_LOG_ROTATE) && !user_provided_log) {
|
||||
long result = ftell(logp);
|
||||
if (result != -1 && result >= prefs_get_max_log_size()) {
|
||||
_rotate_log_file();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!_should_log(level))
|
||||
return;
|
||||
_log_msg(level, area, msg);
|
||||
}
|
||||
|
||||
int
|
||||
|
||||
@@ -111,7 +111,7 @@ api_register_command(const char* const plugin_name, const char* command_name, in
|
||||
char** synopsis, const char* description, char* arguments[][2], char** examples,
|
||||
void* callback, void (*callback_exec)(PluginCommand* command, gchar** args), void (*callback_destroy)(void* callback))
|
||||
{
|
||||
PluginCommand* command = malloc(sizeof(PluginCommand));
|
||||
PluginCommand* command = calloc(1, sizeof(PluginCommand));
|
||||
command->command_name = strdup(command_name);
|
||||
command->min_args = min_args;
|
||||
command->max_args = max_args;
|
||||
@@ -119,28 +119,22 @@ api_register_command(const char* const plugin_name, const char* command_name, in
|
||||
command->callback_exec = callback_exec;
|
||||
command->callback_destroy = callback_destroy;
|
||||
|
||||
CommandHelp* help = malloc(sizeof(CommandHelp));
|
||||
|
||||
help->tags[0] = NULL;
|
||||
CommandHelp* help = calloc(1, sizeof(CommandHelp));
|
||||
|
||||
int i;
|
||||
for (i = 0; synopsis[i] != NULL; i++) {
|
||||
help->synopsis[i] = strdup(synopsis[i]);
|
||||
}
|
||||
help->synopsis[i] = NULL;
|
||||
|
||||
help->desc = strdup(description);
|
||||
for (i = 0; arguments[i][0] != NULL; i++) {
|
||||
help->args[i][0] = strdup(arguments[i][0]);
|
||||
help->args[i][1] = strdup(arguments[i][1]);
|
||||
}
|
||||
help->args[i][0] = NULL;
|
||||
help->args[i][1] = NULL;
|
||||
|
||||
for (i = 0; examples[i] != NULL; i++) {
|
||||
help->examples[i] = strdup(examples[i]);
|
||||
}
|
||||
help->examples[i] = NULL;
|
||||
|
||||
command->help = help;
|
||||
|
||||
|
||||
@@ -104,7 +104,8 @@ _free_command(PluginCommand* command)
|
||||
}
|
||||
free(command->command_name);
|
||||
|
||||
_free_command_help(command->help);
|
||||
if (command->help)
|
||||
_free_command_help(command->help);
|
||||
|
||||
free(command);
|
||||
}
|
||||
@@ -118,6 +119,8 @@ _free_command_hash(GHashTable* command_hash)
|
||||
static void
|
||||
_free_timed_function(PluginTimedFunction* timed_function)
|
||||
{
|
||||
if (!timed_function)
|
||||
return;
|
||||
if (timed_function->callback_destroy) {
|
||||
timed_function->callback_destroy(timed_function->callback);
|
||||
}
|
||||
@@ -178,8 +181,11 @@ void
|
||||
callbacks_close(void)
|
||||
{
|
||||
g_hash_table_destroy(p_window_callbacks);
|
||||
p_window_callbacks = NULL;
|
||||
g_hash_table_destroy(p_timed_functions);
|
||||
p_timed_functions = NULL;
|
||||
g_hash_table_destroy(p_commands);
|
||||
p_commands = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -69,12 +69,6 @@ disable_python_threads()
|
||||
PyEval_RestoreThread(thread_state);
|
||||
}
|
||||
|
||||
static void
|
||||
_unref_module(PyObject* module)
|
||||
{
|
||||
Py_XDECREF(module);
|
||||
}
|
||||
|
||||
const char*
|
||||
python_get_version_string(void)
|
||||
{
|
||||
@@ -95,14 +89,15 @@ python_get_version_number(void)
|
||||
void
|
||||
python_env_init(void)
|
||||
{
|
||||
loaded_modules = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_unref_module);
|
||||
loaded_modules = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)Py_XDECREF);
|
||||
|
||||
python_init_prof();
|
||||
|
||||
auto_gchar gchar* plugins_dir = files_get_data_path(DIR_PLUGINS);
|
||||
auto_gchar gchar* path = g_strdup_printf(
|
||||
"import sys\n"
|
||||
"sys.path.append(\"%s/\")\n", plugins_dir);
|
||||
"import sys\n"
|
||||
"sys.path.append(\"%s/\")\n",
|
||||
plugins_dir);
|
||||
|
||||
PyRun_SimpleString(path);
|
||||
python_check_error();
|
||||
|
||||
@@ -298,7 +298,10 @@ _call_and_free_shutdown_routine(struct shutdown_routine* r)
|
||||
void
|
||||
prof_shutdown(void)
|
||||
{
|
||||
g_list_free_full(shutdown_routines, (GDestroyNotify)_call_and_free_shutdown_routine);
|
||||
if (shutdown_routines) {
|
||||
g_list_free_full(shutdown_routines, (GDestroyNotify)_call_and_free_shutdown_routine);
|
||||
shutdown_routines = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
|
||||
@@ -62,12 +62,7 @@ static gchar* _search(Autocomplete ac, GList* curr, gboolean quote, search_direc
|
||||
Autocomplete
|
||||
autocomplete_new(void)
|
||||
{
|
||||
Autocomplete new = malloc(sizeof(struct autocomplete_t));
|
||||
new->items = NULL;
|
||||
new->last_found = NULL;
|
||||
new->search_str = NULL;
|
||||
|
||||
return new;
|
||||
return calloc(1, sizeof(struct autocomplete_t));
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -99,9 +99,10 @@ _ui_close(void)
|
||||
inp_close();
|
||||
status_bar_close();
|
||||
free_title_bar();
|
||||
delwin(main_scr);
|
||||
delscreen(set_term(NULL));
|
||||
endwin();
|
||||
delwin(main_scr);
|
||||
main_scr = NULL;
|
||||
delscreen(set_term(NULL));
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -164,7 +164,6 @@ create_input_window(void)
|
||||
|
||||
inp_win = newpad(1, INP_WIN_MAX);
|
||||
wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));
|
||||
;
|
||||
keypad(inp_win, TRUE);
|
||||
wmove(inp_win, 0, 0);
|
||||
|
||||
@@ -274,7 +273,9 @@ inp_close(void)
|
||||
{
|
||||
rl_callback_handler_remove();
|
||||
delwin(inp_win);
|
||||
inp_win = NULL;
|
||||
fclose(discard);
|
||||
discard = NULL;
|
||||
}
|
||||
|
||||
char*
|
||||
|
||||
@@ -120,6 +120,7 @@ void
|
||||
status_bar_close(void)
|
||||
{
|
||||
delwin(statusbar_win);
|
||||
statusbar_win = NULL;
|
||||
if (statusbar) {
|
||||
if (statusbar->time) {
|
||||
g_free(statusbar->time);
|
||||
@@ -133,10 +134,11 @@ status_bar_close(void)
|
||||
if (statusbar->tabs) {
|
||||
g_hash_table_destroy(statusbar->tabs);
|
||||
}
|
||||
free(statusbar);
|
||||
FREE_SET_NULL(statusbar);
|
||||
}
|
||||
if (tz) {
|
||||
g_time_zone_unref(tz);
|
||||
tz = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,7 @@ void
|
||||
free_title_bar(void)
|
||||
{
|
||||
delwin(win);
|
||||
win = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -1226,8 +1226,11 @@ void
|
||||
wins_destroy(void)
|
||||
{
|
||||
g_hash_table_destroy(windows);
|
||||
windows = NULL;
|
||||
autocomplete_free(wins_ac);
|
||||
wins_ac = NULL;
|
||||
autocomplete_free(wins_close_ac);
|
||||
wins_close_ac = NULL;
|
||||
}
|
||||
|
||||
ProfWin*
|
||||
|
||||
@@ -77,22 +77,30 @@ _free_avatar_data(avatar_metadata* data)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_avatar_cleanup(void)
|
||||
{
|
||||
if (looking_for) {
|
||||
g_hash_table_destroy(looking_for);
|
||||
}
|
||||
if (shall_open) {
|
||||
g_hash_table_destroy(shall_open);
|
||||
}
|
||||
looking_for = NULL;
|
||||
shall_open = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
avatar_pep_subscribe(void)
|
||||
{
|
||||
prof_add_shutdown_routine(_avatar_cleanup);
|
||||
message_pubsub_event_handler_add(STANZA_NS_USER_AVATAR_METADATA, _avatar_metadata_handler, NULL, NULL);
|
||||
message_pubsub_event_handler_add(STANZA_NS_USER_AVATAR_DATA, _avatar_metadata_handler, NULL, NULL);
|
||||
|
||||
// caps_add_feature(XMPP_FEATURE_USER_AVATAR_METADATA_NOTIFY);
|
||||
|
||||
if (looking_for) {
|
||||
g_hash_table_destroy(looking_for);
|
||||
}
|
||||
_avatar_cleanup();
|
||||
looking_for = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
||||
|
||||
if (shall_open) {
|
||||
g_hash_table_destroy(shall_open);
|
||||
}
|
||||
shall_open = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ _chat_session_new(const char* const barejid, const char* const resource, gboolea
|
||||
assert(barejid != NULL);
|
||||
assert(resource != NULL);
|
||||
|
||||
ChatSession* new_session = malloc(sizeof(struct chat_session_t));
|
||||
ChatSession* new_session = calloc(1, sizeof(*new_session));
|
||||
new_session->barejid = strdup(barejid);
|
||||
new_session->resource = strdup(resource);
|
||||
new_session->resource_override = resource_override;
|
||||
@@ -88,8 +88,10 @@ chat_sessions_init(void)
|
||||
void
|
||||
chat_sessions_clear(void)
|
||||
{
|
||||
if (sessions)
|
||||
if (sessions) {
|
||||
g_hash_table_remove_all(sessions);
|
||||
sessions = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@@ -101,7 +103,7 @@ chat_session_resource_override(const char* const barejid, const char* const reso
|
||||
ChatSession*
|
||||
chat_session_get(const char* const barejid)
|
||||
{
|
||||
return g_hash_table_lookup(sessions, barejid);
|
||||
return sessions ? g_hash_table_lookup(sessions, barejid) : NULL;
|
||||
}
|
||||
|
||||
char*
|
||||
|
||||
@@ -320,7 +320,6 @@ void
|
||||
iq_handlers_clear(void)
|
||||
{
|
||||
if (id_handlers) {
|
||||
g_hash_table_remove_all(id_handlers);
|
||||
g_hash_table_destroy(id_handlers);
|
||||
id_handlers = NULL;
|
||||
}
|
||||
@@ -410,7 +409,6 @@ void
|
||||
iq_rooms_cache_clear(void)
|
||||
{
|
||||
if (rooms_cache) {
|
||||
g_hash_table_remove_all(rooms_cache);
|
||||
g_hash_table_destroy(rooms_cache);
|
||||
rooms_cache = NULL;
|
||||
}
|
||||
|
||||
@@ -320,13 +320,9 @@ _handle_form(xmpp_stanza_t* const stanza)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
message_handlers_init(void)
|
||||
static void
|
||||
_message_handlers_cleanup(void)
|
||||
{
|
||||
xmpp_conn_t* const conn = connection_get_conn();
|
||||
xmpp_ctx_t* const ctx = connection_get_ctx();
|
||||
xmpp_handler_add(conn, _message_handler, NULL, STANZA_NAME_MESSAGE, NULL, ctx);
|
||||
|
||||
if (pubsub_event_handlers) {
|
||||
GList* keys = g_hash_table_get_keys(pubsub_event_handlers);
|
||||
GList* curr = keys;
|
||||
@@ -340,7 +336,17 @@ message_handlers_init(void)
|
||||
g_list_free(keys);
|
||||
g_hash_table_destroy(pubsub_event_handlers);
|
||||
}
|
||||
pubsub_event_handlers = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
message_handlers_init(void)
|
||||
{
|
||||
prof_add_shutdown_routine(_message_handlers_cleanup);
|
||||
xmpp_conn_t* const conn = connection_get_conn();
|
||||
xmpp_ctx_t* const ctx = connection_get_ctx();
|
||||
xmpp_handler_add(conn, _message_handler, NULL, STANZA_NAME_MESSAGE, NULL, ctx);
|
||||
_message_handlers_cleanup();
|
||||
pubsub_event_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user