Skip to content

Commit

Permalink
Merge pull request #216 from ThomasHabets/safe
Browse files Browse the repository at this point in the history
Fix gotchas that could cause crashes or worse
  • Loading branch information
jeaye committed Mar 30, 2024
2 parents 9a9d1d7 + f307e05 commit f7f2179
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn gen_constants() {
let bin = format!("{}", Path::new(&out_dir).join(if cfg!(windows) { "genconstants.exe" } else { "genconstants" }).display());
let src = format!("{}", Path::new(&out_dir).join("raw_constants.rs").display());

let mut build = cc::Build::new();
let build = cc::Build::new();
let compiler = build.try_get_compiler().expect("Failed Build::try_get_compiler");
let mut command = compiler.to_command();

Expand All @@ -101,7 +101,7 @@ fn gen_menu_constants() {
let bin = format!("{}", Path::new(&out_dir).join(if cfg!(windows) { "genmenuconstants.exe" } else { "genmenuconstants" }).display());
let src = format!("{}", Path::new(&out_dir).join("menu_constants.rs").display());

let mut build = cc::Build::new();
let build = cc::Build::new();
let compiler = build.try_get_compiler().expect("Failed Build::try_get_compiler");
let mut command = compiler.to_command();

Expand Down
56 changes: 22 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,26 +593,6 @@ pub fn insstr(s: &str) -> i32
}


pub fn instr(s: &mut String) -> i32
{
/* XXX: This is probably broken. */
unsafe
{
let buf = s.as_bytes().as_ptr();
let ret = ll::instr(mem::transmute(buf));

let capacity = s.capacity();
match s.find('\0')
{
Some(index) => s.as_mut_vec().set_len(index as usize),
None => s.as_mut_vec().set_len(capacity),
}

ret
}
}


pub fn intrflush(w: WINDOW, bf: bool) -> i32
{ unsafe { ll::intrflush(w, bf as ll::c_bool) } }

Expand Down Expand Up @@ -827,14 +807,6 @@ pub fn mvinsstr(y: i32, x: i32, s: &str) -> i32
}


pub fn mvinstr(y: i32, x: i32, s: &mut String) -> i32
{
if mv(y, x) == ERR
{ return ERR; }
instr(s)
}


pub fn mvprintw(y: i32, x: i32, s: &str) -> Result<i32, std::ffi::NulError>
{
if mv(y, x) == ERR
Expand Down Expand Up @@ -1011,8 +983,13 @@ pub fn mvwinstr(w: WINDOW, y: i32, x: i32, s: &mut String) -> i32
}


pub fn mvwprintw(w: WINDOW, y: i32, x: i32, s: &str) -> Result<i32, std::ffi::NulError>
{ unsafe { Ok(ll::mvwprintw(w, y, x, s.to_c_str()?.as_ptr())) } }
pub fn mvwprintw(w: WINDOW, y: i32, x: i32, s: &str) -> Result<i32, std::ffi::NulError> {
// We don't actually need this function to support format strings,
// as they are more safely done in Rust.
unsafe {
Ok(ll::mvwprintw(w, y, x, "%s".to_c_str()?.as_ptr(), s.to_c_str()?.as_ptr()))
}
}


pub fn mvwvline(w: WINDOW, y: i32, x: i32, ch: chtype, n: i32) -> i32
Expand Down Expand Up @@ -1103,9 +1080,15 @@ pub fn prefresh(pad: WINDOW, pmin_row: i32, pmin_col: i32, smin_row: i32, smin_c
{ unsafe { ll::prefresh(pad, pmin_row, pmin_col, smin_row, smin_col, smax_row, smax_col) } }


#[deprecated(since = "5.98.0", note = "printw can segfault when printing string that contains % sign. Use addstr instead")]
#[deprecated(since = "5.98.0", note = "printw format support is disabled. Use addstr instead")]
pub fn printw(s: &str) -> Result<i32, std::ffi::NulError>
{ unsafe { Ok(ll::printw(s.to_c_str()?.as_ptr())) } }
{
// We don't actually need this function to support format strings,
// as they are more safely done in Rust.
unsafe {
Ok(ll::printw("%s".to_c_str()?.as_ptr(), s.to_c_str()?.as_ptr()))
}
}


pub fn putp(s: &str) -> Result<i32, std::ffi::NulError>
Expand Down Expand Up @@ -1632,8 +1615,13 @@ pub fn wnoutrefresh(w: WINDOW) -> i32
{ unsafe { ll::wnoutrefresh(w) } }


pub fn wprintw(w: WINDOW, s: &str) -> Result<i32, std::ffi::NulError>
{ unsafe { Ok(ll::wprintw(w, s.to_c_str()?.as_ptr())) } }
pub fn wprintw(w: WINDOW, s: &str) -> Result<i32, std::ffi::NulError> {
// We don't actually need this function to support format strings,
// as they are more safely done in Rust.
unsafe {
Ok(ll::wprintw(w, "%s".to_c_str()?.as_ptr(), s.to_c_str()?.as_ptr()))
}
}


pub fn wredrawln(w: WINDOW, start: i32, n: i32) -> i32
Expand Down
8 changes: 4 additions & 4 deletions src/ll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ extern {
pub fn mvinsnstr(_:c_int, _:c_int, _:char_p, _:c_int) -> c_int;
pub fn mvinsstr(_:c_int, _:c_int, _:char_p) -> c_int;
pub fn mvinstr(_:c_int, _:c_int, _:char_p) -> c_int;
pub fn mvprintw(_:c_int, _:c_int, _:char_p) -> c_int;
pub fn mvprintw(_:c_int, _:c_int, fmt: char_p, _:char_p) -> c_int;
// fn mvscanw(_:c_int,_:c_int, _:char_p) -> c_int;
pub fn mvvline(_:c_int, _:c_int, _:chtype, _:c_int) -> c_int;
pub fn mvwaddch(_:WINDOW, _:c_int, _:c_int, _:chtype) -> c_int;
Expand All @@ -176,7 +176,7 @@ extern {
pub fn mvwinsnstr(_:WINDOW, _:c_int, _:c_int, _:char_p, _:c_int) -> c_int;
pub fn mvwinsstr(_:WINDOW, _:c_int, _:c_int, _:char_p) -> c_int;
pub fn mvwinstr(_:WINDOW, _:c_int, _:c_int, _:char_p) -> c_int;
pub fn mvwprintw(_:WINDOW, _:c_int, _:c_int, _:char_p) -> c_int;
pub fn mvwprintw(_:WINDOW, _:c_int, _:c_int, fmt: char_p, _:char_p) -> c_int;

// fn mvwscanw(_:WINDOW, _:c_int, _:c_int, _:char_p) -> c_int;
pub fn mvwvline(_:WINDOW, _:c_int, _:c_int, _:chtype, _:c_int) -> c_int;
Expand All @@ -200,7 +200,7 @@ extern {
pub fn pnoutrefresh(_:WINDOW,_:c_int,_:c_int,_:c_int,_:c_int,_:c_int,_:c_int) -> c_int;
pub fn prefresh(_:WINDOW,_:c_int,_:c_int,_:c_int,_:c_int,_:c_int,_:c_int) -> c_int;

pub fn printw(_:char_p) -> c_int;
pub fn printw(fmt: char_p, _:char_p) -> c_int;
pub fn putwin(_:WINDOW, _:FILE_p) -> c_int;
pub fn qiflush();
pub fn raw() -> c_int;
Expand Down Expand Up @@ -311,7 +311,7 @@ extern {
pub fn winstr(_:WINDOW, _:char_p) -> c_int;
pub fn wmove(_:WINDOW,_:c_int,_:c_int) -> c_int;
pub fn wnoutrefresh(_:WINDOW) -> c_int;
pub fn wprintw(_:WINDOW, _:char_p) -> c_int;
pub fn wprintw(_:WINDOW, fmt: char_p, _:char_p) -> c_int;
pub fn wredrawln(_:WINDOW,_:c_int,_:c_int) -> c_int;
pub fn wrefresh(_:WINDOW) -> c_int;
pub fn wresize(_:WINDOW, _:c_int, _:c_int) -> c_int;
Expand Down

0 comments on commit f7f2179

Please sign in to comment.