From 7526309ccf38a6384d457f6f15c8f9eed5e9e76c Mon Sep 17 00:00:00 2001 From: Andrew <18502096+classicrocker883@users.noreply.github.com> Date: Fri, 5 Sep 2025 23:00:28 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Tweaks=20to?= =?UTF-8?q?=20our=20aging=20Arduino=20SdFat=20Library=20code=20(#28031)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Scott Lahteine --- Marlin/src/lcd/e3v2/proui/gcode_preview.cpp | 17 ++++++++++------ Marlin/src/sd/SdBaseFile.cpp | 22 ++++++++++++--------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Marlin/src/lcd/e3v2/proui/gcode_preview.cpp b/Marlin/src/lcd/e3v2/proui/gcode_preview.cpp index 045615f3c2..9ad098f149 100644 --- a/Marlin/src/lcd/e3v2/proui/gcode_preview.cpp +++ b/Marlin/src/lcd/e3v2/proui/gcode_preview.cpp @@ -83,7 +83,7 @@ fileprop_t fileprop; void getValue(const char * const buf, PGM_P const key, float &value) { if (value != 0.0f) return; - char *posptr = strstr_P(buf, key); + const char *posptr = strstr_P(buf, key); if (posptr == nullptr) return; char num[10] = ""; @@ -101,8 +101,9 @@ void getValue(const char * const buf, PGM_P const key, float &value) { bool Preview::hasPreview() { const char * const tbstart = PSTR("; thumbnail begin " STRINGIFY(THUMBWIDTH) "x" STRINGIFY(THUMBHEIGHT)); - char *posptr = nullptr; + const char *posptr = nullptr; uint32_t indx = 0; + uint32_t prev_indx = 0; float tmp = 0; fileprop.clear(); @@ -114,7 +115,7 @@ bool Preview::hasPreview() { uint8_t nbyte = 1; while (!fileprop.thumbstart && nbyte > 0 && indx < 4 * sizeof(buf)) { nbyte = card.read(buf, sizeof(buf) - 1); - if (nbyte > 0) { + if (nbyte >= 0) { buf[nbyte] = '\0'; getValue(buf, PSTR(";TIME:"), fileprop.time); getValue(buf, PSTR(";Filament used:"), fileprop.filament); @@ -132,13 +133,17 @@ bool Preview::hasPreview() { fileprop.height -= tmp; posptr = strstr_P(buf, tbstart); if (posptr != nullptr) { - fileprop.thumbstart = indx + (posptr - &buf[0]); + fileprop.thumbstart = indx + (posptr - buf); } else { indx += _MAX(10, nbyte - (signed)strlen_P(tbstart)); + if (indx <= prev_indx) break; + prev_indx = indx; card.setIndex(indx); } } + else + break; } if (!fileprop.thumbstart) { @@ -190,7 +195,7 @@ void Preview::drawFromSD() { return; } - MString<45> buf; + TString buf; dwinDrawRectangle(1, hmiData.colorBackground, 0, 0, DWIN_WIDTH, STATUS_Y - 1); if (fileprop.time) { buf.setf(F("Estimated time: %i:%02i"), (uint16_t)fileprop.time / 3600, ((uint16_t)fileprop.time % 3600) / 60); @@ -210,7 +215,7 @@ void Preview::drawFromSD() { } DWINUI::drawButton(BTN_Print, 26, 290); DWINUI::drawButton(BTN_Cancel, 146, 290); - show(); + if (fileprop.thumbsize) show(); drawSelectHighlight(true, 290); dwinUpdateLCD(); } diff --git a/Marlin/src/sd/SdBaseFile.cpp b/Marlin/src/sd/SdBaseFile.cpp index 3963043ca6..cc14ca9c29 100644 --- a/Marlin/src/sd/SdBaseFile.cpp +++ b/Marlin/src/sd/SdBaseFile.cpp @@ -255,10 +255,12 @@ bool SdBaseFile::exists(const char *name) { * If no data is read, fgets() returns zero for EOF or -1 if an error occurred. */ int16_t SdBaseFile::fgets(char *str, int16_t num, char *delim) { + if (!str || num <= 1) return -1; // Ensure space for at least '\0' + char ch; int16_t n = 0; int16_t r = -1; - while ((n + 1) < num && (r = read(&ch, 1)) == 1) { + while (n < num - 1 && (r = read(&ch, 1)) == 1) { // delete CR if (ch == '\r') continue; str[n++] = ch; @@ -269,10 +271,7 @@ int16_t SdBaseFile::fgets(char *str, int16_t num, char *delim) { if (strchr(delim, ch)) break; } } - if (r < 0) { - // read error - return -1; - } + if (r < 0) return -1; // read error str[n] = '\0'; return n; } @@ -342,7 +341,9 @@ int8_t SdBaseFile::lsPrintNext(const uint8_t flags, const uint8_t indent) { uint8_t w = 0; while (1) { - if (read(&dir, sizeof(dir)) != sizeof(dir)) return 0; + const int16_t r = read(&dir, sizeof(dir)); + if (r < 0) return -1; + if (r != sizeof(dir)) return 0; if (dir.name[0] == DIR_NAME_FREE) return 0; // skip deleted entry and entries for . and .. @@ -1277,7 +1278,8 @@ int SdBaseFile::peek() { filepos_t pos; getpos(&pos); int c = read(); - if (c >= 0) setpos(&pos); + if (c < 0) return -1; + setpos(&pos); return c; } @@ -1346,8 +1348,10 @@ bool SdBaseFile::printName() { * If an error occurs or end of file is reached -1 is returned. */ int16_t SdBaseFile::read() { - uint8_t b; - return read(&b, 1) == 1 ? b : -1; + uint8_t b = 0; + const int16_t r = read(&b, 1); + if (r != 1) return -1; + return static_cast(b); } /**