From df6f1ba19ccb836939b4dae65b62254702ca345d Mon Sep 17 00:00:00 2001 From: Arcterus Date: Thu, 19 Jun 2014 16:26:49 -0700 Subject: [PATCH] echo: fix handling of \0 --- echo/echo.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/echo/echo.rs b/echo/echo.rs index febdc77d3..bddbf4e47 100644 --- a/echo/echo.rs +++ b/echo/echo.rs @@ -61,19 +61,22 @@ fn convert_str(string: &str, index: uint, base: uint) -> (char, int) { let mut bytes = vec!(); for offset in range(0, max_digits) { + if string.len() <= index + offset as uint { + break; + } let c = string[index + offset as uint]; if is_legal_digit(c) { bytes.push(c as u8); } else { - if bytes.len() > 0 { - return (to_char(&bytes, base), offset); - } else { - return (' ', offset); - } + break; } } - (to_char(&bytes, base), max_digits) + if bytes.len() == 0 { + (' ', 0) + } else { + (to_char(&bytes, base), bytes.len() as int) + } } fn parse_options(args: Vec, options: &mut EchoOptions) -> Option> { @@ -181,8 +184,7 @@ pub fn uumain(args: Vec) -> int { '0' => { let (c, num_char_used) = convert_str(string.as_slice(), index + 1, 8u); if num_char_used == 0 { - print_char('\\'); - print_char('0'); + print_char('\0'); } else { print_char(c); for _ in range(0, num_char_used) {