Skip to content

Commit bb557e5

Browse files
🩹 Fix string buffer warning (MarlinFirmware#26550)
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
1 parent 89fdfcf commit bb557e5

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

Marlin/src/core/mstring.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class MString {
136136
MString& setn(FSTR_P const f, int len) { return setn_P(FTOP(f), len); }
137137

138138
// set(repchr_t('-', 10))
139-
MString& set(const repchr_t &s) { int c = _MIN(s.count, SIZE); memset(str, s.asc, c); str[c] = '\0'; debug(F("")); return *this; }
139+
MString& set(const repchr_t &s) { int c = _MIN(s.count, SIZE); if (c >= 0) { if (c > 0) memset(str, s.asc, c); str[c] = '\0'; } debug(F("repchr_t")); return *this; }
140140

141141
// set(spaces_t(10))
142142
MString& set(const spaces_t &s) { repchr_t r(' ', s.count); return set(r); }

Marlin/src/core/types.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,10 @@ typedef struct WFloat { float value; char width; char prec;
309309
typedef struct PFloat { float value; char prec;
310310
PFloat(float v, char p) : value(v), prec(p) {}
311311
} p_float_t;
312-
typedef struct RepChr { char asc; uint8_t count;
312+
typedef struct RepChr { char asc; int8_t count;
313313
RepChr(char a, uint8_t c) : asc(a), count(c) {}
314314
} repchr_t;
315-
typedef struct Spaces { uint8_t count;
315+
typedef struct Spaces { int8_t count;
316316
Spaces(uint8_t c) : count(c) {}
317317
} spaces_t;
318318

Marlin/src/lcd/dogm/status_screen_DOGM.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -479,16 +479,16 @@ FORCE_INLINE void _draw_axis_value(const AxisEnum axis, const char *value, const
479479
// Prepare strings for progress display
480480
#if ANY(HAS_EXTRA_PROGRESS, HAS_PRINT_PROGRESS)
481481
static MarlinUI::progress_t progress = 0;
482-
static MString<12> progressString;
482+
static MString<13> progressString;
483483
#endif
484484

485485
#if HAS_EXTRA_PROGRESS
486486

487487
#if HAS_TIME_DISPLAY
488488
static void prepare_time_string(const duration_t &time, char prefix) {
489-
char str[10];
489+
char str[13];
490490
const uint8_t time_len = time.toDigital(str, time.value >= 60*60*24L); // 5 to 8 chars
491-
progressString.set(prefix, ':', spaces_t(10 - time_len), str); // 2 to 5 spaces
491+
progressString.set(prefix, ':', spaces_t(10 - time_len), str); // 2 to 5 spaces
492492
}
493493
#endif
494494
#if ENABLED(SHOW_PROGRESS_PERCENT)

Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp

+14-17
Original file line numberDiff line numberDiff line change
@@ -662,39 +662,36 @@ bool ST7920_Lite_Status_Screen::indicators_changed() {
662662

663663
// Process progress strings
664664
#if HAS_PRINT_PROGRESS
665-
static char screenstr[8];
665+
static MString<8> screenstr;
666666

667667
#if HAS_TIME_DISPLAY
668668
char * ST7920_Lite_Status_Screen::prepare_time_string(const duration_t &time, char prefix) {
669-
static char str[6];
670-
memset(&screenstr, ' ', 8); // fill with spaces to avoid artifacts, not doing right-justification to save cycles
671-
screenstr[0] = prefix;
672-
TERN_(HOTENDS == 1, screenstr[1] = 0x07;) // add bullet • separator when there is space
673-
int str_length = time.toDigital(str);
674-
memcpy(&screenstr[TERN(HOTENDS == 1, 2, 1)], str, str_length); //memcpy because we can't have terminator
675-
return screenstr;
669+
static char time_str[6];
670+
(void)time.toDigital(time_str); // Up to 5 chars
671+
screenstr = prefix;
672+
if (HOTENDS == 1) screenstr += char(0x07); // Add bullet • separator when there is space
673+
screenstr += time_str;
674+
screenstr += Spaces(3);
675+
return &screenstr;
676676
}
677677
#endif
678678

679679
void ST7920_Lite_Status_Screen::draw_progress_string(uint8_t addr, const char *str) {
680680
set_ddram_address(addr);
681681
begin_data();
682-
write_str(str, TERN(HOTENDS == 1, 8, 6));
682+
write_str(str, HOTENDS == 1 ? 8 : 6);
683683
}
684684

685-
#define PPOS (DDRAM_LINE_3 + TERN(HOTENDS == 1, 4, 5)) // progress string position, in 16-bit words
685+
constexpr uint8_t PPOS = (DDRAM_LINE_3 + (HOTENDS == 1 ? 4 : 5)); // Progress string position, in 16-bit words
686686

687687
#if ENABLED(SHOW_PROGRESS_PERCENT)
688688
void MarlinUI::drawPercent() { lightUI.drawPercent(); }
689689
void ST7920_Lite_Status_Screen::drawPercent() {
690-
#define LSHIFT TERN(HOTENDS == 1, 0, 1)
691690
const uint8_t progress = ui.get_progress_percent();
692-
memset(&screenstr, ' ', 8); // fill with spaces to avoid artifacts
693-
if (progress){
694-
memcpy(&screenstr[2 - LSHIFT], \
695-
TERN(PRINT_PROGRESS_SHOW_DECIMALS, permyriadtostr4(ui.get_progress_permyriad()), ui8tostr3rj(progress)), \
696-
TERN(PRINT_PROGRESS_SHOW_DECIMALS, 4, 3));
697-
screenstr[(TERN(PRINT_PROGRESS_SHOW_DECIMALS, 6, 5) - LSHIFT)] = '%';
691+
if (progress) {
692+
screenstr += Spaces(1 + (HOTENDS == 1));
693+
screenstr += TERN(PRINT_PROGRESS_SHOW_DECIMALS, permyriadtostr4(ui.get_progress_permyriad()), ui8tostr3rj(progress));
694+
screenstr += "% ";
698695
draw_progress_string(PPOS, screenstr);
699696
}
700697
}

Marlin/src/lcd/marlinui.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -1035,9 +1035,13 @@ void MarlinUI::init() {
10351035
uint8_t abs_diff = ABS(encoderDiff);
10361036

10371037
#if ENCODER_PULSES_PER_STEP > 1
1038-
static int8_t lastEncoderDiff;
1039-
TERN_(HAS_TOUCH_SLEEP, if (lastEncoderDiff != encoderDiff) wakeup_screen());
1040-
lastEncoderDiff = encoderDiff;
1038+
#if HAS_TOUCH_SLEEP
1039+
static int8_t lastEncoderDiff;
1040+
if (lastEncoderDiff != encoderDiff) {
1041+
wakeup_screen();
1042+
lastEncoderDiff = encoderDiff;
1043+
}
1044+
#endif
10411045
#endif
10421046

10431047
const bool encoderPastThreshold = (abs_diff >= epps);

0 commit comments

Comments
 (0)