Skip to content

Commit 114fc1a

Browse files
andy-shevtorvalds
authored andcommitted
hexdump: make it return number of bytes placed in buffer
This patch makes hexdump return the number of bytes placed in the buffer excluding trailing NUL. In the case of overflow it returns the desired amount of bytes to produce the entire dump. Thus, it mimics snprintf(). This will be useful for users that would like to repeat with a bigger buffer. [akpm@linux-foundation.org: fix printk warning] Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5d909c8 commit 114fc1a

3 files changed

Lines changed: 103 additions & 21 deletions

File tree

‎include/linux/printk.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ enum {
417417
DUMP_PREFIX_ADDRESS,
418418
DUMP_PREFIX_OFFSET
419419
};
420-
extern void hex_dump_to_buffer(const void *buf, size_t len,
421-
int rowsize, int groupsize,
422-
char *linebuf, size_t linebuflen, bool ascii);
420+
extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
421+
int groupsize, char *linebuf, size_t linebuflen,
422+
bool ascii);
423423
#ifdef CONFIG_PRINTK
424424
extern void print_hex_dump(const char *level, const char *prefix_str,
425425
int prefix_type, int rowsize, int groupsize,

‎lib/hexdump.c‎

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,26 @@ EXPORT_SYMBOL(bin2hex);
9797
*
9898
* example output buffer:
9999
* 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
100+
*
101+
* Return:
102+
* The amount of bytes placed in the buffer without terminating NUL. If the
103+
* output was truncated, then the return value is the number of bytes
104+
* (excluding the terminating NUL) which would have been written to the final
105+
* string if enough space had been available.
100106
*/
101-
void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
102-
int groupsize, char *linebuf, size_t linebuflen,
103-
bool ascii)
107+
int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
108+
char *linebuf, size_t linebuflen, bool ascii)
104109
{
105110
const u8 *ptr = buf;
106111
int ngroups;
107112
u8 ch;
108113
int j, lx = 0;
109114
int ascii_column;
115+
int ret;
110116

111117
if (rowsize != 16 && rowsize != 32)
112118
rowsize = 16;
113119

114-
if (!len)
115-
goto nil;
116120
if (len > rowsize) /* limit to one line at a time */
117121
len = rowsize;
118122
if (!is_power_of_2(groupsize) || groupsize > 8)
@@ -122,27 +126,50 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
122126

123127
ngroups = len / groupsize;
124128
ascii_column = rowsize * 2 + rowsize / groupsize + 1;
129+
130+
if (!linebuflen)
131+
goto overflow1;
132+
133+
if (!len)
134+
goto nil;
135+
125136
if (groupsize == 8) {
126137
const u64 *ptr8 = buf;
127138

128-
for (j = 0; j < ngroups; j++)
129-
lx += scnprintf(linebuf + lx, linebuflen - lx,
130-
"%s%16.16llx", j ? " " : "",
131-
(unsigned long long)*(ptr8 + j));
139+
for (j = 0; j < ngroups; j++) {
140+
ret = snprintf(linebuf + lx, linebuflen - lx,
141+
"%s%16.16llx", j ? " " : "",
142+
(unsigned long long)*(ptr8 + j));
143+
if (ret >= linebuflen - lx)
144+
goto overflow1;
145+
lx += ret;
146+
}
132147
} else if (groupsize == 4) {
133148
const u32 *ptr4 = buf;
134149

135-
for (j = 0; j < ngroups; j++)
136-
lx += scnprintf(linebuf + lx, linebuflen - lx,
137-
"%s%8.8x", j ? " " : "", *(ptr4 + j));
150+
for (j = 0; j < ngroups; j++) {
151+
ret = snprintf(linebuf + lx, linebuflen - lx,
152+
"%s%8.8x", j ? " " : "",
153+
*(ptr4 + j));
154+
if (ret >= linebuflen - lx)
155+
goto overflow1;
156+
lx += ret;
157+
}
138158
} else if (groupsize == 2) {
139159
const u16 *ptr2 = buf;
140160

141-
for (j = 0; j < ngroups; j++)
142-
lx += scnprintf(linebuf + lx, linebuflen - lx,
143-
"%s%4.4x", j ? " " : "", *(ptr2 + j));
161+
for (j = 0; j < ngroups; j++) {
162+
ret = snprintf(linebuf + lx, linebuflen - lx,
163+
"%s%4.4x", j ? " " : "",
164+
*(ptr2 + j));
165+
if (ret >= linebuflen - lx)
166+
goto overflow1;
167+
lx += ret;
168+
}
144169
} else {
145-
for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
170+
for (j = 0; j < len; j++) {
171+
if (linebuflen < lx + 3)
172+
goto overflow2;
146173
ch = ptr[j];
147174
linebuf[lx++] = hex_asc_hi(ch);
148175
linebuf[lx++] = hex_asc_lo(ch);
@@ -154,14 +181,24 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
154181
if (!ascii)
155182
goto nil;
156183

157-
while (lx < (linebuflen - 1) && lx < ascii_column)
184+
while (lx < ascii_column) {
185+
if (linebuflen < lx + 2)
186+
goto overflow2;
158187
linebuf[lx++] = ' ';
159-
for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) {
188+
}
189+
for (j = 0; j < len; j++) {
190+
if (linebuflen < lx + 2)
191+
goto overflow2;
160192
ch = ptr[j];
161193
linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
162194
}
163195
nil:
196+
linebuf[lx] = '\0';
197+
return lx;
198+
overflow2:
164199
linebuf[lx++] = '\0';
200+
overflow1:
201+
return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
165202
}
166203
EXPORT_SYMBOL(hex_dump_to_buffer);
167204

‎lib/test-hexdump.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,45 @@ static void __init test_hexdump_set(int rowsize, bool ascii)
114114
test_hexdump(len, rowsize, 1, ascii);
115115
}
116116

117+
static void __init test_hexdump_overflow(bool ascii)
118+
{
119+
char buf[56];
120+
const char *t = test_data_1_le[0];
121+
size_t l = get_random_int() % sizeof(buf);
122+
bool a;
123+
int e, r;
124+
125+
memset(buf, ' ', sizeof(buf));
126+
127+
r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, l, ascii);
128+
129+
if (ascii)
130+
e = 50;
131+
else
132+
e = 2;
133+
buf[e + 2] = '\0';
134+
135+
if (!l) {
136+
a = r == e && buf[0] == ' ';
137+
} else if (l < 3) {
138+
a = r == e && buf[0] == '\0';
139+
} else if (l < 4) {
140+
a = r == e && !strcmp(buf, t);
141+
} else if (ascii) {
142+
if (l < 51)
143+
a = r == e && buf[l - 1] == '\0' && buf[l - 2] == ' ';
144+
else
145+
a = r == e && buf[50] == '\0' && buf[49] == '.';
146+
} else {
147+
a = r == e && buf[e] == '\0';
148+
}
149+
150+
if (!a) {
151+
pr_err("Len: %zu rc: %u strlen: %zu\n", l, r, strlen(buf));
152+
pr_err("Result: '%s'\n", buf);
153+
}
154+
}
155+
117156
static int __init test_hexdump_init(void)
118157
{
119158
unsigned int i;
@@ -129,6 +168,12 @@ static int __init test_hexdump_init(void)
129168
for (i = 0; i < 16; i++)
130169
test_hexdump_set(rowsize, true);
131170

171+
for (i = 0; i < 16; i++)
172+
test_hexdump_overflow(false);
173+
174+
for (i = 0; i < 16; i++)
175+
test_hexdump_overflow(true);
176+
132177
return -EINVAL;
133178
}
134179
module_init(test_hexdump_init);

0 commit comments

Comments
 (0)