From 1eff1e8214568e68dcd97b71adcdb0626d77407d Mon Sep 17 00:00:00 2001 From: FRIGN Date: Mon, 26 Oct 2015 12:29:02 +0100 Subject: [PATCH] Properly handle partial chunks in od(1) Grab the remaining bytes and fill them up with zeroes in a temporary buffer. --- od.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/od.c b/od.c index 0bd8f6c..0c361e2 100644 --- a/od.c +++ b/od.c @@ -93,6 +93,7 @@ printline(unsigned char *line, size_t len, off_t addr) struct type *t = NULL; size_t i; int first = 1; + unsigned char *tmp; if (TAILQ_EMPTY(&head)) goto once; @@ -104,10 +105,17 @@ once: } else { printf("%*c", (addr_format == 'n') ? 1 : 7, ' '); } - for (i = 0; i < len; ) { - printchunk(line + i, t ? t->format : 'o', - MIN(len - i, t ? t->len : 4)); - i += MIN(len - i, t ? t->len : 4); + for (i = 0; i < len; i += MIN(len - i, t ? t->len : 4)) { + if (len - i < (t ? t->len : 4)) { + tmp = ecalloc(t ? t->len : 4, 1); + memcpy(tmp, line + i, len - i); + printchunk(tmp, t ? t->format : 'o', + t ? t->len : 4); + free(tmp); + } else { + printchunk(line + i, t ? t->format : 'o', + t ? t->len : 4); + } } fputc('\n', stdout); if (TAILQ_EMPTY(&head) || (!len && !first))