Commit c67ae69b661f3c2fe1a9c8259bc948c68b082166
Committed by
Linus Torvalds
1 parent
a9c5695393
Exists in
master
and in
4 other branches
hexdump: remove the trailing space
For example: hex_dump_to_buffer("AB", 2, 16, 1, buf, 100, 0); pr_info("[%s]\n", buf); I'd expect the output to be "[41 42]", but actually it's "[41 42 ]" This patch also makes the required buf to be minimum. To print the hex format of "AB", a buf with size 6 should be sufficient, but hex_dump_to_buffer() required at least 8. Signed-off-by: Li Zefan <lizf@cn.fujitsu.com> Acked-by: Randy Dunlap <randy.dunlap@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Showing 1 changed file with 9 additions and 6 deletions Side-by-side Diff
lib/hexdump.c
... | ... | @@ -65,7 +65,8 @@ |
65 | 65 | |
66 | 66 | for (j = 0; j < ngroups; j++) |
67 | 67 | lx += scnprintf(linebuf + lx, linebuflen - lx, |
68 | - "%16.16llx ", (unsigned long long)*(ptr8 + j)); | |
68 | + "%s%16.16llx", j ? " " : "", | |
69 | + (unsigned long long)*(ptr8 + j)); | |
69 | 70 | ascii_column = 17 * ngroups + 2; |
70 | 71 | break; |
71 | 72 | } |
... | ... | @@ -76,7 +77,7 @@ |
76 | 77 | |
77 | 78 | for (j = 0; j < ngroups; j++) |
78 | 79 | lx += scnprintf(linebuf + lx, linebuflen - lx, |
79 | - "%8.8x ", *(ptr4 + j)); | |
80 | + "%s%8.8x", j ? " " : "", *(ptr4 + j)); | |
80 | 81 | ascii_column = 9 * ngroups + 2; |
81 | 82 | break; |
82 | 83 | } |
83 | 84 | |
84 | 85 | |
... | ... | @@ -87,19 +88,21 @@ |
87 | 88 | |
88 | 89 | for (j = 0; j < ngroups; j++) |
89 | 90 | lx += scnprintf(linebuf + lx, linebuflen - lx, |
90 | - "%4.4x ", *(ptr2 + j)); | |
91 | + "%s%4.4x", j ? " " : "", *(ptr2 + j)); | |
91 | 92 | ascii_column = 5 * ngroups + 2; |
92 | 93 | break; |
93 | 94 | } |
94 | 95 | |
95 | 96 | default: |
96 | - for (j = 0; (j < rowsize) && (j < len) && (lx + 4) < linebuflen; | |
97 | - j++) { | |
97 | + for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) { | |
98 | 98 | ch = ptr[j]; |
99 | 99 | linebuf[lx++] = hex_asc_hi(ch); |
100 | 100 | linebuf[lx++] = hex_asc_lo(ch); |
101 | 101 | linebuf[lx++] = ' '; |
102 | 102 | } |
103 | + if (j) | |
104 | + lx--; | |
105 | + | |
103 | 106 | ascii_column = 3 * rowsize + 2; |
104 | 107 | break; |
105 | 108 | } |
... | ... | @@ -108,7 +111,7 @@ |
108 | 111 | |
109 | 112 | while (lx < (linebuflen - 1) && lx < (ascii_column - 1)) |
110 | 113 | linebuf[lx++] = ' '; |
111 | - for (j = 0; (j < rowsize) && (j < len) && (lx + 2) < linebuflen; j++) | |
114 | + for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) | |
112 | 115 | linebuf[lx++] = (isascii(ptr[j]) && isprint(ptr[j])) ? ptr[j] |
113 | 116 | : '.'; |
114 | 117 | nil: |