From ad28bb78d2ef9cb5dae20b6366fdbed7b9ca0083 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Thu, 16 Jul 2026 19:32:31 +1000 Subject: [PATCH] AP_Filesystem: correct fgets for last-line-missing-termination refusal to seek past the end of the file broke this --- libraries/AP_Filesystem/AP_Filesystem.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libraries/AP_Filesystem/AP_Filesystem.cpp b/libraries/AP_Filesystem/AP_Filesystem.cpp index 65e3986ddf6..21a838cd0c2 100644 --- a/libraries/AP_Filesystem/AP_Filesystem.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem.cpp @@ -365,8 +365,13 @@ bool AP_Filesystem::fgets(char *buf, uint8_t buflen, int fd) } buf[i] = '\0'; - // get back to the right offset - if (backend.fs.lseek(fd, offset_start+i+1, SEEK_SET) != offset_start+i+1) { + // get back to the right offset, consuming the line terminator if + // we found one. If we did not find one we have either filled the + // buffer or returned an unterminated final line; in both cases + // the seek target must not extend past the data we consumed - + // backends such as ROMFS refuse to seek past the end of the file. + const int32_t new_offset = offset_start + i + (i < n ? 1 : 0); + if (backend.fs.lseek(fd, new_offset, SEEK_SET) != new_offset) { // we need to fail if we can't seek back or the caller may loop or get corrupt data return false; }