- 
                Notifications
    You must be signed in to change notification settings 
- Fork 8.2k
Drivers: MCUX ADC16: Correct buffer size calculation for multi-channel read sequences #98611
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
base: main
Are you sure you want to change the base?
Drivers: MCUX ADC16: Correct buffer size calculation for multi-channel read sequences #98611
Conversation
| Hello @kaabia, and thank you very much for your first pull request to the Zephyr project! | 
429ad65    to
    50a10ba      
    Compare
  
    The mcux_adc16 driver's read function fails to validate the user-provided buffer size when `adc_sequence.options` are used (extra samplings). The calculation erroneously considered only the size required for one channel, neglecting the total number of channels in the sequence. This leads to a **buffer overflow** when reading multiple channels. Fix this by using `POPCOUNT(sequence->channels)` to determine the correct channel count and ensure the buffer size is sufficient. Signed-off-by: Your Name <[email protected]>
50a10ba    to
    9b187ab      
    Compare
  
    | 
 | 
| To get bug fixes merged for 4.3 release, the PR needs to be linked to a bug report issue | 
| return -EINVAL; | ||
| } | ||
|  | ||
| size_t min_buffer_size = channels_count * sizeof(uint16_t); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all declarations/definitions shall be at the function's start.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a written rule somewhere? never heard of it



drivers: adc: mcux_adc16: Fix buffer size validation for multi-channel sequences
The mcux_adc16 driver's read function fails to correctly validate the user-provided buffer size when
adc_sequence.optionsare used (i.e., for extra samplings or continuous conversions).The validation calculation erroneously considered only the size required for a single channel multiplied by the total number of samplings:
sizeof(uint16_t) * (extra_samplings + 1).This neglects the total number of channels enabled in
adc_sequence.channels, leading to a buffer overflow if the sequence includes multiple channels.Fix this by using
POPCOUNT(sequence->channels)to determine the correct number of channels and ensuring the buffer size is sufficient for all requested samples.