Skip to content

Commit

Permalink
UDS: add length check for negative response
Browse files Browse the repository at this point in the history
  • Loading branch information
pd0wm committed Feb 11, 2025
1 parent 1d75476 commit 356cbc8
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/uds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ impl<'a> UDSClient<'a> {
loop {
let response = stream.next().await.unwrap()?;

// Ensure we got a reply at all
if response.is_empty() {
return Err(Error::InvalidResponseLength.into());
}

// Check for errors
let response_sid = response[0];
if response_sid == NEGATIVE_RESPONSE {
let code: NegativeResponseCode = response[2].into();
// Negative response consists of [0x7F, SID, Error]
// According to ISO 14229-1, there can be no extra data after the response code but we will accept this
if response.len() <= 2 {
return Err(Error::InvalidResponseLength.into());
}

let code: NegativeResponseCode = response[2].into();
if code == NegativeResponseCode::RequestCorrectlyReceivedResponsePending {
info!("Received Response Pending");
continue;
Expand All @@ -79,6 +89,11 @@ impl<'a> UDSClient<'a> {

// Check sub function
if let Some(sub_function) = sub_function {
// Ensure we also have a subfunction in the response
if response.len() <= 1 {
return Err(Error::InvalidResponseLength.into());
}

if response[1] != sub_function {
return Err(Error::InvalidSubFunction(response[1]).into());
}
Expand Down

0 comments on commit 356cbc8

Please sign in to comment.