Skip to content

AP_BattMonitor: add null check to mppt power state #30067

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

Merged
merged 1 commit into from
May 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/AP_BattMonitor/AP_BattMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ void AP_BattMonitor::MPPT_set_powered_state_to_all(const bool power_on)
// it will supply energy if available.
void AP_BattMonitor::MPPT_set_powered_state(const uint8_t instance, const bool power_on)
{
if (instance < _num_instances) {
if (instance < _num_instances && drivers[instance] != nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (instance < _num_instances && drivers[instance] != nullptr) {
if (instance < ARRAY_SIZE(drivers) && drivers[instance] != nullptr) {

... makes it clear that the following deref is valid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should replace _num_instances with ARRAY_SIZE(drivers) because this will make the single call different from the 10+ other similar checks we do in AP_BattMonitor. Either way may be fine but I think we should be consistent to avoid the possibility of odd bugs creeping in.

BTW the "voltage(uint8_t instance) const" getter is misisng the nullptr check as well it seems

drivers[instance]->mppt_set_powered_state(power_on);
}
}
Expand Down
Loading