Skip to content

Commit 8e64c3b

Browse files
authored
fix(radio): remove invalid characters from log filenames (#5129)
1 parent 8087283 commit 8e64c3b

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

radio/src/logs.cpp

+11-28
Original file line numberDiff line numberDiff line change
@@ -115,58 +115,41 @@ void logsInit()
115115

116116
const char * logsOpen()
117117
{
118+
if (!sdMounted())
119+
return STR_NO_SDCARD;
120+
118121
// Determine and set log file filename
119122
FRESULT result;
120123

121124
// /LOGS/modelnamexxxxxx_YYYY-MM-DD-HHMMSS.log
122125
char filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME + 18 + 4 + 1];
123126

124-
if (!sdMounted())
125-
return STR_NO_SDCARD;
126-
127127
// check and create folder here
128-
strcpy(filename, STR_LOGS_PATH);
128+
char* tmp = strAppend(filename, STR_LOGS_PATH);
129129
const char * error = sdCheckAndCreateDirectory(filename);
130130
if (error) {
131131
return error;
132132
}
133133

134-
filename[sizeof(LOGS_PATH) - 1] = '/';
135-
memcpy(&filename[sizeof(LOGS_PATH)], g_model.header.name, sizeof(g_model.header.name));
136-
filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME] = '\0';
137-
138-
uint8_t i = sizeof(LOGS_PATH) + LEN_MODEL_NAME - 1;
139-
uint8_t len = 0;
140-
while (i > sizeof(LOGS_PATH) - 1) {
141-
if (!len && filename[i])
142-
len = i+1;
143-
if (len) {
144-
if (!filename[i])
145-
filename[i] = '_';
146-
}
147-
i--;
148-
}
149-
150-
if (len == 0) {
134+
tmp = strAppend(tmp, "/");
135+
if (g_model.header.name[0]) {
136+
tmp = strAppend(tmp, sanitizeForFilename(g_model.header.name, LEN_MODEL_NAME));
137+
} else {
151138
#if defined(EEPROM)
152139
uint8_t num = g_eeGeneral.currModel + 1;
153140
#else
154141
// TODO
155142
uint8_t num = 1;
156143
#endif
157-
strcpy(&filename[sizeof(LOGS_PATH)], STR_MODEL);
158-
filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL)] = (char)((num / 10) + '0');
159-
filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 1] = (char)((num % 10) + '0');
160-
len = sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 2;
144+
tmp = strAppend(tmp, STR_MODEL);
145+
tmp = strAppendUnsigned(tmp, num, 2);
161146
}
162147

163-
char * tmp = &filename[len];
164-
165148
#if defined(RTCLOCK)
166149
tmp = strAppendDate(tmp, true);
167150
#endif
168151

169-
strcpy(tmp, STR_LOGS_EXT);
152+
strAppend(tmp, STR_LOGS_EXT);
170153

171154
result = f_open(&g_oLogFile, filename, FA_OPEN_ALWAYS | FA_WRITE | FA_OPEN_APPEND);
172155
if (result != FR_OK) {

radio/src/strhelpers.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@
3333
static char _static_str_buffer[32];
3434
static const char s_charTab[] = "_-.,";
3535

36+
const char* sanitizeForFilename(const char* name, int len)
37+
{
38+
strAppend(_static_str_buffer, name, len);
39+
40+
char *s = _static_str_buffer;
41+
// Remove invalid characters in filename
42+
for (int i = 0; s[i]; i += 1)
43+
if (s[i] == '"' || s[i] == ':' || s[i] == '\\' ||
44+
s[i] == '/' || s[i] == '<' || s[i] == '>' ||
45+
s[i] == '?' || s[i] == '*')
46+
s[i] = '_';
47+
48+
return _static_str_buffer;
49+
}
50+
3651
char hex2zchar(uint8_t hex) { return (hex >= 10 ? hex - 9 : 27 + hex); }
3752

3853
char hex2char(uint8_t hex) { return (hex >= 10 ? hex - 10 + 'A' : hex + '0'); }

radio/src/strhelpers.h

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ typedef union {
5656
TimerDisplayOptions displayOptions;
5757
} TimerOptions;
5858

59+
const char* sanitizeForFilename(const char* name, int len);
60+
5961
char hex2zchar(uint8_t hex);
6062
char hex2char(uint8_t hex);
6163
char zchar2char(int8_t idx);

0 commit comments

Comments
 (0)