fix(mavlink): reset the log entry on LOG_ERASE [v1.18] (#29855)

* fix(mavlink): reset the log entry on LOG_ERASE (#29831)

LOG_ERASE closed the open log file but kept the entry and the log count,
so a following LOG_REQUEST_DATA for the same id went to SendingData
without a file and crashed in ftell(nullptr). A failed fopen() and a
failed fseek() left the handler in the same state.

Reported by @KANG982.

Assisted-by: Claude:claude-opus-5-5

Signed-off-by: Julian Oes <julian@oes.ch>
(cherry picked from commit d694bf0d53)

* refactor(mavlink): reuse close_current_entry() in close_files()

Signed-off-by: Julian Oes <julian@oes.ch>

---------

Signed-off-by: Julian Oes <julian@oes.ch>
This commit is contained in:
Julian Oes authored and GitHub committed 2026-11-18 13:54:27 +13:00
1 parent 6d1773f4c1
commit ac8af487d2
2 files changed
+26 -30

No files matched your search

+25 -30
View File
@@ -117,34 +117,27 @@ void MavlinkLogHandler::handle_message(const mavlink_message_t *msg)
void MavlinkLogHandler::close_files()
{
if (_current_entry.fp) {
fclose(_current_entry.fp);
_current_entry.fp = nullptr;
}
_current_entry = LogEntry{};
_entry_request = LogEntryRequest{};
close_current_entry();
_state = LogHandlerState::Idle;
}
void MavlinkLogHandler::state_idle()
{
if (_current_entry.fp && _file_send_finished) {
fclose(_current_entry.fp);
_current_entry.fp = nullptr;
_current_entry.id = 0xffff;
_current_entry.time_utc = 0;
_current_entry.size_bytes = 0;
_current_entry.filepath[0] = 0;
_current_entry.offset = 0;
_entry_request.id = 0xffff;
_entry_request.start_offset = 0;
_entry_request.byte_count = 0;
close_current_entry();
}
}
void MavlinkLogHandler::close_current_entry()
{
if (_current_entry.fp) {
fclose(_current_entry.fp);
}
_current_entry = LogEntry{};
_entry_request = LogEntryRequest{};
}
void MavlinkLogHandler::state_listing()
{
static constexpr uint32_t MAVLINK_PACKET_SIZE = MAVLINK_NUM_NON_PAYLOAD_BYTES + MAVLINK_MSG_ID_LOG_ENTRY_LEN;
@@ -224,14 +217,19 @@ void MavlinkLogHandler::state_sending_data()
while (_mavlink.get_free_tx_buf() > MAVLINK_PACKET_SIZE && bytes_sent < MAX_BYTES_BURST) {
if (!_current_entry.fp) {
_state = LogHandlerState::Idle;
return;
}
// Only seek if we need to
long int offset = _current_entry.offset - ftell(_current_entry.fp);
if (offset && fseek(_current_entry.fp, offset, SEEK_CUR)) {
fclose(_current_entry.fp);
_current_entry.fp = nullptr;
PX4_DEBUG("seek error");
close_current_entry();
_state = LogHandlerState::Idle;
return;
}
// Prepare mavlink message
@@ -312,11 +310,7 @@ void MavlinkLogHandler::handle_log_request_data(const mavlink_message_t *msg)
// Handle switching to new request ID
if (request.id != _current_entry.id) {
// Close the old file
if (_current_entry.fp) {
fclose(_current_entry.fp);
_current_entry.fp = nullptr;
}
close_current_entry();
LogEntry entry = {};
@@ -331,6 +325,7 @@ void MavlinkLogHandler::handle_log_request_data(const mavlink_message_t *msg)
if (!entry.fp) {
PX4_DEBUG("Failed to open file %s", entry.filepath);
_state = LogHandlerState::Idle;
return;
}
@@ -375,10 +370,10 @@ void MavlinkLogHandler::handle_log_erase(const mavlink_message_t *msg)
return;
}
if (_current_entry.fp) {
fclose(_current_entry.fp);
_current_entry.fp = nullptr;
}
// Forget the open entry and the listing, the files behind them are about to go.
close_current_entry();
_num_logs = 0;
_logs_listed = false;
_state = LogHandlerState::Idle;
unlink(kLogListFilePath);
@@ -104,6 +104,7 @@ private:
// state functions
void state_idle();
void close_current_entry();
void state_listing();
void state_sending_data();