Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiler warning and potential string buffer overflow #26550

Merged
merged 10 commits into from
Dec 25, 2023
2 changes: 1 addition & 1 deletion Marlin/src/core/mstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class MString {
MString& setn(FSTR_P const f, int len) { return setn_P(FTOP(f), len); }

// set(repchr_t('-', 10))
MString& set(const repchr_t &s) { int c = _MIN(s.count, SIZE); memset(str, s.asc, c); str[c] = '\0'; debug(F("")); return *this; }
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; }

// set(spaces_t(10))
MString& set(const spaces_t &s) { repchr_t r(' ', s.count); return set(r); }
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ typedef struct WFloat { float value; char width; char prec;
typedef struct PFloat { float value; char prec;
PFloat(float v, char p) : value(v), prec(p) {}
} p_float_t;
typedef struct RepChr { char asc; uint8_t count;
typedef struct RepChr { char asc; int8_t count;
RepChr(char a, uint8_t c) : asc(a), count(c) {}
} repchr_t;
typedef struct Spaces { uint8_t count;
typedef struct Spaces { int8_t count;
Spaces(uint8_t c) : count(c) {}
} spaces_t;

Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/lcd/dogm/status_screen_DOGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,16 @@ FORCE_INLINE void _draw_axis_value(const AxisEnum axis, const char *value, const
// Prepare strings for progress display
#if ANY(HAS_EXTRA_PROGRESS, HAS_PRINT_PROGRESS)
static MarlinUI::progress_t progress = 0;
static MString<12> progressString;
static MString<13> progressString;
#endif

#if HAS_EXTRA_PROGRESS

#if HAS_TIME_DISPLAY
static void prepare_time_string(const duration_t &time, char prefix) {
char str[10];
char str[13];
const uint8_t time_len = time.toDigital(str, time.value >= 60*60*24L); // 5 to 8 chars
progressString.set(prefix, ':', spaces_t(10 - time_len), str); // 2 to 5 spaces
progressString.set(prefix, ':', spaces_t(10 - time_len), str); // 2 to 5 spaces
}
#endif
#if ENABLED(SHOW_PROGRESS_PERCENT)
Expand Down
31 changes: 14 additions & 17 deletions Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,39 +662,36 @@ bool ST7920_Lite_Status_Screen::indicators_changed() {

// Process progress strings
#if HAS_PRINT_PROGRESS
static char screenstr[8];
static MString<8> screenstr;

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

void ST7920_Lite_Status_Screen::draw_progress_string(uint8_t addr, const char *str) {
set_ddram_address(addr);
begin_data();
write_str(str, TERN(HOTENDS == 1, 8, 6));
write_str(str, HOTENDS == 1 ? 8 : 6);
}

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

#if ENABLED(SHOW_PROGRESS_PERCENT)
void MarlinUI::drawPercent() { lightUI.drawPercent(); }
void ST7920_Lite_Status_Screen::drawPercent() {
#define LSHIFT TERN(HOTENDS == 1, 0, 1)
const uint8_t progress = ui.get_progress_percent();
memset(&screenstr, ' ', 8); // fill with spaces to avoid artifacts
if (progress){
memcpy(&screenstr[2 - LSHIFT], \
TERN(PRINT_PROGRESS_SHOW_DECIMALS, permyriadtostr4(ui.get_progress_permyriad()), ui8tostr3rj(progress)), \
TERN(PRINT_PROGRESS_SHOW_DECIMALS, 4, 3));
screenstr[(TERN(PRINT_PROGRESS_SHOW_DECIMALS, 6, 5) - LSHIFT)] = '%';
if (progress) {
screenstr += Spaces(1 + (HOTENDS == 1));
screenstr += TERN(PRINT_PROGRESS_SHOW_DECIMALS, permyriadtostr4(ui.get_progress_permyriad()), ui8tostr3rj(progress));
screenstr += "% ";
draw_progress_string(PPOS, screenstr);
}
}
Expand Down
10 changes: 7 additions & 3 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,13 @@ void MarlinUI::init() {
uint8_t abs_diff = ABS(encoderDiff);

#if ENCODER_PULSES_PER_STEP > 1
static int8_t lastEncoderDiff;
TERN_(HAS_TOUCH_SLEEP, if (lastEncoderDiff != encoderDiff) wakeup_screen());
lastEncoderDiff = encoderDiff;
#if HAS_TOUCH_SLEEP
static int8_t lastEncoderDiff;
if (lastEncoderDiff != encoderDiff) {
wakeup_screen();
lastEncoderDiff = encoderDiff;
}
#endif
#endif
soligen2010 marked this conversation as resolved.
Show resolved Hide resolved

const bool encoderPastThreshold = (abs_diff >= epps);
Expand Down
Loading