From 8ca6e6559ef3e8276e28890f81e0b679d804c563 Mon Sep 17 00:00:00 2001 From: Thomas Habets Date: Sat, 30 Mar 2024 17:18:56 +0000 Subject: [PATCH 1/3] Remove instr/mvinstr: If it's impossible to use correctly, it should be removed Bug #209 --- src/lib.rs | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e9f11fc..53b3a2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) } } @@ -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 { if mv(y, x) == ERR From 8fd11147ec9cc85d07a21f71afd07874e06f4a5f Mon Sep 17 00:00:00 2001 From: Thomas Habets Date: Sat, 30 Mar 2024 17:30:14 +0000 Subject: [PATCH 2/3] Remove possibility of accidental format strings in print functions Bug #209 --- src/lib.rs | 28 ++++++++++++++++++++++------ src/ll.rs | 8 ++++---- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 53b3a2c..db262e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 -{ 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 { + // 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 @@ -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 -{ 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 @@ -1604,8 +1615,13 @@ pub fn wnoutrefresh(w: WINDOW) -> i32 { unsafe { ll::wnoutrefresh(w) } } -pub fn wprintw(w: WINDOW, s: &str) -> Result -{ unsafe { Ok(ll::wprintw(w, s.to_c_str()?.as_ptr())) } } +pub fn wprintw(w: WINDOW, s: &str) -> Result { + // 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 diff --git a/src/ll.rs b/src/ll.rs index 24621ba..d6916ec 100644 --- a/src/ll.rs +++ b/src/ll.rs @@ -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; @@ -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; @@ -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; @@ -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; From f307e05167ba06c0f5a70b90ec4fd050cded2883 Mon Sep 17 00:00:00 2001 From: Thomas Habets Date: Sat, 30 Mar 2024 17:32:41 +0000 Subject: [PATCH 3/3] Remove needless `mut` that compiler warned about --- build.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 2dc8d20..1489d53 100644 --- a/build.rs +++ b/build.rs @@ -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(); @@ -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();