diff --git a/src/database_flatfile.c b/src/database_flatfile.c index b4c72740..91114693 100644 --- a/src/database_flatfile.c +++ b/src/database_flatfile.c @@ -54,7 +54,6 @@ ff_state_new(const char* filepath) { ff_contact_state_t* state = g_malloc0(sizeof(ff_contact_state_t)); state->filepath = g_strdup(filepath); - state->cursor_offset = -1; state->archive_ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); state->stanza_senders = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); return state; @@ -294,7 +293,6 @@ ff_state_ensure_fresh(ff_contact_state_t* state) if (st.st_ino == state->stamp.inode && st.st_size > state->stamp.size) return _ff_state_extend(state); - state->cursor_offset = -1; return _ff_state_build(state); } @@ -726,10 +724,6 @@ _flatfile_get_previous_chat(const gchar* const contact_barejid, const gchar* sta if (state->n_entries == 0) return DB_RESPONSE_EMPTY; - // Reset cursor for filtered (non-Page-Up) queries - if (start_time) - state->cursor_offset = -1; - // Determine read byte-range using the sparse index off_t read_from = state->bom_len; off_t read_to = state->stamp.size; @@ -789,6 +783,15 @@ _flatfile_get_previous_chat(const gchar* const contact_barejid, const gchar* sta read_from = backed; } + // Defensive: an empty or inverted range means there is nothing to read. + // Reachable when start_time bisects past end_time, or when an end_time + // upper-bound shrinks read_to below the BOM-adjusted read_from. + if (read_from >= read_to) { + log_debug("flatfile: empty range [%ld, %ld) for %s", + (long)read_from, (long)read_to, contact_barejid); + return DB_RESPONSE_EMPTY; + } + // Open file and read lines in [read_from, read_to) FILE* fp = fopen(state->filepath, "r"); if (!fp) @@ -861,10 +864,6 @@ _flatfile_get_previous_chat(const gchar* const contact_barejid, const gchar* sta if (!all_parsed) return DB_RESPONSE_EMPTY; - // Update cursor: byte offset of oldest parsed message in this batch - ff_parsed_line_t* oldest = all_parsed->data; - state->cursor_offset = oldest->file_offset; - // Apply LMC corrections (if enabled) and build ProfMessage list GSList* pre_result = NULL; if (prefs_get_boolean(PREF_CORRECTION_ALLOW)) { diff --git a/src/database_flatfile.h b/src/database_flatfile.h index c0785328..c0c978f5 100644 --- a/src/database_flatfile.h +++ b/src/database_flatfile.h @@ -88,7 +88,6 @@ typedef struct size_t cap_entries; size_t total_lines; ff_file_stamp_t stamp; - off_t cursor_offset; int bom_len; GHashTable* archive_ids; // set: archive_id -> NULL (MAM dedup, O(1)) GHashTable* stanza_senders; // map: stanza_id -> from_jid (LMC validation, O(1))