Bug#776096: bsdmainutils: hexdump fails if format_string contains backslash
On Fri, Mar 11, 2016 at 10:51:21AM -0500, Rémi Rampin wrote: > Just a reminder that there is still a severe and obvious logic error in the > escape() for loop. I uploaded a patch to fix it 6 months ago. The new > version you uploaded still exhibits the same bugs. Sorry, completely missed it. Might have helped had it been tagged patch, but we never know. Anyhow, thanks, will include in 9.0.8 which is due today. Michael -- Michael Meskes Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org) Meskes at (Debian|Postgresql) dot Org Jabber: michael at xmpp dot meskes dot org VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL
Bug#776096: bsdmainutils: hexdump fails if format_string contains backslash
Just a reminder that there is still a severe and obvious logic error in the escape() for loop. I uploaded a patch to fix it 6 months ago. The new version you uploaded still exhibits the same bugs. Regards
Bug#776096: bsdmainutils: hexdump fails if format_string contains backslash
There's actually another bug in escape(): the terminating null byte doesn't break out of the loop if the last character is a backslash. You can't see the garbage that gets copied to the buffer because the null byte gets copied, the the overrun does happen. New patch that also addresses that. --- bsdmainutils/usr.bin/hexdump/parse.c2015-09-04 09:04:57.0 -0400 +++ bsdmainutils/usr.bin/hexdump/parse.c2015-09-04 10:12:27.705336045 -0400 @@ -454,10 +454,6 @@ /* alphabetic escape sequences have to be done in place */ for (p2 = p1;; ++p1, ++p2) { - if (!*p1) { - *p2 = *p1; - break; - } if (*p1 == '\\') switch(*++p1) { case 'a': @@ -486,6 +482,10 @@ *p2 = *p1; break; } + else + *p2 = *p1; + if(!*p1) + break; } }
Bug#776096: bsdmainutils: hexdump fails if format_string contains backslash
Nevermind, I found the bug in the source. It actually fumbles if the backslash *isn't the last character in the format unit*. New patch attached, fixes up escape() in hexdump/parse.c I don't know if there is an upstream to report this to, but other distributions are affected. --- bsdmainutils/usr.bin/hexdump/parse.c 2015-09-04 09:04:57.0 -0400 +++ bsdmainutils/usr.bin/hexdump/parse.c 2015-09-04 09:04:57.0 -0400 @@ -454,10 +454,6 @@ /* alphabetic escape sequences have to be done in place */ for (p2 = p1;; ++p1, ++p2) { - if (!*p1) { - *p2 = *p1; - break; - } if (*p1 == '\\') switch(*++p1) { case 'a': @@ -486,6 +482,11 @@ *p2 = *p1; break; } +else { +*p2 = *p1; +if(!*p1) +break; +} } }
Bug#776096: bsdmainutils: hexdump fails if format_string contains backslash
I think this workaround warrants a mention in the manpage. I adjusted an example in debian/patches/hexdump_man.diff accordingly, see the attachment. Maybe it should also say somewhere that in order to output a literal backslash it must be put onto a separate format unit. Description: Add examples to manpage. Author: Michael Meskes --- freebsd/usr.bin/hexdump/hexdump.1.orig 2010-11-03 19:36:36.0 +0100 +++ bsdmainutils/usr.bin/hexdump/hexdump.1 2010-11-03 19:37:44.0 +0100 @@ -197,7 +197,7 @@ .Xr fprintf 3 default which prints the entire string if the precision is unspecified). .It -The conversion characters ``h'', ``l'', ``n'', ``p'' and ``q'' are +The conversion characters ``%'', ``h'', ``l'', ``n'', ``p'' and ``q'' are not supported. .It The single character escape sequences @@ -346,6 +346,49 @@ "%07.7_Ax\en" "%07.7_ax " 8/2 "%04x " "\en" .Ed +.Pp +Some examples for the \-e option: +.Bd -literal -offset indent +# hex bytes +% echo hello | hexdump \-v \-e '/1 "%02X "' ; echo +68 65 6C 6C 6F 0A + +# same, with ASCII section +% echo hello | hexdump \-e '8/1 "%02X ""\\t"" "' \-e '8/1 "%c""\\n"' +68 65 6C 6C 6F 0Ahello + +# hex with preceding '\\x' +% echo hello | hexdump \-v \-e '"" 1/1 "x%02X" " "' ; echo +\\x68 \\x65 \\x6C \\x6C \\x6F \\x0A + +# one hex byte per line +% echo hello | hexdump \-v \-e '/1 "%02X\\n"' +68 +65 +6C +6C +6F +0A + +# a table of byte#, hex, decimal, octal, ASCII +% echo hello | hexdump \-v \-e '/1 "%_ad#"' \-e '/1"%02X hex"' \-e '/1 " = %03i dec"' \-e '/1 " = %03o oct"' \-e '/1 " = _%c\\_\\n"' +0#68 hex = 104 dec = 150 oct = _h_ +1#65 hex = 101 dec = 145 oct = _e_ +2#6C hex = 108 dec = 154 oct = _l_ +3#6C hex = 108 dec = 154 oct = _l_ +4#6F hex = 111 dec = 157 oct = _o_ +5#0A hex = 010 dec = 012 oct = _ +_ + +# byte# & ASCII with control chars +% echo hello | hexdump \-v \-e '/1 "%_ad# "' \-e '/1 " _%_u\\_\\n"' +0# _h_ +1# _e_ +2# _l_ +3# _l_ +4# _o_ +5# _lf_ +.Ed .Sh SEE ALSO .Xr gdb 1 , .Xr od 1
Bug#776096: bsdmainutils: hexdump fails if format_string contains backslash
This is a neat workaround, but I really wouldn't say that "it works".
Bug#776096: bsdmainutils: hexdump fails if format_string contains backslash
Actually, it works. Try $ echo hello|hexdump -ve '"\\" /1 "x%02X"';printf \\n \x68\x65\x6C\x6C\x6F\x0A
Bug#776096: bsdmainutils: hexdump fails if format_string contains backslash
Package: bsdmainutils Version: 9.0.6 Severity: important Tags: upstream Dear Maintainer, I was trying to get hexdump to output a C-string with hexadecimal escapes, like "\x68\x65\x6C\x6C\x6F\x0A". Not only did I never reach that result, but I also run into error messages that don't appear to make much sense. $ echo hello | hexdump -v -e "\"\\x\" 1/1 \"%02X\"" x68x65x6Cx6Cx6Fx0A $ echo hello | hexdump -v -e "\"x\" 1/1 \"%02X\"" \\68\\65\\6C\\6C\\6F\\0A $ echo hello | hexdump -v -e "1/1 \"\\x%02X\"" hexdump: %A: bad conversion character $ echo hello | hexdump -v -e "1/1 \"x%02X\"" hexdump: %A: bad conversion character -- System Information: Debian Release: 8.0 APT prefers testing-updates APT policy: (500, 'testing-updates'), (500, 'testing') Architecture: amd64 (x86_64) Kernel: Linux 3.16.0-4-amd64 (SMP w/4 CPU cores) Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8) Shell: /bin/sh linked to /bin/dash Init: systemd (via /run/systemd/system) Versions of packages bsdmainutils depends on: ii bsdutils 1:2.25.2-4 ii debianutils 4.4+b1 ii libc62.19-13 ii libncurses5 5.9+20140913-1+b1 ii libtinfo55.9+20140913-1+b1 bsdmainutils recommends no packages. Versions of packages bsdmainutils suggests: ii cpp 4:4.9.1-5 pn vacation ii wamerican [wordlist] 7.1-1 ii whois 5.2.2 -- no debconf information -- To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org