Skip to content

Commit

Permalink
fix: potential in panic message_vector_get_at (#6233)
Browse files Browse the repository at this point in the history
Description
---
fixes potential in panic message_vector_get_at

Motivation and Context
---
creating the message vector and asking for index[0] will result in a
panic
  • Loading branch information
SWvheerden authored Mar 26, 2024
1 parent 6dbfd79 commit 2867454
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
7 changes: 4 additions & 3 deletions base_layer/chat_ffi/src/conversationalists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,15 @@ pub unsafe extern "C" fn conversationalists_vector_get_at(

let conversationalists = &(*conversationalists);

let len = conversationalists.0.len() - 1;
if position as usize > len {
let len = conversationalists.0.len();
let position = position as usize;
if conversationalists.0.is_empty() || position > len - 1 {
error = LibChatError::from(InterfaceError::PositionInvalidError).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
}

Box::into_raw(Box::new(conversationalists.0[position as usize].clone()))
Box::into_raw(Box::new(conversationalists.0[position].clone()))
}

/// Frees memory for ConversationalistsVector
Expand Down
7 changes: 4 additions & 3 deletions base_layer/chat_ffi/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,16 @@ pub unsafe extern "C" fn chat_metadata_get_at(

let message = &(*message);

let len = message.metadata.len() - 1;
if position as usize > len {
let len = message.metadata.len();
let position = position as usize;
if message.metadata.is_empty() || position > len - 1 {
error = LibChatError::from(InterfaceError::PositionInvalidError).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
}

let message_metadata_vec = &(*(message).metadata);
let message_metadata = Box::new(message_metadata_vec[position as usize].clone());
let message_metadata = Box::new(message_metadata_vec[position].clone());

Box::into_raw(message_metadata)
}
Expand Down
7 changes: 4 additions & 3 deletions base_layer/chat_ffi/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,16 @@ pub unsafe extern "C" fn message_vector_get_at(
}

let messages = &(*messages);
let position = position as usize;
let len = messages.0.len();

let len = messages.0.len() - 1;
if position as usize > len {
if messages.0.is_empty() || position > len - 1 {
error = LibChatError::from(InterfaceError::PositionInvalidError).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
}

Box::into_raw(Box::new(messages.0[position as usize].clone()))
Box::into_raw(Box::new(messages.0[position].clone()))
}

/// Frees memory for MessagesVector
Expand Down
4 changes: 2 additions & 2 deletions base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2506,8 +2506,8 @@ pub unsafe extern "C" fn seed_words_get_at(
error = LibWalletError::from(InterfaceError::NullError("seed words".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
} else {
let len = (*seed_words).0.len() - 1; // clamp to length
if position > len as u32 {
let len = (*seed_words).0.len(); // clamp to length
if (*seed_words).0.is_empty() || position > (len - 1) as u32 {
error = LibWalletError::from(InterfaceError::PositionInvalidError).code;
ptr::swap(error_out, &mut error as *mut c_int);
} else if let Ok(v) = CString::new(
Expand Down

0 comments on commit 2867454

Please sign in to comment.