From bb75140f2c280944157c213f7a656624961df806 Mon Sep 17 00:00:00 2001 From: VAMSI KRISHNA Date: Tue, 6 Oct 2026 18:59:34 +0000 Subject: [PATCH 1/2] Fix panic when printing the help, version, or specification output fails --- bin/CHANGELOG.md | 1 + bin/src/main.rs | 10 ++++++---- bin/test.sh | 3 +++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/bin/CHANGELOG.md b/bin/CHANGELOG.md index 848da00..42321da 100644 --- a/bin/CHANGELOG.md +++ b/bin/CHANGELOG.md @@ -4,6 +4,7 @@ ### Patch +- Fix panic when printing the help, version, or specification output fails - Fix inaccurate error positions when decoding by small blocks with ignored characters - Fix panic when the width value is not a multiple of the base or is too large - Fix write errors being silently ignored when flushing the end of the output diff --git a/bin/src/main.rs b/bin/src/main.rs index 59e96b0..703a22b 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -227,7 +227,8 @@ fn wrapped_main(program: &str, args: Vec) -> Result<()> { r"Usage: {program} --mode= --base= [] Usage: {program} --mode= --symbols= []" ); - print!( + write!( + std::io::stdout(), "{0} Examples: # Using the RFC4648 base64 encoding @@ -250,12 +251,13 @@ Examples: ", opts.usage(&brief), program - ); + ) + .map_err(Error::Write)?; return Ok(()); } if args.len() == 1 && (args[0] == "--version" || args[0] == "-v") { - println!("{}", env!("CARGO_PKG_VERSION")); + writeln!(std::io::stdout(), "{}", env!("CARGO_PKG_VERSION")).map_err(Error::Write)?; return Ok(()); } @@ -325,7 +327,7 @@ Examples: x if "encode".starts_with(x) => true, x if "decode".starts_with(x) => false, x if "specification".starts_with(x) => { - println!("{:#?}", base.specification()); + writeln!(std::io::stdout(), "{:#?}", base.specification()).map_err(Error::Write)?; return Ok(()); } _ => return Err(Error::Cmdline("Invalid mode".into())), diff --git a/bin/test.sh b/bin/test.sh index 5f18591..7384241 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -114,6 +114,9 @@ if [ -e /dev/full ]; then 'Write error: No space left on device (os error 28)' unit '-md -b64 -p= -o /dev/full' Zm9v '' \ 'Write error: No space left on device (os error 28)' + unit '-ms -b64 >/dev/full' '' '' 'Write error: No space left on device (os error 28)' + unit '--version >/dev/full' '' '' 'Write error: No space left on device (os error 28)' + unit '--help >/dev/full' '' '' 'Write error: No space left on device (os error 28)' fi echo echo -n 'custom:' From 1786f292d5b50f065d9edf530b1f1e6e1f539c39 Mon Sep 17 00:00:00 2001 From: ia0 Date: Wed, 7 Oct 2026 10:34:29 +0200 Subject: [PATCH 2/2] review --- bin/src/main.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/bin/src/main.rs b/bin/src/main.rs index 40c85e1..0d36b0a 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -14,6 +14,14 @@ macro_rules! check { }; } +macro_rules! println { + ($($x:tt)*) => {{ + let mut stdout = std::io::stdout(); + let result = writeln!(stdout, $($x)*).map_err(Error::Write); + result.and_then(|()| stdout.flush().map_err(Error::Write)) + }}; +} + #[derive(Debug)] pub enum Error { ParseOpts(getopts::Fail), @@ -227,8 +235,7 @@ fn wrapped_main(program: &str, args: Vec) -> Result<()> { r"Usage: {program} --mode= --base= [] Usage: {program} --mode= --symbols= []" ); - write!( - std::io::stdout(), + return println!( "{0} Examples: # Using the RFC4648 base64 encoding @@ -247,18 +254,14 @@ Examples: {1} --mode=decode \\ --symbols=0123456789bcdfghjklmnpqrstuvwxyz \\ --translate=BCDFGHJKLMNPQRSTUVWXYZbcdfghjklmnpqrstuvwxyz \\ - --least_significant_bit_first -", + --least_significant_bit_first", opts.usage(&brief), program - ) - .map_err(Error::Write)?; - return Ok(()); + ); } if args.len() == 1 && (args[0] == "--version" || args[0] == "-v") { - writeln!(std::io::stdout(), "{}", env!("CARGO_PKG_VERSION")).map_err(Error::Write)?; - return Ok(()); + return println!("{}", env!("CARGO_PKG_VERSION")); } let matches = opts.parse(&args).map_err(Error::ParseOpts)?; @@ -326,10 +329,7 @@ Examples: "" => return Err(Error::Cmdline("Empty mode".into())), x if "encode".starts_with(x) => true, x if "decode".starts_with(x) => false, - x if "specification".starts_with(x) => { - writeln!(std::io::stdout(), "{:#?}", base.specification()).map_err(Error::Write)?; - return Ok(()); - } + x if "specification".starts_with(x) => return println!("{:#?}", base.specification()), _ => return Err(Error::Cmdline("Invalid mode".into())), };