diff --git a/bin/CHANGELOG.md b/bin/CHANGELOG.md index 6767e29..84b4545 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 panic when the block value is too large - 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 diff --git a/bin/src/main.rs b/bin/src/main.rs index aed6e7e..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,7 +235,7 @@ fn wrapped_main(program: &str, args: Vec) -> Result<()> { r"Usage: {program} --mode= --base= [] Usage: {program} --mode= --symbols= []" ); - print!( + return println!( "{0} Examples: # Using the RFC4648 base64 encoding @@ -246,17 +254,14 @@ Examples: {1} --mode=decode \\ --symbols=0123456789bcdfghjklmnpqrstuvwxyz \\ --translate=BCDFGHJKLMNPQRSTUVWXYZbcdfghjklmnpqrstuvwxyz \\ - --least_significant_bit_first -", + --least_significant_bit_first", opts.usage(&brief), program ); - return Ok(()); } if args.len() == 1 && (args[0] == "--version" || args[0] == "-v") { - println!("{}", env!("CARGO_PKG_VERSION")); - return Ok(()); + return println!("{}", env!("CARGO_PKG_VERSION")); } let matches = opts.parse(&args).map_err(Error::ParseOpts)?; @@ -324,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) => { - println!("{:#?}", base.specification()); - return Ok(()); - } + x if "specification".starts_with(x) => return println!("{:#?}", base.specification()), _ => return Err(Error::Cmdline("Invalid mode".into())), }; diff --git a/bin/test.sh b/bin/test.sh index 46b212f..5735f18 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -119,6 +119,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:'