use static initialization for output string in RenderCurrentFolder

The dynamic sized variable outputString may not be properly initialized.
Second issue, we fill the line for directories beyond fileNameLength, so
the initialization is not always sufficient.

We now initialize outputString statically with the maximum printable
size which should resolve both.

Also clean up some variable declarations.
This commit is contained in:
Stefan Kalscheuer
2023-06-24 12:21:56 +02:00
parent 4d5ba3899b
commit a9c018f18c

View File

@@ -830,7 +830,6 @@ void AnycubicTouchscreenClass::RenderSpecialMenu(uint16_t selectedNumber) {
void AnycubicTouchscreenClass::RenderCurrentFolder(uint16_t selectedNumber) { void AnycubicTouchscreenClass::RenderCurrentFolder(uint16_t selectedNumber) {
FileList currentFileList; FileList currentFileList;
uint16_t count = selectedNumber;
uint16_t max_files; uint16_t max_files;
uint16_t filesOnSDCard = currentFileList.count(); uint16_t filesOnSDCard = currentFileList.count();
@@ -850,7 +849,7 @@ void AnycubicTouchscreenClass::RenderCurrentFolder(uint16_t selectedNumber) {
selectedNumber = 0; selectedNumber = 0;
} }
for (count = selectedNumber; count <= max_files; count++) { for (uint16_t count = selectedNumber; count <= max_files; count++) {
if (count == 0) { // Special Entry if (count == 0) { // Special Entry
if (currentFileList.isAtRootDir()) { if (currentFileList.isAtRootDir()) {
SENDLINE_PGM(SM_SPECIAL_MENU_S); SENDLINE_PGM(SM_SPECIAL_MENU_S);
@@ -876,25 +875,22 @@ void AnycubicTouchscreenClass::RenderCurrentFolder(uint16_t selectedNumber) {
SERIAL_ECHOLN(currentFileList.filename()); SERIAL_ECHOLN(currentFileList.filename());
#endif #endif
const char* fileName = currentFileList.filename(); const char* fileName = currentFileList.filename();
int fileNameLen = strlen(fileName); unsigned int fileNameLen = strlen(fileName);
// Cut off too long filenames. // Cut off too long filenames.
// They don't fit on the screen anyway. // They don't fit on the screen anyway.
#if ENABLED(KNUTWURST_DGUS2_TFT) #if ENABLED(KNUTWURST_DGUS2_TFT)
bool fileNameWasCut = false; bool fileNameWasCut = false;
if (fileNameLen >= MAX_PRINTABLE_FILENAME_LEN) { if (fileNameLen >= MAX_PRINTABLE_FILENAME_LEN) {
fileNameWasCut = true; fileNameWasCut = true;
fileNameLen = MAX_PRINTABLE_FILENAME_LEN; fileNameLen = MAX_PRINTABLE_FILENAME_LEN;
} }
char outputString[MAX_PRINTABLE_FILENAME_LEN] = {'\0'}; #endif
#else
char outputString[fileNameLen] = {'\0'};
#endif
// Bugfix for non-printable special characters // Bugfix for non-printable special characters which are now replaced by underscores.
// which are now replaced by underscores. static char outputString[MAX_PRINTABLE_FILENAME_LEN] = {'\0'};
for (unsigned char i = 0; i <= fileNameLen; i++) { for (unsigned int i = 0; i <= fileNameLen; i++) {
if (isPrintable(fileName[i])) { if (isPrintable(fileName[i])) {
outputString[i] = fileName[i]; outputString[i] = fileName[i];
} else { } else {