History

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.

Andy Shevchenko committed 11 years ago

Commit 9f029f51 parent 8de1ee71 file · +5 −1Browse files

Unified · Split

lib/hexdump.c+5−1
@@ -169,11 +169,15 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, · 168 unmodified lines
169 }169 }
170 } else {170 } else {
171 for (j = 0; j < len; j++) {171 for (j = 0; j < len; j++) {
172 if (linebuflen < lx + 3)172 if (linebuflen < lx + 2)
173 goto overflow2;173 goto overflow2;
174 ch = ptr[j];174 ch = ptr[j];
175 linebuf[lx++] = hex_asc_hi(ch);175 linebuf[lx++] = hex_asc_hi(ch);
176 if (linebuflen < lx + 2)
177 goto overflow2;
176 linebuf[lx++] = hex_asc_lo(ch);178 linebuf[lx++] = hex_asc_lo(ch);
179 if (linebuflen < lx + 2)
180 goto overflow2;
177 linebuf[lx++] = ' ';181 linebuf[lx++] = ' ';
178 }182 }
179 if (j)183 if (j)