fix(mam, log): improve datetime handling and memory management in MAM and log fetching
- Introduce static helper `_truncate_datetime_suffix()` to safely trim datetime strings, removing unwanted suffixes like timezone offsets - Replace manual string management with auto_gchar and g_strdup for safer, clearer ownership and to prevent leaks - Add safety checks and logging warnings for unexpected datetime string lengths or null pointers - Refactor _mam_rsm_id_handler to use the helper function and updated string handling - Change log_database_get_previous_chat parameters for consistent ownership semantics, avoiding double frees and mem leaks - Overall improve stability and prevent memory leaks during log database queries
This commit is contained in:
@@ -320,7 +320,7 @@ log_database_get_limits_info(const gchar* const contact_barejid, gboolean is_las
|
||||
// null the current time is used. from_start gets first few messages if true
|
||||
// otherwise the last ones. Flip flips the order of the results
|
||||
db_history_result_t
|
||||
log_database_get_previous_chat(const gchar* const contact_barejid, const char* start_time, char* end_time, gboolean from_start, gboolean flip, GSList** result)
|
||||
log_database_get_previous_chat(const gchar* const contact_barejid, const gchar* start_time, const gchar* end_time, gboolean from_start, gboolean flip, GSList** result)
|
||||
{
|
||||
if (!g_chatlog_database) {
|
||||
log_warning("log_database_get_previous_chat() called but db is not initialized");
|
||||
@@ -337,7 +337,7 @@ log_database_get_previous_chat(const gchar* const contact_barejid, const char* s
|
||||
gchar* sort1 = from_start ? "ASC" : "DESC";
|
||||
gchar* sort2 = !flip ? "ASC" : "DESC";
|
||||
GDateTime* now = g_date_time_new_now_local();
|
||||
auto_gchar gchar* end_date_fmt = end_time ? end_time : g_date_time_format_iso8601(now);
|
||||
auto_gchar gchar* end_date_fmt = end_time ? g_strdup(end_time) : g_date_time_format_iso8601(now);
|
||||
auto_sqlite gchar* query = sqlite3_mprintf("SELECT * FROM ("
|
||||
"SELECT COALESCE(B.`message`, A.`message`) AS message, "
|
||||
"A.`timestamp`, A.`from_jid`, A.`from_resource`, A.`to_jid`, A.`to_resource`, A.`type`, A.`encryption`, A.`stanza_id` FROM `ChatLogs` AS A "
|
||||
|
||||
@@ -53,7 +53,7 @@ void log_database_add_incoming(ProfMessage* message);
|
||||
void log_database_add_outgoing_chat(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc);
|
||||
void log_database_add_outgoing_muc(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc);
|
||||
void log_database_add_outgoing_muc_pm(const char* const id, const char* const barejid, const char* const message, const char* const replace_id, prof_enc_t enc);
|
||||
db_history_result_t log_database_get_previous_chat(const gchar* const contact_barejid, const char* start_time, char* end_time, gboolean from_start, gboolean flip, GSList** result);
|
||||
db_history_result_t log_database_get_previous_chat(const gchar* const contact_barejid, const gchar* start_time, const gchar* end_time, gboolean from_start, gboolean flip, GSList** result);
|
||||
ProfMessage* log_database_get_limits_info(const gchar* const contact_barejid, gboolean is_last);
|
||||
void log_database_close(void);
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "glib.h"
|
||||
#include "xmpp/chat_session.h"
|
||||
#include "window_list.h"
|
||||
#include "xmpp/roster_list.h"
|
||||
@@ -585,10 +586,12 @@ _chatwin_history(ProfChatWin* chatwin, const char* const contact_barejid)
|
||||
// first entry's timestamp in the buffer is used. Flip true to prepend to buffer.
|
||||
// Timestamps should be in iso8601
|
||||
db_history_result_t
|
||||
chatwin_db_history(ProfChatWin* chatwin, const char* start_time, char* end_time, gboolean flip)
|
||||
chatwin_db_history(ProfChatWin* chatwin, const gchar* start_time, const gchar* end_time, gboolean flip)
|
||||
{
|
||||
if (!end_time) {
|
||||
end_time = buffer_size(((ProfWin*)chatwin)->layout->buffer) == 0 ? NULL : g_date_time_format_iso8601(buffer_get_entry(((ProfWin*)chatwin)->layout->buffer, 0)->time);
|
||||
auto_gchar gchar* _end_time = NULL;
|
||||
if (!end_time && buffer_size(((ProfWin*)chatwin)->layout->buffer) > 0) {
|
||||
_end_time = g_date_time_format_iso8601(buffer_get_entry(((ProfWin*)chatwin)->layout->buffer, 0)->time);
|
||||
end_time = _end_time;
|
||||
}
|
||||
|
||||
GSList* history = NULL;
|
||||
|
||||
@@ -146,7 +146,7 @@ void chatwin_set_incoming_char(ProfChatWin* chatwin, const char* const ch);
|
||||
void chatwin_unset_incoming_char(ProfChatWin* chatwin);
|
||||
void chatwin_set_outgoing_char(ProfChatWin* chatwin, const char* const ch);
|
||||
void chatwin_unset_outgoing_char(ProfChatWin* chatwin);
|
||||
db_history_result_t chatwin_db_history(ProfChatWin* chatwin, const char* start_time, char* end_time, gboolean flip);
|
||||
db_history_result_t chatwin_db_history(ProfChatWin* chatwin, const gchar* start_time, const gchar* end_time, gboolean flip);
|
||||
|
||||
// MUC window
|
||||
ProfMucWin* mucwin_new(const char* const barejid);
|
||||
|
||||
@@ -711,19 +711,17 @@ win_page_down(ProfWin* window, int scroll_size)
|
||||
if ((*page_start > total_rows - page_space || (*page_start == page_space && *page_start >= total_rows)) && window->type == WIN_CHAT) {
|
||||
int bf_size = buffer_size(window->layout->buffer);
|
||||
if (bf_size > 0) {
|
||||
GDateTime* now = g_date_time_new_now_local();
|
||||
ProfBuffEntry* last_entry = buffer_get_entry(window->layout->buffer, bf_size - 1);
|
||||
auto_gchar gchar* start = g_date_time_format_iso8601(last_entry->time);
|
||||
GDateTime* now = g_date_time_new_now_local();
|
||||
gchar* end_date = g_date_time_format_iso8601(now);
|
||||
g_date_time_unref(now);
|
||||
auto_gchar gchar* end_date = g_date_time_format_iso8601(now);
|
||||
if (*scroll_state != WIN_SCROLL_REACHED_BOTTOM && !chatwin_db_history((ProfChatWin*)window, start, end_date, FALSE)) {
|
||||
*scroll_state = WIN_SCROLL_REACHED_BOTTOM;
|
||||
} else if (*scroll_state == WIN_SCROLL_REACHED_BOTTOM) {
|
||||
g_free(end_date);
|
||||
}
|
||||
|
||||
int offset = last_entry->y_end_pos - 1;
|
||||
*page_start = offset - page_space + scroll_size;
|
||||
g_date_time_unref(now);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2752,6 +2752,22 @@ iq_mam_request(ProfChatWin* win, GDateTime* enddate)
|
||||
return;
|
||||
}
|
||||
|
||||
// Trim the last 3 characters from the datetime string to remove unwanted suffix (timezone offset)
|
||||
static void
|
||||
_truncate_datetime_suffix(gchar* str)
|
||||
{
|
||||
if (!str) {
|
||||
log_warning("_truncate_datetime_suffix(): unexpected null datetime string");
|
||||
return;
|
||||
}
|
||||
size_t len = strlen(str);
|
||||
if (len >= 3) {
|
||||
str[len - 3] = '\0';
|
||||
} else {
|
||||
log_warning("_truncate_datetime_suffix(): unexpected short datetime string: %s", str);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
_mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
|
||||
{
|
||||
@@ -2773,17 +2789,17 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
|
||||
|
||||
buffer_remove_entry(window->layout->buffer, 0);
|
||||
|
||||
auto_char char* start_str = NULL;
|
||||
auto_gchar gchar* start_str = NULL;
|
||||
if (data->start_datestr) {
|
||||
start_str = strdup(data->start_datestr);
|
||||
start_str = g_strdup(data->start_datestr);
|
||||
// Convert to iso8601
|
||||
start_str[strlen(start_str) - 3] = '\0';
|
||||
_truncate_datetime_suffix(start_str);
|
||||
}
|
||||
char* end_str = NULL;
|
||||
auto_gchar gchar* end_str = NULL;
|
||||
if (data->end_datestr) {
|
||||
end_str = strdup(data->end_datestr);
|
||||
end_str = g_strdup(data->end_datestr);
|
||||
// Convert to iso8601
|
||||
end_str[strlen(end_str) - 3] = '\0';
|
||||
_truncate_datetime_suffix(end_str);
|
||||
}
|
||||
|
||||
if (is_complete || !data->fetch_next) {
|
||||
|
||||
@@ -644,7 +644,7 @@ _available_handler(xmpp_stanza_t* const stanza)
|
||||
const char* account_name = session_get_account_name();
|
||||
int max_sessions = accounts_get_max_sessions(account_name);
|
||||
if (max_sessions > 0) {
|
||||
const gchar* cur_resource = accounts_get_resource(account_name);
|
||||
auto_gchar gchar* cur_resource = accounts_get_resource(account_name);
|
||||
int res_count = connection_count_available_resources();
|
||||
if (res_count > max_sessions && g_strcmp0(cur_resource, resource->name)) {
|
||||
ProfWin* console = wins_get_console();
|
||||
|
||||
Reference in New Issue
Block a user