Skip to content

Commit 51e6b5b

Browse files
committed
Use strlcpy with buffer size
1 parent fe7203e commit 51e6b5b

File tree

14 files changed

+51
-61
lines changed

14 files changed

+51
-61
lines changed

Marlin/src/HAL/LINUX/include/Arduino.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
#include <pinmapping.h>
3030

31+
#define strlcpy(A, B, C) strncpy(A, B, (C) - 1)
32+
#define strlcpy_P(A, B, C) strncpy_P(A, B, (C) - 1)
33+
3134
#define HIGH 0x01
3235
#define LOW 0x00
3336

Marlin/src/core/mstring.h

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#define DEFAULT_MSTRING_SIZE 20
4949
#endif
5050

51-
//#define UNSAFE_MSTRING // Don't initialize the string and don't terminate strncpy
51+
//#define UNSAFE_MSTRING // Don't initialize the string to "" or set a terminating nul
5252
//#define USE_SPRINTF // Use sprintf instead of snprintf
5353
//#define DJB2_HASH // 32-bit hash with Djb2 algorithm
5454
//#define MSTRING_DEBUG // Debug string operations to diagnose memory leaks
@@ -98,26 +98,20 @@ class MString {
9898

9999
void debug(FSTR_P const f) {
100100
#if ENABLED(MSTRING_DEBUG)
101-
SERIAL_ECHO(FTOP(f));
102-
SERIAL_CHAR(':');
103-
SERIAL_ECHO(uintptr_t(str));
104-
SERIAL_CHAR(' ');
105-
SERIAL_ECHO(length());
106-
SERIAL_CHAR(' ');
107-
SERIAL_ECHOLN(str);
101+
SERIAL_ECHOLN(f, ':', uintptr_t(str), ' ', length(), ' ', str);
108102
#endif
109103
}
110104

111105
void safety(const int n) { if (SAFE && n <= SIZE) str[n] = '\0'; }
112106

113107
// Chainable String Setters
114108
MString& set() { str[0] = '\0'; debug(F("clear")); return *this; }
115-
MString& set(char *s) { strncpy(str, s, SIZE); debug(F("string")); return *this; }
109+
MString& set(char *s) { strlcpy(str, s, SIZE + 1); debug(F("string")); return *this; }
116110
MString& set(const char *s) { return set(const_cast<char*>(s)); }
117-
MString& set_P(PGM_P const s) { strncpy_P(str, s, SIZE); debug(F("pstring")); return *this; }
111+
MString& set_P(PGM_P const s) { strlcpy_P(str, s, SIZE + 1); debug(F("pstring")); return *this; }
118112
MString& set(FSTR_P const f) { return set_P(FTOP(f)); }
119113
MString& set(const bool &b) { return set(b ? F("true") : F("false")); }
120-
MString& set(const char c) { str[0] = c; if (1 < SIZE) str[1] = '\0'; debug(F("char")); return *this; }
114+
MString& set(const char c) { str[0] = c; str[1] = '\0'; debug(F("char")); return *this; }
121115
MString& set(const int8_t &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("int8_t")); return *this; }
122116
MString& set(const short &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("short")); return *this; }
123117
MString& set(const int &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("int")); return *this; }
@@ -134,11 +128,11 @@ class MString {
134128
MString& set(const xyze_pos_t &v) { set(); return append(v); }
135129

136130
template <int S>
137-
MString& set(const MString<S> &m) { strncpy(str, &m, SIZE); debug(F("MString")); return *this; }
131+
MString& set(const MString<S> &m) { strlcpy(str, &m, SIZE + 1); debug(F("MString")); return *this; }
138132

139-
MString& setn(char *s, int len) { int c = _MIN(len, SIZE); strncpy(str, s, c); str[c] = '\0'; debug(F("string")); return *this; }
133+
MString& setn(char *s, int len) { int c = _MIN(len, SIZE); strlcpy(str, s, c + 1); debug(F("string")); return *this; }
140134
MString& setn(const char *s, int len) { return setn(const_cast<char*>(s), len); }
141-
MString& setn_P(PGM_P const s, int len) { int c = _MIN(len, SIZE); strncpy_P(str, s, c); str[c] = '\0'; debug(F("pstring")); return *this; }
135+
MString& setn_P(PGM_P const s, int len) { int c = _MIN(len, SIZE); strlcpy_P(str, s, c + 1); debug(F("pstring")); return *this; }
142136
MString& setn(FSTR_P const f, int len) { return setn_P(FTOP(f), len); }
143137

144138
// set(repchr_t('-', 10))
@@ -159,9 +153,9 @@ class MString {
159153

160154
// Chainable String appenders
161155
MString& append() { debug(F("nil")); return *this; } // for macros that might emit no output
162-
MString& append(char *s) { int sz = length(); if (sz < SIZE) strncpy(str + sz, s, SIZE - sz); debug(F("string")); return *this; }
156+
MString& append(char *s) { int sz = length(); if (sz < SIZE) strlcpy(str + sz, s, SIZE - sz + 1); debug(F("string")); return *this; }
163157
MString& append(const char *s) { return append(const_cast<char *>(s)); }
164-
MString& append_P(PGM_P const s) { int sz = length(); if (sz < SIZE) strncpy_P(str + sz, s, SIZE - sz); debug(F("pstring")); return *this; }
158+
MString& append_P(PGM_P const s) { int sz = length(); if (sz < SIZE) strlcpy_P(str + sz, s, SIZE - sz + 1); debug(F("pstring")); return *this; }
165159
MString& append(FSTR_P const f) { return append_P(FTOP(f)); }
166160
MString& append(const bool &b) { return append(b ? F("true") : F("false")); }
167161
MString& append(const char c) { int sz = length(); if (sz < SIZE) { str[sz] = c; if (sz < SIZE - 1) str[sz + 1] = '\0'; } return *this; }
@@ -195,15 +189,15 @@ class MString {
195189
MString& append(const MString<S> &m) { return append(&m); }
196190

197191
// Append only if the given space is available
198-
MString& appendn(char *s, int len) { int sz = length(), c = _MIN(len, SIZE - sz); if (c > 0) { strncpy(str + sz, s, c); str[sz + c] = '\0'; } debug(F("string")); return *this; }
192+
MString& appendn(char *s, int len) { int sz = length(), c = _MIN(len, SIZE - sz); if (c > 0) { strlcpy(str + sz, s, c + 1); } debug(F("string")); return *this; }
199193
MString& appendn(const char *s, int len) { return appendn(const_cast<char *>(s), len); }
200-
MString& appendn_P(PGM_P const s, int len) { int sz = length(), c = _MIN(len, SIZE - sz); if (c > 0) { strncpy_P(str + sz, s, c); str[sz + c] = '\0'; } debug(F("pstring")); return *this; }
194+
MString& appendn_P(PGM_P const s, int len) { int sz = length(), c = _MIN(len, SIZE - sz); if (c > 0) { strlcpy_P(str + sz, s, c + 1); } debug(F("pstring")); return *this; }
201195
MString& appendn(FSTR_P const f, int len) { return appendn_P(FTOP(f), len); }
202196

203197
// append(repchr_t('-', 10))
204198
MString& append(const repchr_t &s) {
205199
const int sz = length(), c = _MIN(s.count, SIZE - sz);
206-
if (c > 0) { memset(str + sz, s.asc, c); safety(sz + c); }
200+
if (c > 0) { memset(str + sz, s.asc, c); str[sz + c] = '\0'; }
207201
debug(F("repchr"));
208202
return *this;
209203
}
@@ -299,7 +293,7 @@ class MString {
299293
}
300294

301295
void copyto(char * const dst) const { strcpy(dst, str); }
302-
void copyto(char * const dst, int len) const { strncpy(dst, str, len); }
296+
void copyto(char * const dst, int len) const { strlcpy(dst, str, len + 1); }
303297

304298
MString& clear() { return set(); }
305299
MString& eol() { return append('\n'); }
@@ -318,6 +312,7 @@ class MString {
318312

319313
#pragma GCC diagnostic pop
320314

315+
// Temporary inline string typically used to compose a G-code command
321316
#ifndef TS_SIZE
322317
#define TS_SIZE 63
323318
#endif

Marlin/src/gcode/queue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class GCodeQueue {
134134
* Aborts the current SRAM queue so only use for one or two commands.
135135
*/
136136
static void inject(const char * const gcode) {
137-
strncpy(injected_commands, gcode, sizeof(injected_commands) - 1);
137+
strlcpy(injected_commands, gcode, sizeof(injected_commands));
138138
}
139139

140140
/**

Marlin/src/lcd/extui/anycubic_chiron/chiron_tft.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ void ChironTFT::sendFileList(int8_t startindex) {
433433
}
434434

435435
void ChironTFT::selectFile() {
436-
const size_t namelen = command_len - 4 + (panel_type <= AC_panel_new);
437-
strncpy(selectedfile, panel_command + 4, namelen);
438-
selectedfile[namelen] = '\0';
436+
const size_t fnlen = command_len - 4 + (panel_type <= AC_panel_new);
437+
strlcpy(selectedfile, panel_command + 4, fnlen + 1);
439438
#if ACDEBUG(AC_FILE)
440439
DEBUG_ECHOLNPGM(" Selected File: ", selectedfile);
441440
#endif
441+
442442
switch (selectedfile[0]) {
443443
case '/': // Valid file selected
444444
tftSendLn(AC_msg_sd_file_open_success);
@@ -449,10 +449,9 @@ void ChironTFT::selectFile() {
449449
tftSendLn(AC_msg_sd_file_open_failed);
450450
sendFileList( 0 );
451451
break;
452-
default: // enter sub folder
453-
// for new panel remove the '.GCO' tag that was added to the end of the path
454-
if (panel_type <= AC_panel_new)
455-
selectedfile[strlen(selectedfile) - 4] = '\0';
452+
default: // enter subfolder
453+
// For new panel remove the '.GCO' tag that was added to the end of the path
454+
if (panel_type <= AC_panel_new) selectedfile[fnlen - 4] = '\0';
456455
filenavigator.changeDIR(selectedfile);
457456
tftSendLn(AC_msg_sd_file_open_failed);
458457
sendFileList( 0 );

Marlin/src/lcd/extui/anycubic_vyper/dgus_tft.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,7 @@ namespace Anycubic {
971971
}
972972

973973
void DgusTFT::selectFile() {
974-
strncpy(selectedfile, panel_command + 4, command_len - 4);
975-
selectedfile[command_len - 5] = '\0';
974+
strlcpy(selectedfile, panel_command + 4, command_len - 3);
976975
#if ACDEBUG(AC_FILE)
977976
DEBUG_ECHOLNPGM(" Selected File: ", selectedfile);
978977
#endif
@@ -1293,8 +1292,7 @@ namespace Anycubic {
12931292
TERN_(CASE_LIGHT_ENABLE, setCaseLightState(true));
12941293

12951294
char str_buf[20];
1296-
strncpy_P(str_buf, filenavigator.filelist.longFilename(), 17);
1297-
str_buf[17] = '\0';
1295+
strlcpy_P(str_buf, filenavigator.filelist.longFilename(), 18);
12981296
sendTxtToTFT(str_buf, TXT_PRINT_NAME);
12991297

13001298
#if ENABLED(POWER_LOSS_RECOVERY)
@@ -1332,8 +1330,7 @@ namespace Anycubic {
13321330
printFile(filenavigator.filelist.shortFilename());
13331331

13341332
char str_buf[20];
1335-
strncpy_P(str_buf, filenavigator.filelist.longFilename(), 17);
1336-
str_buf[17] = '\0';
1333+
strlcpy_P(str_buf, filenavigator.filelist.longFilename(), 18);
13371334
sendTxtToTFT(str_buf, TXT_PRINT_NAME);
13381335

13391336
sprintf(str_buf, "%5.2f", getFeedrate_percent());

Marlin/src/lcd/extui/mks_ui/draw_keyboard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
155155
#endif // MKS_WIFI_MODULE
156156
case autoLevelGcodeCommand:
157157
uint8_t buf[100];
158-
strncpy((char *)buf, ret_ta_txt, sizeof(buf));
158+
strlcpy((char *)buf, ret_ta_txt, sizeof(buf));
159159
update_gcode_command(AUTO_LEVELING_COMMAND_ADDR, buf);
160160
goto_previous_ui();
161161
break;

Marlin/src/lcd/extui/mks_ui/draw_print_file.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ void cutFileName(char *path, int len, int bytePerLine, char *outStr) {
474474
wcscpy(outStr, beginIndex);
475475
#else
476476
if ((int)strlen(beginIndex) > len)
477-
strncpy(outStr, beginIndex, len);
477+
strlcpy(outStr, beginIndex, len + 1);
478478
else
479479
strcpy(outStr, beginIndex);
480480
#endif
@@ -485,17 +485,17 @@ void cutFileName(char *path, int len, int bytePerLine, char *outStr) {
485485
wcsncpy(outStr, (const WCHAR *)beginIndex, len - 3);
486486
wcscat(outStr, (const WCHAR *)gFileTail);
487487
#else
488-
//strncpy(outStr, beginIndex, len - 3);
489-
strncpy(outStr, beginIndex, len - 4);
488+
strlcpy(outStr, beginIndex, len - 3);
490489
strcat_P(outStr, PSTR("~.g"));
491490
#endif
492491
}
493492
else {
493+
const size_t strsize = strIndex2 - beginIndex + 1;
494494
#if _LFN_UNICODE
495-
wcsncpy(outStr, (const WCHAR *)beginIndex, strIndex2 - beginIndex + 1);
495+
wcsncpy(outStr, (const WCHAR *)beginIndex, strsize);
496496
wcscat(outStr, (const WCHAR *)&gFileTail[3]);
497497
#else
498-
strncpy(outStr, beginIndex, strIndex2 - beginIndex + 1);
498+
strlcpy(outStr, beginIndex, strsize + 1);
499499
strcat_P(outStr, PSTR("g"));
500500
#endif
501501
}

Marlin/src/lcd/extui/mks_ui/mks_hardware.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,7 @@ void disp_assets_update_progress(FSTR_P const fmsg) {
727727
static constexpr int buflen = 30;
728728
char buf[buflen];
729729
memset(buf, ' ', buflen);
730-
strncpy_P(buf, FTOP(fmsg), buflen - 1);
731-
buf[buflen - 1] = '\0';
730+
strlcpy_P(buf, FTOP(fmsg), buflen);
732731
disp_string(100, 165, buf, 0xFFFF, 0x0000);
733732
#else
734733
disp_string(100, 165, FTOP(fmsg), 0xFFFF, 0x0000);

Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ void tft_lvgl_init() {
237237
uiCfg.print_state = REPRINTING;
238238

239239
#if ENABLED(LONG_FILENAME_HOST_SUPPORT)
240-
strncpy(public_buf_m, recovery.info.sd_filename, sizeof(public_buf_m));
240+
strlcpy(public_buf_m, recovery.info.sd_filename, sizeof(public_buf_m));
241241
card.printLongPath(public_buf_m);
242-
strncpy(list_file.long_name[sel_id], card.longFilename, sizeof(list_file.long_name[0]));
242+
strlcpy(list_file.long_name[sel_id], card.longFilename, sizeof(list_file.long_name[0]));
243243
#else
244-
strncpy(list_file.long_name[sel_id], recovery.info.sd_filename, sizeof(list_file.long_name[0]));
244+
strlcpy(list_file.long_name[sel_id], recovery.info.sd_filename, sizeof(list_file.long_name[0]));
245245
#endif
246246
lv_draw_printing();
247247
}

Marlin/src/lcd/extui/nextion/nextion_tft.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ void NextionTFT::sendFileList(int8_t startindex) {
158158
}
159159

160160
void NextionTFT::selectFile() {
161-
strncpy(selectedfile, nextion_command + 4, command_len - 4);
162-
selectedfile[command_len - 5] = '\0';
161+
strlcpy(selectedfile, nextion_command + 4, command_len - 3);
163162
#if NEXDEBUG(N_FILE)
164163
DEBUG_ECHOLNPGM(" Selected File: ", selectedfile);
165164
#endif

0 commit comments

Comments
 (0)