Fix null pointer dereferences in SettingsScreen index helpers

lowBatIndex(), autoOffIndex(), gpsIntervalIndex() each called
getNodePrefs() independently without null check, while renderItem()
had its own guarded copy. Added null checks consistent with the rest
of the class. Also initialize _node_prefs to NULL in UITask constructor.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jakub
2026-05-10 20:26:05 +02:00
parent 35b2b008c8
commit 4e83e99316
2 changed files with 12 additions and 9 deletions

View File

@@ -116,10 +116,11 @@ class SettingsScreen : public UIScreen {
static const int BATT_DISPLAY_COUNT = 3; static const int BATT_DISPLAY_COUNT = 3;
int lowBatIndex() { int lowBatIndex() {
uint16_t v = _task->getNodePrefs()->low_batt_mv; NodePrefs* p = _task->getNodePrefs();
if (!p) return 0;
for (int i = 0; i < LOW_BAT_COUNT; i++) for (int i = 0; i < LOW_BAT_COUNT; i++)
if (LOW_BAT_OPTS[i] == v) return i; if (LOW_BAT_OPTS[i] == p->low_batt_mv) return i;
return 0; // default: off return 0;
} }
void renderBar(DisplayDriver& display, int x, int y, int value, int max_val) { void renderBar(DisplayDriver& display, int x, int y, int value, int max_val) {
@@ -132,17 +133,19 @@ class SettingsScreen : public UIScreen {
} }
int autoOffIndex() { int autoOffIndex() {
uint16_t v = _task->getNodePrefs()->auto_off_secs; NodePrefs* p = _task->getNodePrefs();
if (!p) return 1;
for (int i = 0; i < AUTO_OFF_COUNT; i++) for (int i = 0; i < AUTO_OFF_COUNT; i++)
if (AUTO_OFF_OPTS[i] == v) return i; if (AUTO_OFF_OPTS[i] == p->auto_off_secs) return i;
return 1; // default: 15s return 1;
} }
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
int gpsIntervalIndex() { int gpsIntervalIndex() {
uint32_t v = _task->getNodePrefs()->gps_interval; NodePrefs* p = _task->getNodePrefs();
if (!p) return 0;
for (int i = 0; i < GPS_INTERVAL_COUNT; i++) for (int i = 0; i < GPS_INTERVAL_COUNT; i++)
if (GPS_INTERVAL_OPTS[i] == v) return i; if (GPS_INTERVAL_OPTS[i] == p->gps_interval) return i;
return 0; return 0;
} }
#endif #endif

View File

@@ -68,7 +68,7 @@ class UITask : public AbstractUITask {
public: public:
UITask(mesh::MainBoard* board, BaseSerialInterface* serial) : AbstractUITask(board, serial), _display(NULL), _sensors(NULL) { UITask(mesh::MainBoard* board, BaseSerialInterface* serial) : AbstractUITask(board, serial), _display(NULL), _sensors(NULL), _node_prefs(NULL) {
next_batt_chck = _next_refresh = 0; next_batt_chck = _next_refresh = 0;
ui_started_at = 0; ui_started_at = 0;
_batt_mv = 0; _batt_mv = 0;