diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index 72b15410fe..5a17c0633a 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -81,10 +81,10 @@ uint8_t CardReader::workDirDepth; char **CardReader::sortshort, **CardReader::sortnames; #else char CardReader::sortshort[SDSORT_LIMIT][FILENAME_LENGTH]; - char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN]; + char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE]; #endif #elif DISABLED(SDSORT_USES_STACK) - char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN]; + char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE]; #endif #if HAS_FOLDER_SORTING @@ -760,11 +760,13 @@ void CardReader::setroot() { #endif #else // Copy filenames into the static array - #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH - #define SET_SORTNAME(I) do{ strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN); \ - sortnames[I][SORTED_LONGNAME_MAXLEN] = '\0'; }while(0) + #define _SET_SORTNAME(I) strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN) + #if SORTED_LONGNAME_MAXLEN == LONG_FILENAME_LENGTH + // Short name sorting always use LONG_FILENAME_LENGTH with no trailing nul + #define SET_SORTNAME(I) _SET_SORTNAME(I) #else - #define SET_SORTNAME(I) strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN) + // Copy multiple name blocks. Add a nul for the longest case. + #define SET_SORTNAME(I) do{ _SET_SORTNAME(I); sortnames[I][SORTED_LONGNAME_MAXLEN] = '\0'; }while(0) #endif #if ENABLED(SDSORT_CACHE_NAMES) #define SET_SORTSHORT(I) strcpy(sortshort[I], filename) @@ -816,7 +818,7 @@ void CardReader::setroot() { sortnames = new char*[fileCnt]; #endif #elif ENABLED(SDSORT_USES_STACK) - char sortnames[fileCnt][SORTED_LONGNAME_MAXLEN]; + char sortnames[fileCnt][SORTED_LONGNAME_STORAGE]; #endif // Folder sorting needs 1 bit per entry for flags. diff --git a/Marlin/src/sd/cardreader.h b/Marlin/src/sd/cardreader.h index 5bdbffaf20..623ca4dc7f 100644 --- a/Marlin/src/sd/cardreader.h +++ b/Marlin/src/sd/cardreader.h @@ -175,9 +175,11 @@ private: #endif #if BOTH(SDSORT_USES_RAM, SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM) - #define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1) + #define SORTED_LONGNAME_MAXLEN (SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + #define SORTED_LONGNAME_STORAGE (SORTED_LONGNAME_MAXLEN + 1) #else #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH + #define SORTED_LONGNAME_STORAGE SORTED_LONGNAME_MAXLEN #endif // Cache filenames to speed up SD menus. @@ -189,10 +191,11 @@ private: static char **sortshort, **sortnames; #else static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH]; - static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN]; #endif - #elif DISABLED(SDSORT_USES_STACK) - static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN]; + #endif + + #if (ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)) || NONE(SDSORT_CACHE_NAMES, SDSORT_USES_STACK) + static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE]; #endif // Folder sorting uses an isDir array when caching items.