Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
Remove possibility of accidental format strings in print functions
Browse files Browse the repository at this point in the history
Bug #209
  • Loading branch information
ThomasHabets committed Mar 30, 2024
1 parent 8ca6e65 commit 8fd1114
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
28 changes: 22 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,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 @@ -1075,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 @@ -1604,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 8fd1114

Please sign in to comment.