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

Conversation

magicrub
Copy link
Contributor

AP_BattMonitor: add null check to mppt power state

@magicrub magicrub added the BUG label May 13, 2025
Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

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

LGTM.

@magicrub does this need to be backported to 4.6? Do Bad Things happen on real vehicles?

@@ -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

@rmackay9 rmackay9 merged commit aed7d78 into ArduPilot:master May 14, 2025
102 of 103 checks passed
@magicrub magicrub deleted the pr/mppt_null_check branch May 14, 2025 16:21
@magicrub
Copy link
Contributor Author

@peterbarker @rmackay9 Yes, bad things happen (aka it reboots) so worthwhile to backport to 4.6. I'll make a PR for that. I never noticed this problem until recently because I just added a battery instance AFTER the mppts which are occasionally disabled on test aircraft.

Example:
Batt1=OK, Batt2/Mppt=Disabled: (total instance count = 1) this worked,
Batt1=OK, Batt2/Mppt=Disabled, Batt3=OK: (total instance count = 3) this was causing a crash accessing Batt2

Like what Randy says, we use _num_instance a dozen times so lets keep that scheme. ARRAY_SIZE is not accurate. Technically we would want to use MIN(_num_instances, ARRAY_SIZE(drivers)) to limit loop count AND stay within bounds but the null check is still always required

@rmackay9
Copy link
Contributor

rmackay9 commented May 15, 2025

Hi @magicrub,

great, thanks for the feedback and offer of a backport PR. We probably don't actually need that because it'll be an easy cherry-pick into 4.6. Either way is fine though

@rmackay9 rmackay9 moved this to Pending in 4.6 Backports May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Pending
Development

Successfully merging this pull request may close these issues.

3 participants