Skip to content

Commit a9f2ebf

Browse files
committed
Fix snprintf buffer size for digit buffers
Replaces hardcoded buffer size '2' in snprintf calls with sizeof the respective digit buffer. This prevents potential buffer overflows and ensures correct string formatting for power and altitude display labels.
1 parent f9df4bd commit a9f2ebf

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

.claude/settings.local.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"permissions": {
33
"allow": [
44
"Bash(pio)",
5-
"Bash(pio run)"
5+
"Bash(pio run)",
6+
"Bash(cpplint:*)"
67
],
78
"deny": []
89
}

src/sp140/lvgl/lvgl_updates.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -450,19 +450,19 @@ void updateLvglMainScreen(
450450

451451
// Tens digit (only show if >= 10kW)
452452
if (tens > 0) {
453-
snprintf(power_digit_buffers[0], 2, "%d", tens);
453+
snprintf(power_digit_buffers[0], sizeof(power_digit_buffers[0]), "%d", tens);
454454
lv_label_set_text(power_char_labels[0], power_digit_buffers[0]);
455455
}
456456

457457
// Ones digit (always show)
458-
snprintf(power_digit_buffers[1], 2, "%d", ones);
458+
snprintf(power_digit_buffers[1], sizeof(power_digit_buffers[1]), "%d", ones);
459459
lv_label_set_text(power_char_labels[1], power_digit_buffers[1]);
460460

461461
// Decimal point
462462
lv_label_set_text(power_char_labels[2], ".");
463463

464464
// Tenths digit
465-
snprintf(power_digit_buffers[3], 2, "%d", decimal_part);
465+
snprintf(power_digit_buffers[3], sizeof(power_digit_buffers[3]), "%d", decimal_part);
466466
lv_label_set_text(power_char_labels[3], power_digit_buffers[3]);
467467

468468
// Unit label is static and already set to "kW"
@@ -545,31 +545,31 @@ void updateLvglMainScreen(
545545

546546
// Thousands digit
547547
if (thousands > 0) {
548-
snprintf(digit_buffers[0], 2, "%d", thousands);
548+
snprintf(digit_buffers[0], sizeof(digit_buffers[0]), "%d", thousands);
549549
lv_label_set_text(altitude_char_labels[0], digit_buffers[0]);
550550
}
551551

552552
// Hundreds digit
553553
if (thousands > 0 || hundreds > 0) {
554-
snprintf(digit_buffers[1], 2, "%d", hundreds);
554+
snprintf(digit_buffers[1], sizeof(digit_buffers[1]), "%d", hundreds);
555555
lv_label_set_text(altitude_char_labels[1], digit_buffers[1]);
556556
}
557557

558558
// Tens digit
559559
if (whole_part >= 10) {
560-
snprintf(digit_buffers[2], 2, "%d", tens);
560+
snprintf(digit_buffers[2], sizeof(digit_buffers[2]), "%d", tens);
561561
lv_label_set_text(altitude_char_labels[2], digit_buffers[2]);
562562
}
563563

564564
// Ones digit (always show)
565-
snprintf(digit_buffers[3], 2, "%d", ones);
565+
snprintf(digit_buffers[3], sizeof(digit_buffers[3]), "%d", ones);
566566
lv_label_set_text(altitude_char_labels[3], digit_buffers[3]);
567567

568568
// Decimal point
569569
lv_label_set_text(altitude_char_labels[4], ".");
570570

571571
// Tenths digit
572-
snprintf(digit_buffers[5], 2, "%d", decimal_part);
572+
snprintf(digit_buffers[5], sizeof(digit_buffers[5]), "%d", decimal_part);
573573
lv_label_set_text(altitude_char_labels[5], digit_buffers[5]);
574574

575575
// Adjust width of position 4 back to narrow for meters (decimal point)
@@ -614,30 +614,30 @@ void updateLvglMainScreen(
614614

615615
// Ten-thousands digit in position 0
616616
if (ten_thousands > 0) {
617-
snprintf(digit_buffers_ft[0], 2, "%d", ten_thousands);
617+
snprintf(digit_buffers_ft[0], sizeof(digit_buffers_ft[0]), "%d", ten_thousands);
618618
lv_label_set_text(altitude_char_labels[0], digit_buffers_ft[0]);
619619
}
620620

621621
// Thousands digit in position 1
622622
if (ten_thousands > 0 || thousands > 0) {
623-
snprintf(digit_buffers_ft[1], 2, "%d", thousands);
623+
snprintf(digit_buffers_ft[1], sizeof(digit_buffers_ft[1]), "%d", thousands);
624624
lv_label_set_text(altitude_char_labels[1], digit_buffers_ft[1]);
625625
}
626626

627627
// Hundreds digit in position 2
628628
if (ten_thousands > 0 || thousands > 0 || hundreds > 0) {
629-
snprintf(digit_buffers_ft[2], 2, "%d", hundreds);
629+
snprintf(digit_buffers_ft[2], sizeof(digit_buffers_ft[2]), "%d", hundreds);
630630
lv_label_set_text(altitude_char_labels[2], digit_buffers_ft[2]);
631631
}
632632

633633
// Tens digit in position 3
634634
if (feet >= 10) {
635-
snprintf(digit_buffers_ft[3], 2, "%d", tens);
635+
snprintf(digit_buffers_ft[3], sizeof(digit_buffers_ft[3]), "%d", tens);
636636
lv_label_set_text(altitude_char_labels[3], digit_buffers_ft[3]);
637637
}
638638

639639
// Ones digit in position 4 (always show) - now has normal width
640-
snprintf(digit_buffers_ft[4], 2, "%d", ones);
640+
snprintf(digit_buffers_ft[4], sizeof(digit_buffers_ft[4]), "%d", ones);
641641
lv_label_set_text(altitude_char_labels[4], digit_buffers_ft[4]);
642642

643643
// Feet unit in position 6 (same as meters, with proper spacing)

src/sp140/system_monitors.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ void addInternalMonitors() {
2020

2121
void addAltimeterMonitors() {
2222
// Barometer Temperature monitor disabled for now.
23-
// TODO:
24-
// The BMP barometer shares the I2C bus with other peripherals; without a
23+
// TODO: The BMP barometer shares the I2C bus with other peripherals; without a
2524
// global I2C mutex the `Wire` driver can log "Unfinished Repeated Start"
2625
// errors when transactions from multiple tasks interleave. Until we
2726
// introduce a proper mutex (or move barometer access into a dedicated

src/sp140/vibration_pwm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "sp140/esp32s3-config.h"
33
#include "sp140/lvgl/lvgl_updates.h"
44

5-
const int VIBE_PWM_PIN = 46; // TODO: move to config
5+
// Pin is configured via s3_config.vibe_pwm
66
const int VIBE_PWM_FREQ = 1000; // Adjust as needed
77
const int VIBE_PWM_RESOLUTION = 8; // 8-bit resolution
88

0 commit comments

Comments
 (0)