foc_sensor_mode from COMM_SET_MCCONF isn't range-checked, and the FOC ISR switch has no default

Open
#995 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c
Domain
embedded-iot

Research direction

Read confgenerator.c around the deserialization at line 425, commands_apply_mcconf_hw_limits, and the mcpwm_foc_adc_int_handler switch in motor/mcpwm_foc.c around lines 3473–3618. Trace when the configuration is applied and how invalid sensor modes affect phase updates; then decide where validation belongs and how to handle an invalid value. Done means an out-of-range mode cannot leave rotor angle updates stuck, with coverage for that case.

Written by the indexing model from the issue text.

Description

Hi, I think an out-of-range foc_sensor_mode in a configuration packet makes the FOC interrupt stop updating the rotor angle.

The bug

The deserializer stores the byte as is (confgenerator.c L425):

conf->foc_sensor_mode = buffer[ind++];

The enum has 10 values (FOC_SENSOR_MODE_SENSORLESS … FOC_SENSOR_MODE_ENCODER_AB). The switch in mcpwm_foc_adc_int_handler (mcpwm_foc.c L3473-L3601) has a case for each of them and no default. For a value of 10 or more no case runs, and outside handbrake, open-loop and phase override nothing else assigns state_now->phase (L3603-L3618), so current control keeps driving the last angle.

I couldn't find a range check on the way in: not in confgenerator_deserialize_mcconf and not in commands_apply_mcconf_hw_limits. The switch at L3724 reads the same field.

What it costs

VESC Tool sends valid values, so this needs a buggy tool or a hand-made packet. If it happens, the motor is driven at a frozen angle instead of faulting. This is similar to #930. I found it by reading the code and have not reproduced it on hardware.

Fix

Validate the field when a configuration is applied, for example in commands_apply_mcconf_hw_limits:

if (mcconf->foc_sensor_mode > FOC_SENSOR_MODE_ENCODER_AB) {
    mcconf->foc_sensor_mode = FOC_SENSOR_MODE_SENSORLESS;
}

or give the ISR switch a default that raises a fault.

Dominant language
C
Stars
3.6k
Forks
2k
Avg merge
20h 29m
Merged PRs (30d)
12

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vedderb/bldc

All issues in vedderb/bldc

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.