Skip to content
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

AP_Proximity: prevent buffer overflow in LD06 driver #27059

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
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
31 changes: 21 additions & 10 deletions libraries/AP_Proximity/AP_Proximity_LD06.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,33 @@ void AP_Proximity_LD06::get_readings()
// Loops through all bytes that were received
while (nbytes-- > 0) {

// Gets and logs the current byte being read
const uint8_t c = _uart->read();
uint8_t c;
if (!_uart->read(c)) {
break;
}

// Stores the byte in an array if the byte is a start byte or we have already read a start byte
if (c == LD_START_CHAR || _response_data) {

// Sets to true if a start byte has been read, default false otherwise
_response_data = true;

if (c == LD_START_CHAR || _byte_count) {
// Stores the next byte in an array
_response[_byte_count] = c;
if (_byte_count < START_DATA_LENGTH) {
_byte_count++;
continue;
}

const uint32_t total_packet_length = _response[START_DATA_LENGTH] + 3;
if (total_packet_length > ARRAY_SIZE(_response)) {
// invalid packet received; throw away all data and
// start again.
_byte_count = 0;
_uart->discard_input();
break;
}

_byte_count++;

if (_byte_count == _response[START_DATA_LENGTH] + 3) {
if (_byte_count == total_packet_length) {

const uint32_t current_ms = AP_HAL::millis();

// Stores the last distance taken, used to reduce number of readings taken
Expand All @@ -121,7 +133,6 @@ void AP_Proximity_LD06::get_readings()

// Resets the bytes read and whether or not we are reading data to accept a new payload
_byte_count = 0;
_response_data = false;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion libraries/AP_Proximity/AP_Proximity_LD06.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class AP_Proximity_LD06 : public AP_Proximity_Backend_Serial

// Store and keep track of the bytes being read from the sensor
uint8_t _response[MESSAGE_LENGTH_LD06];
bool _response_data;
uint16_t _byte_count;

// Store for error-tracking purposes
Expand Down
Loading