Bläddra i källkod

Clarify SD longname string size (#14148)

Robby Candra 5 år sedan
förälder
incheckning
5515f88452
2 ändrade filer med 16 tillägg och 11 borttagningar
  1. 9
    7
      Marlin/src/sd/cardreader.cpp
  2. 7
    4
      Marlin/src/sd/cardreader.h

+ 9
- 7
Marlin/src/sd/cardreader.cpp Visa fil

@@ -81,10 +81,10 @@ uint8_t CardReader::workDirDepth;
81 81
         char **CardReader::sortshort, **CardReader::sortnames;
82 82
       #else
83 83
         char CardReader::sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
84
-        char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
84
+        char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
85 85
       #endif
86 86
     #elif DISABLED(SDSORT_USES_STACK)
87
-      char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
87
+      char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
88 88
     #endif
89 89
 
90 90
     #if HAS_FOLDER_SORTING
@@ -760,11 +760,13 @@ void CardReader::setroot() {
760 760
       #endif
761 761
     #else
762 762
       // Copy filenames into the static array
763
-      #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH
764
-        #define SET_SORTNAME(I) do{ strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN); \
765
-                                    sortnames[I][SORTED_LONGNAME_MAXLEN] = '\0'; }while(0)
763
+      #define _SET_SORTNAME(I) strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN)
764
+      #if SORTED_LONGNAME_MAXLEN == LONG_FILENAME_LENGTH
765
+        // Short name sorting always use LONG_FILENAME_LENGTH with no trailing nul
766
+        #define SET_SORTNAME(I) _SET_SORTNAME(I)
766 767
       #else
767
-        #define SET_SORTNAME(I) strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN)
768
+        // Copy multiple name blocks. Add a nul for the longest case.
769
+        #define SET_SORTNAME(I) do{ _SET_SORTNAME(I); sortnames[I][SORTED_LONGNAME_MAXLEN] = '\0'; }while(0)
768 770
       #endif
769 771
       #if ENABLED(SDSORT_CACHE_NAMES)
770 772
         #define SET_SORTSHORT(I) strcpy(sortshort[I], filename)
@@ -816,7 +818,7 @@ void CardReader::setroot() {
816 818
             sortnames = new char*[fileCnt];
817 819
           #endif
818 820
         #elif ENABLED(SDSORT_USES_STACK)
819
-          char sortnames[fileCnt][SORTED_LONGNAME_MAXLEN];
821
+          char sortnames[fileCnt][SORTED_LONGNAME_STORAGE];
820 822
         #endif
821 823
 
822 824
         // Folder sorting needs 1 bit per entry for flags.

+ 7
- 4
Marlin/src/sd/cardreader.h Visa fil

@@ -175,9 +175,11 @@ private:
175 175
     #endif
176 176
 
177 177
     #if BOTH(SDSORT_USES_RAM, SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
178
-      #define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1)
178
+      #define SORTED_LONGNAME_MAXLEN (SDSORT_CACHE_VFATS) * (FILENAME_LENGTH)
179
+      #define SORTED_LONGNAME_STORAGE (SORTED_LONGNAME_MAXLEN + 1)
179 180
     #else
180 181
       #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
182
+      #define SORTED_LONGNAME_STORAGE SORTED_LONGNAME_MAXLEN
181 183
     #endif
182 184
 
183 185
     // Cache filenames to speed up SD menus.
@@ -189,10 +191,11 @@ private:
189 191
           static char **sortshort, **sortnames;
190 192
         #else
191 193
           static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
192
-          static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
193 194
         #endif
194
-      #elif DISABLED(SDSORT_USES_STACK)
195
-        static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
195
+      #endif
196
+
197
+      #if (ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)) || NONE(SDSORT_CACHE_NAMES, SDSORT_USES_STACK)
198
+        static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
196 199
       #endif
197 200
 
198 201
       // Folder sorting uses an isDir array when caching items.

Laddar…
Avbryt
Spara