Skip to content

Commit

Permalink
session: Handle invalid strings in keyboard prompt
Browse files Browse the repository at this point in the history
If we are given no prompts, there is no reason to continue. If we are
given no instruction or username string, I've opted to use an empty
string instead, though this can be argued to be incorrect.

Fixes #326
  • Loading branch information
craigdods authored and yodaldevoid committed Nov 5, 2024
1 parent fa33d2a commit 1b026dd
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,29 @@ impl Session {
// There's not much to be done with them though because the
// signature of the callback doesn't allow reporting an error.
let _ = catch_unwind(AssertUnwindSafe(|| {
if prompts.is_null() || responses.is_null() || num_prompts < 0 {
return;
}

let prompter = unsafe { &mut **(abstrakt as *mut *mut P) };

let username =
unsafe { slice::from_raw_parts(username as *const u8, username_len as usize) };
let username = String::from_utf8_lossy(username);
let username = if !username.is_null() && username_len >= 0 {
let username = unsafe {
slice::from_raw_parts(username as *const u8, username_len as usize)
};
String::from_utf8_lossy(username)
} else {
Cow::Borrowed("")
};

let instruction = unsafe {
slice::from_raw_parts(instruction as *const u8, instruction_len as usize)
let instruction = if !instruction.is_null() && instruction_len >= 0 {
let instruction = unsafe {
slice::from_raw_parts(instruction as *const u8, instruction_len as usize)
};
String::from_utf8_lossy(instruction)
} else {
Cow::Borrowed("")
};
let instruction = String::from_utf8_lossy(instruction);

let prompts = unsafe { slice::from_raw_parts(prompts, num_prompts as usize) };
let responses =
Expand Down

0 comments on commit 1b026dd

Please sign in to comment.