From 8e64c3bbb161d97f97024f189501fa15b34e4b9d Mon Sep 17 00:00:00 2001 From: philmoz Date: Thu, 6 Jun 2024 22:20:25 +1000 Subject: [PATCH] fix(radio): remove invalid characters from log filenames (#5129) --- radio/src/logs.cpp | 39 +++++++++++---------------------------- radio/src/strhelpers.cpp | 15 +++++++++++++++ radio/src/strhelpers.h | 2 ++ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/radio/src/logs.cpp b/radio/src/logs.cpp index 28ea184212d..f708337317e 100644 --- a/radio/src/logs.cpp +++ b/radio/src/logs.cpp @@ -115,58 +115,41 @@ void logsInit() const char * logsOpen() { + if (!sdMounted()) + return STR_NO_SDCARD; + // Determine and set log file filename FRESULT result; // /LOGS/modelnamexxxxxx_YYYY-MM-DD-HHMMSS.log char filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME + 18 + 4 + 1]; - if (!sdMounted()) - return STR_NO_SDCARD; - // check and create folder here - strcpy(filename, STR_LOGS_PATH); + char* tmp = strAppend(filename, STR_LOGS_PATH); const char * error = sdCheckAndCreateDirectory(filename); if (error) { return error; } - filename[sizeof(LOGS_PATH) - 1] = '/'; - memcpy(&filename[sizeof(LOGS_PATH)], g_model.header.name, sizeof(g_model.header.name)); - filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME] = '\0'; - - uint8_t i = sizeof(LOGS_PATH) + LEN_MODEL_NAME - 1; - uint8_t len = 0; - while (i > sizeof(LOGS_PATH) - 1) { - if (!len && filename[i]) - len = i+1; - if (len) { - if (!filename[i]) - filename[i] = '_'; - } - i--; - } - - if (len == 0) { + tmp = strAppend(tmp, "/"); + if (g_model.header.name[0]) { + tmp = strAppend(tmp, sanitizeForFilename(g_model.header.name, LEN_MODEL_NAME)); + } else { #if defined(EEPROM) uint8_t num = g_eeGeneral.currModel + 1; #else // TODO uint8_t num = 1; #endif - strcpy(&filename[sizeof(LOGS_PATH)], STR_MODEL); - filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL)] = (char)((num / 10) + '0'); - filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 1] = (char)((num % 10) + '0'); - len = sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 2; + tmp = strAppend(tmp, STR_MODEL); + tmp = strAppendUnsigned(tmp, num, 2); } - char * tmp = &filename[len]; - #if defined(RTCLOCK) tmp = strAppendDate(tmp, true); #endif - strcpy(tmp, STR_LOGS_EXT); + strAppend(tmp, STR_LOGS_EXT); result = f_open(&g_oLogFile, filename, FA_OPEN_ALWAYS | FA_WRITE | FA_OPEN_APPEND); if (result != FR_OK) { diff --git a/radio/src/strhelpers.cpp b/radio/src/strhelpers.cpp index 66433e687da..a8739f41068 100644 --- a/radio/src/strhelpers.cpp +++ b/radio/src/strhelpers.cpp @@ -33,6 +33,21 @@ static char _static_str_buffer[32]; static const char s_charTab[] = "_-.,"; +const char* sanitizeForFilename(const char* name, int len) +{ + strAppend(_static_str_buffer, name, len); + + char *s = _static_str_buffer; + // Remove invalid characters in filename + for (int i = 0; s[i]; i += 1) + if (s[i] == '"' || s[i] == ':' || s[i] == '\\' || + s[i] == '/' || s[i] == '<' || s[i] == '>' || + s[i] == '?' || s[i] == '*') + s[i] = '_'; + + return _static_str_buffer; +} + char hex2zchar(uint8_t hex) { return (hex >= 10 ? hex - 9 : 27 + hex); } char hex2char(uint8_t hex) { return (hex >= 10 ? hex - 10 + 'A' : hex + '0'); } diff --git a/radio/src/strhelpers.h b/radio/src/strhelpers.h index c54a9900bad..532372b6513 100644 --- a/radio/src/strhelpers.h +++ b/radio/src/strhelpers.h @@ -56,6 +56,8 @@ typedef union { TimerDisplayOptions displayOptions; } TimerOptions; +const char* sanitizeForFilename(const char* name, int len); + char hex2zchar(uint8_t hex); char hex2char(uint8_t hex); char zchar2char(int8_t idx);