Skip to content

Commit 9f029f5

Browse files
andy-shevtorvalds
authored andcommitted
lib/hexdump.c: truncate output in case of overflow
There is a classical off-by-one error in case when we try to place, for example, 1+1 bytes as hex in the buffer of size 6. The expected result is to get an output truncated, but in the reality we get 6 bytes filed followed by terminating NUL. Change the logic how we fill the output in case of byte dumping into limited space. This will follow the snprintf() behaviour by truncating output even on half bytes. Fixes: 114fc1a (hexdump: make it return number of bytes placed in buffer) Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reported-by: Aaro Koskinen <aaro.koskinen@nokia.com> Tested-by: Aaro Koskinen <aaro.koskinen@nokia.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8de1ee7 commit 9f029f5

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎lib/hexdump.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,15 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
169169
}
170170
} else {
171171
for (j = 0; j < len; j++) {
172-
if (linebuflen < lx + 3)
172+
if (linebuflen < lx + 2)
173173
goto overflow2;
174174
ch = ptr[j];
175175
linebuf[lx++] = hex_asc_hi(ch);
176+
if (linebuflen < lx + 2)
177+
goto overflow2;
176178
linebuf[lx++] = hex_asc_lo(ch);
179+
if (linebuflen < lx + 2)
180+
goto overflow2;
177181
linebuf[lx++] = ' ';
178182
}
179183
if (j)

0 commit comments

Comments
 (0)